replace with Spring Annotation

后端 未结 4 1587
傲寒
傲寒 2021-01-02 07:32

there is a way to replace constructor-arg with Annotation?

I have this constructor:

public GenericDAOImpl(Class type) {
    this.type = type         


        
4条回答
  •  没有蜡笔的小新
    2021-01-02 08:23

    I think @Inject alone won't help, you will have to use a @Qualifier annotation also.

    Here's the relevant Section of the Spring Reference:
    3.9.3 Fine-tuning annotation-based autowiring with qualifiers

    If I understand this correctly, you will have to use the @Qualifier mechanism.

    If you use Spring's @Qualifier annotation, you can probably do it inline, something like this:

    @Repository
    public class DaoImpl implements Dao{
    
        private final Class type;
    
        public DaoImpl(@Qualifier("type") final Class type){
            this.type = type;
        }
    
    }
    

    But if you use the JSR-330 @Qualifier annotation, I guess you will have to create your own custom annotation that is marked with @Qualifier.


    Another possibility would be the @Value annotation. With it you can use Expression Language, e.g. like this:

    public DaoImpl(
        @Value("#{ systemProperties['dao.type'] }")
        final Class type){
        this.type = type;
    }
    

提交回复
热议问题