I am moving to new Spring 3.x code based @Configuration style. I have done good, so far, but when I tried to add a @Profile("production") to select a JNDI data source instead of C3p0 connection pool @Profile("standalone"), I've got:
Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?
...
Of course, there is a lot of details (I included some below for reference), but my question is: would someone kindly post/point to a working example?
Thanks,
Details:
- Same configuration, as XML, is working fine,
- web.xml points to a root context configured in AppConfig.class and to a servlet context configured in WebConfig.class,
- AppConfig has an @Import(SomeConfig.class) pulling a config from a service layer project - dependencies are wired by maven,
- The SomeConfig class has the @Profile("standalone") on the C3p0 datasource,
- The AppConfig class has the @Profile("production") on the JNDI datasource, web.xml defines spring.profiles.default=production.
Edit: Solved
I moved the profile to the right place. Now they are in the same file and in the same project:
...
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);
}
}
}