Spring将自动装配对象传递给类构造函数 [英] Spring passsing autowired object to class constructor

查看:130
本文介绍了Spring将自动装配对象传递给类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SpringMVC应用程序出现问题

I got problem with my SpringMVC application

我有一个用@Component注释的类,其中有2个用@Autowired注释的字段

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

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

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

}

推荐答案

Spring只能在创建对象并通过构造函数初始化后的后自动连接字段.

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

按您的时间

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

在构造函数中,Spring不可能自动连接两个字段.

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

要么使用@PostConstruct初始化方法,要么使用构造函数注入将数据源注入到构造函数中.

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

这篇关于Spring将自动装配对象传递给类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆