Question

Here is my Spring xml config:

    <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>

And my DAOManager class:

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;
}

And here I call get my 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;
}

Why is it complaining if I remove the setter method? I don't need it. If I remove the @Autowired before the constructor (it also works) it's just useless and not using the by constructor autowire function.

Was it helpful?

Solution

Needed to remove the property value of the daoManager bean

<bean id="daoManager" class="com.d.DAOManager" autowire="constructor"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top