Spring - 执行时@Autowired 方法

Eri*_*ang 3 java spring inversion-of-control autowired

我有一个BaseDaoImpl类,它有以下方法:

@Autowired
public void initSessionFactory(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    super.setSqlSessionFactory(sqlSessionFactory);
    System.out.println("------ ok ------");
}
Run Code Online (Sandbox Code Playgroud)

我定义了一个子类UserDaoImpl,其中implements BaseDaoImpl. 并将其定义为 bean。
在init Spring context的时候,发现initSessionFactory()方法是自动执行的,但是我没有调用任何方法。
根据我的理解,该方法仅在我调用它时才会执行并自动装配其参数,有人可以帮助解释它是如何工作的吗?谢谢。

And*_*fan 5

这种行为很正常。您initSessionFactory注释的方法@Autowired被认为是一个config method@Autowired可以放在构造函数、字段和方法上。创建 bean 时,首先调用构造函数,然后注入字段,然后调用配置方法。

配置方法的(用 注释@Autowired)参数将与 Spring 容器中的匹配 bean 自动装配。

有关更多详细信息,请参阅Javadoc API for Autowired annotation