带有可选嵌入式 Tomcat 容器的 Spring Boot 中的 JNDI [英] JNDI in Spring Boot with optional Embedded Tomcat Container
问题描述
我对 Spring Boot 比较陌生,并且已经按照 如何在带有嵌入式Tomcat容器的Spring Boot中创建JNDI上下文
I am rather new to spring boot and have implemented a way of placing a datasource in JNDI context by following the advice given in How to create JNDI context in Spring Boot with Embedded Tomcat Container
我通过将这些 bean 添加到应用程序来使其工作,这使得命名并将数据源放置在 JNDI 上下文中,如下所示:
I got it to work by adding these beans to the application, which enable naming and place the datasource in JNDI-context like this:
@Autowired
Environment env;
public static void main(final String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory(){
@Override
public TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(final Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
public void postProcessContext(final Context context) {
final String dataSourceURL = env.getProperty("datasource.url");
// more properties
final ContextResource resource = new ContextResource();
resource.setType(DataSource.class.getName());
resource.setProperty("url", dataSourceURL);
// more properties
resource.setName("jdbc/myDB");
resource.setAuth("Container");
resource.setType("javax.sql.DataSource");
resource.setScope("Sharable");
resource.setProperty("factory", "org.apache.commons.dbcp.BasicDataSourceFactory");
context.getNamingResources().addResource(resource);
}
};
}
@Bean(destroyMethod = "")
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
final JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("java:comp/env/jdbc/myDB");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource)bean.getObject();
}
到目前为止一切顺利,它奏效了.但现在项目的规格已经改变.除了提供一个可启动嵌入式 Tomcat 的胖 JAR(按照最初的计划)之外,我还需要能够提供一个不引用嵌入式 Tomcat 的 WAR.因此,我创建了额外的项目来打包并通过 Maven 依赖项处理嵌入式 Tomcat 的可选包含.问题是,我必须将上面显示的代码从我的主类中移到我通过 Maven 依赖项包含的单独包中.现在它不再起作用了.
So far so good, it worked. But now the specification of the project has changed. In addition of delivering a fat JAR that starts up an embedded Tomcat (as initially planned), I need to be able to deliver a WAR that has no references to the embedded Tomcat. So i created extra projects for packaging and handled the optional inclusion of embedded Tomcat via Maven dependencies. Problem is, I had to move the code that is shown above out of my main class and into a seperate package which i include via Maven dependency. And now it no longer does the trick.
我承认我不太熟悉 @Autowired 和 @Bean 的 Spring-magic 是如何工作的,所以我在这里绊倒了.
I admit that I am not too familiar with how the Spring-magic with @Autowired and @Bean works, so I am kind off stumbling around here.
我需要做什么才能在我的主类之外创建 JNDI 上下文?
What do i have to do to make the creation of JNDI context work outside of my main class?
推荐答案
结果证明解决方案非常简单,我可以通过更多地了解 spring bean 来找到它.我创建了另一个类,在其中创建了 bean,使用 @Configuration
对其进行了注释,并将其添加到主类的 @ComponentScan
规范中.看起来像这样:
The solution turned out to be quite simple and i could find it by learning more about spring beans. I created another class in which the beans were created, annotated it with @Configuration
and added it to the @ComponentScan
specification of the main class. Looks like this:
@ComponentScan(basePackages = {"myBasePackage",
"externalPackageContainingConfiguration"})
externalPackageContainingConfiguration
仅在通过 Maven 依赖项添加工件时才能找到.有点老套,但可以解决问题.
externalPackageContainingConfiguration
is only found when it´s artifact is added via Maven Dependency. A little hacky, but does the trick.
这篇关于带有可选嵌入式 Tomcat 容器的 Spring Boot 中的 JNDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!