在xml文件中引入带有@Configuration的Java类

在xml引入带有@Configuration的Java类,就是把这个带有@Configuration的Java类,当做一个普通的的类用<bean>标签引入:

核心代码如下:

@Configuration
public class AppConfig {
@Autowired
private DataSource dataSource;
@Bean
public AccountRepository accountRepository() {
return new JdbcAccountRepository(dataSource);
}
@Bean
public TransferService transferService() {
return new TransferService(accountRepository());
}
}
<beans>
<!-- enable processing of annotations such as @Autowired and @Configuration -->
<!--这个必须要带否则会报错--> <context:annotation-config/> <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<!--引入带有@Configuration的Java类--> <bean class="com.acme.AppConfig"/> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>
原文地址:https://www.cnblogs.com/1540340840qls/p/7940921.html