[英]Spring - Java configuration @Bean parameter
I've just updated from Spring 3.1.1 to 3.2.6 我刚刚从Spring 3.1.1更新到了3.2.6
With 3.1 the following code worked well: 使用3.1,以下代码运行良好:
@Bean(name = DEMO_DS)
public JndiObjectFactoryBean demoDataSource()
{
JndiObjectFactoryBean factory = new JndiObjectFactoryBean();
factory.setJndiName(JDBC_DEMO_DS);
factory.setProxyInterface(DataSource.class);
return factory;
}
@Bean(name = DEMO_SESSION_FACTORY)
public SqlSessionFactoryBean demoSqlSessionFactory(@Qualifier(DEMO_DS) DataSource dataSource)
{
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setConfigLocation(new ClassPathResource("demo/config.xml"));
return sessionFactory;
}
However with the uprgraded version I get the following exception: 但是,对于升级版本,我得到以下异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[javax.sql.DataSource]的合格Bean作为依赖项:至少应有1个符合此依赖项自动候选条件的bean。 Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=DemoDataSource)}
依赖项注释:{@ org.springframework.beans.factory.annotation.Qualifier(value = DemoDataSource)}
I have multiple DataSources hence the @Qualifier is a need. 我有多个数据源,因此需要@Qualifier。
Thanks. 谢谢。
Edit: 编辑:
It seems that this solves the problem: 看来这解决了问题:
public DataSource dataSourceFactory() {
try
{
return (DataSource) demoDataSource().getObject();
}
catch (Exception ex)
{
throw new RuntimeException(ex);
}
}
...
sessionFactory.setDataSource(dataSourceFactory());
However I don't think it's a nice solution. 但是,我认为这不是一个很好的解决方案。
Depending on your need rewrite your configuration a little. 根据您的需要稍微重写您的配置。 If you don't really need the datasource injected you can do something like this.
如果您确实不需要注入数据源,则可以执行以下操作。
@Bean(name = DEMO_DS)
public JndiObjectFactoryBean demoDataSource() {
JndiObjectFactoryBean factory = new JndiObjectFactoryBean();
factory.setJndiName(JDBC_DEMO_DS);
factory.setProxyInterface(DataSource.class);
return factory;
}
@Bean(name = DEMO_SESSION_FACTORY)
public SqlSessionFactoryBean demoSqlSessionFactory() {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(demoDataSource().getObject());
sessionFactory.setConfigLocation(new ClassPathResource("demo/config.xml"));
return sessionFactory;
}
If you need to have a datasource injected you might want to switch to using a JndiLocatorDelegate
to do the lookup instead of a JndiObjectFactoryBean
. 如果需要注入数据源,则可能要切换为使用
JndiLocatorDelegate
而不是JndiObjectFactoryBean
进行查找。
@Bean(name = DEMO_DS)
public DataSource demoDataSource() throws NamingException {
return JndiLocatorDelegate.createDefaultResourceRefLocator().lookup(JDBC_DEMO_DS, DataSource.class);
}
This gives you a DataSource
directly instead of a FactoryBean<Object>
(which is what the JndiObjctFactoryBean
is) what probably is the source of the problem. 这直接为您提供了一个
DataSource
,而不是FactoryBean<Object>
(这就是JndiObjctFactoryBean
),这可能是问题的根源。
Or (in theory) you should also be able to use a @Value
annotation on a property in your config class. 或者(理论上),您还应该能够在配置类中的属性上使用
@Value
注释。 Instead of a @Value
a normal @Resource
should also do the trick (that can also delegate a call to JNDI for a lookup). 除了
@Value
,普通的@Resource
也可以解决问题(也可以将调用委派给JNDI进行查找)。
public class MyConfig {
@Value("${" + JDBC_DEMO_DS + "}")
private DataSource demoDs;
}
With @Resource
使用
@Resource
public class MyConfig {
@Resource(mappedName=JDBC_DEMO_DS)
private DataSource demoDs;
}
And you can then simply reference it in your configuration method. 然后,您可以在配置方法中简单地引用它。
@Bean(name = DEMO_SESSION_FACTORY)
public SqlSessionFactoryBean demoSqlSessionFactory() {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(demoDs);
sessionFactory.setConfigLocation(new ClassPathResource("demo/config.xml"));
return sessionFactory;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.