Spring passsing autowired object to class constructor

亡梦爱人 提交于 2019-12-11 11:47:32

问题


I got problem with my SpringMVC application

I have class annotated with @Component with 2 fields annotated with @Autowired

@Component
public class Crud {

private final Logger logger = LoggerFactory.getLogger(Crud.class);
private final Map<String, Database> dataSources = new HashMap<>();

@Autowired
private DataSource clientDataSource;    
@Autowired
private DataSource adminDataSource;

public Crud(){                                                                 
    dataSources.put("client", new Database(clientDataSource));
    dataSources.put("admin", new Database(adminDataSource));
}

}

when this component is created i got following error:

Error creating bean with name 'dbcrud' defined in ServletContext resource [/WEB-INF/rest-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.test.db.Crud]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required

can u help me out with this please? I tried add depends-on property but dont work as well.

rest-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:task="http://www.springframework.org/schema/task"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc          http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/task 
            http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<context:annotation-config />    

<mvc:annotation-driven />

<task:annotation-driven />    

<context:component-scan base-package="com.test" />

<bean id="clientDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost:5432/gymtracker"/>
    <property name="username" value="sa"/>
    <property name="password" value="sp"/>
</bean>
<bean id="adminDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost:5432/gymtracker"/>
    <property name="username" value="sa"/>
    <property name="password" value="sp"/>
</bean>    

<bean id="dbcrud" class="com.test.db.Crud">
    <property name="clientDataSource" ref="clientDataSource"/>
    <property name="adminDataSource" ref="adminDataSource"/>
</bean>

Database.java

public class Database {

private final Logger logger = LoggerFactory.getLogger(Database.class);
private final JdbcTemplate jdbc;

public Database(DataSource source) {
    this.jdbc = new JdbcTemplate(source);
}

public boolean insert(String table, Record record) throws DatabaseException {
    try {
        SqlBulider query = new SqlBulider();
        query.insertInto(table).columns(record.columns());

        Record.Content content = record.content();
        int queryResult = jdbc.update(query.sql(), content.values, content.types);

        return queryResult > 0;
    } catch (DataAccessException e) {            
        throw new DatabaseException("cannot preform insert on " + table    + " table", e);
    }
}

public void execute(String sql) throws DatabaseException {
    jdbc.execute(sql);
}

}

回答1:


Spring can only ever autowire a field after an object has been created and initialized through a constructor.

By the time you do

public Crud(){                                                                 
    dataSources.put("client", new Database(clientDataSource));
    dataSources.put("admin", new Database(adminDataSource));
}

in the constructor, it is impossible for Spring to have autowired the two fields.

Either use a @PostConstruct init method or use constructor injection to inject the data sources into the constructor.



来源:https://stackoverflow.com/questions/29012714/spring-passsing-autowired-object-to-class-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!