Spring将autowired对象传递给类构造函数

时间:2015-03-12 14:39:39

标签: java spring spring-mvc autowired spring-bean

我的SpringMVC应用程序出现问题

我用@Component注释了类,其中2个字段用@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));
}

}

创建此组件时出现以下错误:

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
你可以帮我解决这个问题吗?我尝试添加依赖属性但不能正常工作。

其余-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 个答案:

答案 0 :(得分:3)

在通过构造函数创建和初始化对象之后,Spring才能自动对字段进行自动装配。

当你做的时候

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

在构造函数中,Spring无法自动连接这两个字段。

使用@PostConstruct init方法或使用构造函数注入将数据源注入构造函数。