问题描述
我正在尝试使用 Spring 4 和数据源配置连接到数据库。 我正在学习教程,所以这是正确的配置:
package spittr.config;
import ...
@Configuration
public class DataConfig {
@Bean
public Jndiobjectfactorybean dataSource() {
Jndiobjectfactorybean jndiobjectFB = new Jndiobjectfactorybean();
jndiobjectFB.setJndiName("java:jboss/datasources/jdbc/SpitterDS");
jndiobjectFB.setProxyInterface(javax.sql.DataSource.class);
return jndiobjectFB;
}
@Bean
public Jdbcoperations jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
一切正常,但如您所见,我返回的是 Jndiobjectfactorybean 而不是数据源。如果我很了解 Spring(可能我不了解,否则我会在这里理解),如果您不指定 Bean 名称,Spring 会将 Bean 的名称设置为返回类型,首字母为小写。 例如,以下代码行将返回一个 id 为“myFantasticBean”的 bean(带有“m”小写)
@Bean
public MyFantasticBean createMyBean() {
return new MyFantasticBean();
}
我在网上看到很多人在使用这个版本的 DataConfig,其中方法 dataSource() 返回一个 DataSource 类型的对象(应该是这样):
package spittr.config;
import ...
@Configuration
public class DataConfig {
@Bean
public DataSource dataSource() {
Jndiobjectfactorybean jndiobjectFB = new Jndiobjectfactorybean();
jndiobjectFB.setJndiName("java:jboss/datasources/jdbc/SpitterDS");
jndiobjectFB.setProxyInterface(javax.sql.DataSource.class);
return (DataSource) jndiobjectFB.getobject();
}
@Bean
public Jdbcoperations jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
但是如果我使用这个 DataSource 创建,我会收到以下错误:
bin/content/10_SpringWeb_BE_JDBC.war/WEB-INF/classes/spittr/data/JdbcSpitterRepository.class"]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in class path resource [spittr/config/DataConfig.class]: Bean instantia
tion via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jdbc.core.Jdbcoperations]: Factory method 'jdbcTemplate' threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required
我什至找到了解决方案,修改了 dataSource() 方法,现在变成这样:
package spittr.config;
import ...
@Configuration
public class DataConfig {
@Bean
public DataSource dataSource(){
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
DataSource dataSource = dsLookup.getDataSource("java:jboss/datasources/jdbc/SpitterDS");
return dataSource;
}
@Bean
public Jdbcoperations jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
但我真的不明白为什么这有效而前一个无效。有人可以向我解释我做错了什么吗?
非常感谢
解决方法
Spring 按类型连接所有 bean。这意味着当使用 java 配置时,它将寻找返回类型为 DataSource
的 bean 定义。这就是您在使用 JndiDataSourceLookup 时所拥有的,因此 Spring 可以毫无问题地找到它。
它不能从 JndiObjectFactoryBean 派生数据源,除非您在返回它时对其进行转换。
@Bean
public DataSource dataSource() {
JndiObjectFactoryBean jndiObjectFB = new JndiObjectFactoryBean();
jndiObjectFB.setJndiName("java:jboss/datasources/jdbc/SpitterDS");
jndiObjectFB.setProxyInterface(javax.sql.DataSource.class);
return (DataSource) jndiObjectFB.getObject();
}
另外,你说的有点误会
如果我了解 Spring(可能我不了解,否则我会 这里已经明白了),如果不指定Bean名称,Spring会 将 bean 的名称设置为第一个返回类型 字母为小写。例如,以下代码行将 返回一个 id 为“myFantasticBean”的 bean(“m”小写)
这仅在使用组件扫描时成立。
所以如果你有一个用例如注释的类@Service 本身:
@Service
public class MyFantasticBean { .. }
那么是的,名称将是 myFantasticBean,因为该名称不能从其他名称派生而来。 重要的是要意识到使用 Java 配置时名称不会与类型相同。
@Bean
public MyFantasticBean createMyBean() {
return new MyFantasticBean();
}
在您的示例中,MyFantasticBean 是 spring 将在您想要注入它时搜索的 bean 类型。而 createMyBean
将是 bean 的名称。因此,如果您有多个类型为 MyFantasticBean 的 bean 实例,则可以使用 bean 名称指定要注入的 bean。在你的情况下
@Qualifier("createMyBean")
@Autowired
private MyFantasticBean myFantasticBean;