Spring - @Autowired method when execute

autowired, inversion-of-control, java, spring

Solution

That behavior is normal. Your `initSessionFactory` method annotated with `@Autowired` is considered to be a config method. `@Autowired` can be placed on constructors, fields and methods. When the bean is created, first the constructor is called, then the fields are injected and then config methods are called.

A config method's (annotated with `@Autowired`) arguments will be autowired with a matching bean in the Spring container.

See the Javadoc API for Autowired annotation for more details.

Problem

I have a `BaseDaoImpl` class, it has following method: ``` @Autowired public void initSessionFactory(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); System.out.println("------ ok ------"); } ``` I defined a subclass `UserDaoImpl`, which `implements BaseDaoImpl`. And defined it as a bean. When init Spring context, I found that the `initSessionFactory()` method is executed automatically, but I didn't call any methods. In my understanding, the method is executed & autowire its params only when I call it, can someone help to explain how it works? Thanks.

Original source