2

I'm trying to run an application on spring boot, but to do that I need to add some resources to tomcat, eg. data source configuration and others.

Normally i would add something ike

<Resources name="..." ....>

but how can i achieve that in spring boot?

zibi
  • 3,183
  • 3
  • 27
  • 47

2 Answers2

1

I think the following would work for you (I have successfully used a similar approach to customize some other aspect of embedded tomcat):

@Configuration
public class TomcatConfig implements EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if(container instanceof TomcatEmbeddedServletContainerFactory) {
            TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory = (TomcatEmbeddedServletContainerFactory) container;
            tomcatEmbeddedServletContainerFactory.addContextCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setNamingResources(.......);
            }
        });
        }
    }
}
geoand
  • 60,071
  • 24
  • 172
  • 190
  • But why bother doing that when you can configure a `DataSource` natively in Spring? I suppose maybe there are 3rd party libraries that assume JNDI is the only place to look for a database. – Dave Syer Apr 07 '14 at 17:45
  • @DaveSyer That's a very good question Dr. Sayer! The way Spring Boot can configure the Data Source so easily from property files makes JNDI seem redundant – geoand Apr 07 '14 at 18:09
  • 1
    Another similar reason for ex. we have spring 3 based spring security code which uses JNDI datasources. How can that be used in Spring Boot app with Embedded Tomcat? – Vineet Bhatia Sep 02 '14 at 13:06
  • I am not sure it this is entirely related but I want to start not just the Spring Application but also add another resource (a simple web client) to Tomcat on startup - is there a way to do this? Maybe you can [take a look at my question](http://stackoverflow.com/questions/42052345/how-to-start-springbootapplication-together-with-another-resource-web-client)? Thanks! BR; – Stefan Falk Feb 05 '17 at 13:14
0

There's a sample project in github that provides for resolving customizing Tomcat /setting resource property and they use datasource config as example. You can found the project application sample here.

And you can find the discussion here.

NullPointer
  • 412
  • 7
  • 17