Spring Download still requires a resource reference in web.xml

Take a look at Spring Download now and want to do it right using the Java configuration and ultimately without web.xml . So, the tricky part is that a classic WAR file is required for the production environment.

Therefore, I specified a WAR for packaging in my Maven pom.xml file, and the main class is Application extends SpringBootServletInitializer .

It works well. Now the tricky part is that in a production environment, Datasource provided through JNDI . In a classic Spring application, you should reference this dependency in web.xml using resource-ref as follows:

  <resource-ref> <res-ref-name>jdbc/DefaultDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> </resource-ref> 

All the studies carried out seem to indicate that I can get rid of web.xml and replace it with the corresponding context.xml file (in the META-INF folder):

  <Resource name="jdbc/DefaultDB" auth="Container" type="javax.sql.DataSource" factory="com.sap.jpaas.service.persistence.core.JNDIDataSourceFactory"/> 

Unfortunately this does not work: /

Interestingly, however, the regular servlet3 web application works just fine, see [ https://github.com/steinermatt/servlet3-sample] .

So, I am tempted to believe that the reason it does not work for the Spring application to boot is due to the w760> Boot Boot Boot ... boot process, so, really looking for any hints, suggestions for what it might be!

Any help is appreciated!

+7
spring spring-boot
source share
1 answer

By default, JNDI is disabled in the integrated Tomcat.

You can use the following code to enable JNDI in tomcat. The following code will help you initialize the DataSource spring bean.

  @Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { tomcat.enableNaming(); return super.getTomcatEmbeddedServletContainer(tomcat); } @Override protected void postProcessContext(Context context) { ContextResource resource = new ContextResource(); resource.setName("jdbc/myDataSource"); resource.setType(DataSource.class.getName()); resource.setProperty("driverClassName", "your.db.Driver"); resource.setProperty("url", "jdbc:yourDb"); context.getNamingResources().addResource(resource); } }; } 

You can use the DataSource bean in your controller using automatic wiring.

 @Autowired private DataSource dataSource; 
0
source share

All Articles