I am getting the following exception when I try to use DataSource created in web logic and trying to use in my application:
weblogic.application.ModuleException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140) at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124) at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:233) at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:228) at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45) at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:78) at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:752) at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45) at weblogic.a pplication.internal.B
Following is the java code:
@Configuration
@ConfigurationProperties("my.oracle")
public class MyDBConfiguration {
@NotNull
private String jndiname;
public void setJndiname(String jndiname) {
this.jndiname = jndiname;
}
@Bean(name = "testA")
@Primary
public DataSource dataSource() throws SQLException{
DataSource dataSource;
try {
Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup(this.jndiname); // i can clearly see that this matches with the jndi name(jdbc/myOracleDataSource) created in weblogic.
return dataSource;
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
application.local.properties
my.oracle.jndiname=jdbc/myOracleDataSource
We have many spring boot apps deployed in weblogic that lookup datasources using jndi. Below is an example of how we do it. We explicitly specify to use weblogic.jndi.WLInitialContextFactory and also make sure the destroy method of your datasource beans is blank, otherwise undeploying your app can remove the datasource from weblogic.
@Configuration
public class DataSourceConfig {
@Autowired
private Environment env;
@Bean
public JndiTemplate jndiTemplate() {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
return new JndiTemplate(props);
}
// destroyMethod MUST be blank or datasource is removed from jndi tree on
// see https://github.com/spring-projects/spring-framework/issues/17153
@Bean(destroyMethod = "")
@Primary
public DataSource batchDataSource() throws NamingException {
return (DataSource) jndiTemplate().lookup(env.getProperty("dms-geo-sync.batch-datasource-jndi-name"));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.