replace with Spring Annotation

后端 未结 4 1580
傲寒
傲寒 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:32

    Spring's Java Configuration might be of help here. If you create a Java class that simply defines your beans using the annotations @Configuration and @Bean it could look something like this:

    @Configuration
    public class DaoConfiguration {
        @Bean
        public GenericDAO personDao() {
            return new GenericDaoHibernateImpl(Person.class);
        }
    }
    

    Make sure that the DaoConfiguration class is scanned (typically via @ComponentScan) and a proper DAO-object will be created for you in the Spring context. The bean will have the name of the method which in this case is personDao so you can inject it by name using the name personDao or by type if the type is GenericDAO.

提交回复
热议问题