这是我的Spring xml配置:
<beans profile="test">
<context:annotation-config />
<!-- hsqldbDataSource bean for testing purposes -->
<bean id="hsqldbDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<constructor-arg>
<props>
<prop key="dataSource.url">${hsqldb.url}</prop>
<prop key="dataSource.user">${user}</prop>
<prop key="dataSource.password">${password}</prop>
</props>
</constructor-arg>
<property name="dataSourceClassName" value="org.hsqldb.jdbc.JDBCDataSource" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="maximumPoolSize" value="5" />
<property name="minimumPoolSize" value="1" />
</bean>
</constructor-arg>
</bean>
<!-- execute a script to create tables after creation of bean for in-memory HSQLDB -->
<jdbc:embedded-database id="hsqldbDataSource" type="HSQL">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
<bean id="daoManager" class="com.d.DAOManager" autowire="constructor">
<property name="dataSource" ref="hsqldbDataSource"/>
</bean>
</beans>
我的DAOManager课程:
public class DAOManager {
private DataSource dataSource;
private Connection connection;
@Autowired
public DAOManager(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
在这里我调用get我的daoManager bean。
public static DAOManager createHSQLDBDAOManager() {
LOG.info("Setting up datasource to in-memory HSQLDB");
ConfigurableEnvironment env = (ConfigurableEnvironment)applicationContext.getEnvironment();
env.setActiveProfiles("test");
applicationContext.load("classpath:/applicationContext.xml");
applicationContext.refresh();
DAOManager daoManager = applicationContext.getBean("daoManager", DAOManager.class);
return daoManager;
}
如果我删除了setter方法,为什么会抱怨?我不需要它。如果我在构造函数之前移除了@Autowired(它也可以),它只是无用而且不使用by构造函数autowire函数。
答案 0 :(得分:1)
需要删除daoManager bean的属性值
<bean id="daoManager" class="com.d.DAOManager" autowire="constructor"/>