Java tutorial
/* * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package sample.tomcat.jndi; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.catalina.Context; import org.apache.catalina.startup.Tomcat; import org.apache.tomcat.util.descriptor.web.ContextResource; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.jndi.JndiObjectFactoryBean; @Configuration @ComponentScan @EnableAutoConfiguration @ImportResource("classpath:context/applicationContext.xml") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @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/CMSDataSource"); resource.setProperty("global", "jdbc/MYDataSource"); resource.setType("javax.sql.DataSource"); resource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver"); resource.setProperty("url", "jdbc:yourDb"); resource.setAuth("Container"); context.getNamingResources().addResource(resource); System.out .println("\n\n\ncontext naming resources\n" + context.getResourceOnlyServlets() + "\n\n\n"); } }; } @Bean(destroyMethod = "") public DataSource jndiDataSource() throws IllegalArgumentException, NamingException { System.out.println("\n\n\nIn jndiDataSource\n\n\n"); JndiObjectFactoryBean bean = new JndiObjectFactoryBean(); bean.setJndiName("jdbc/MYDataSource"); bean.setProxyInterface(DataSource.class); bean.setLookupOnStartup(false); bean.afterPropertiesSet(); return (DataSource) bean.getObject(); } }