我正在转向基于Spring 3.x代码的@Configuration风格。到目前为止,我做得很好,但是当我尝试添加@Profile(“production”)来选择JNDI数据源而不是C3p0连接池@Profile(“standalone”)时,我得到了:
Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?
...
当然,有很多细节(我在下面提供了一些参考资料),但我的问题是:有人会发布/指向一个有效的例子吗?
谢谢,
详细说明:
修改:已解决
我将个人资料移到了正确的位置。现在他们在同一个文件和同一个项目中:
...
other bean definitions
/**
* Stand-alone mode of operation.
*/
@Configuration
@Profile("standalone")
static class StandaloneProfile {
@Autowired
private Environment env;
/**
* Data source.
*/
@Bean
public DataSource dataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(env.getRequiredProperty("helianto.jdbc.driverClassName"));
ds.setJdbcUrl(env.getRequiredProperty("helianto.jdbc.url"));
ds.setUser(env.getRequiredProperty("helianto.jdbc.username"));
ds.setPassword(env.getRequiredProperty("helianto.jdbc.password"));
ds.setAcquireIncrement(5);
ds.setIdleConnectionTestPeriod(60);
ds.setMaxPoolSize(100);
ds.setMaxStatements(50);
ds.setMinPoolSize(10);
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/**
* Production mode of operation.
*/
@Configuration
@Profile("production")
static class ProductionProfile {
@Autowired
private Environment env;
/**
* JNDI data source.
*/
@Bean
public DataSource dataSource() {
try {
JndiObjectFactoryBean jndiFactory = new JndiObjectFactoryBean();
jndiFactory.setJndiName("java:comp/env/jdbc/heliantoDB");
jndiFactory.afterPropertiesSet(); //edited: do not forget this!
return (DataSource) jndiFactory.getObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}