环境部分说明:
已引入commons-collections4-4.1.jar、commons-dbcp2-2.8.0.jar、commons-pool2-2.9.0.jar
配置jndi代码说明:
@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");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "oracle.jdbc.OracleDriver");
resource.setProperty("url", "jdbc:oracle:thin:@127.0.0.1:1521:ORCL");
resource.setProperty("username", "TEST");
resource.setProperty("password","TESTword1");
context.getNamingResources().addResource(resource);
}
};
}
@Bean
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("java:comp/env/jdbc/mydb");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource)bean.getObject();
}
报错情况:
Could not create resource factory instance [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory]