Son zamanlarda spring boot ile ilgileniyorum ve yavastan yazılacak servisleri artık onunla gelistirme pesindeyim.

JNDI baglantısı kurarken bazı bilgilere ulastim ve bunları projeme entegre ettim.

public static void main(String[] args) {
SpringApplication.run(Main.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/mydb_xx”);
resource.setType(DataSource.class.getName());
resource.setProperty(“driverClassName”, “oracle.jdbc.driver.OracleDriver”);
resource.setProperty(“oracle.url”, “jdbc:oracle:thin:@xxx.xx.xx.xxx:xxxx:mySchema”);
resource.setProperty(“oracle.username”, “my_db”);
resource.setProperty(“oracle.password”, “my_db”);
context.getNamingResources().addResource(resource);
}
};
}

@Bean(destroyMethod = “”)
public static DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName(“java:comp/env/jdbc/my_db”);
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(true);
bean.afterPropertiesSet();
return (DataSource)bean.getObject();
}

Bunun calistirdigim zaman aldıgım hataların cogu asagida ki ile ilgili;

java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory

Burada 2 yol var . Biri application.properties dosyasına yazmak ya da Main sınfının icine.

Istedigim yapı spring web ‘ te olan applicationContext.xml’de olan dataSource’u DI ile ilgili sınıflara ulastırmak.