[英]NoSuchBeanDefinitionException: No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate
[英]No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate
当我尝试使用在 web 逻辑中创建的 DataSource 并尝试在我的应用程序中使用时,出现以下异常:
weblogic.application.ModuleException:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“javax.sql.DataSource”类型的合格bean:预计至少有1个符合自动装配候选资格的bean。 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) 在 weblogic.a 应用程序.internal.B
以下是 java 代码:
@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;
}
}
}
应用程序.local.properties
my.oracle.jndiname=jdbc/myOracleDataSource
我们在 weblogic 中部署了许多 spring 引导应用程序,这些应用程序使用 jndi 查找数据源。 下面是我们如何做的一个例子。 我们明确指定使用 weblogic.jndi.WLInitialContextFactory 并确保您的数据源 bean 的 destroy 方法为空白,否则取消部署您的应用程序可以从 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"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.