Spring 将自动装配对象传递给 class 构造函数
Spring passsing autowired object to class constructor
我的 SpringMVC 应用程序有问题
我有 class 用 @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);
}
}
Spring 只能在通过构造函数创建并初始化对象后自动装配字段。
到你做的时候
public Crud(){
dataSources.put("client", new Database(clientDataSource));
dataSources.put("admin", new Database(adminDataSource));
}
在构造函数中,Spring不可能自动装配这两个字段。
使用 @PostConstruct
init 方法或使用构造函数注入将数据源注入构造函数。
我的 SpringMVC 应用程序有问题
我有 class 用 @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);
}
}
Spring 只能在通过构造函数创建并初始化对象后自动装配字段。
到你做的时候
public Crud(){
dataSources.put("client", new Database(clientDataSource));
dataSources.put("admin", new Database(adminDataSource));
}
在构造函数中,Spring不可能自动装配这两个字段。
使用 @PostConstruct
init 方法或使用构造函数注入将数据源注入构造函数。