Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - @Autowired method when execute

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.

like image 902
user218867 Avatar asked Dec 08 '25 20:12

user218867


1 Answers

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.

like image 94
Andrei Stefan Avatar answered Dec 11 '25 10:12

Andrei Stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!