项目需求
- 本地环境使用embedded tomcat并配置数据源(jndi)
- 生产环境使用weblogic服务器,war包部署,使用jndi数据源
实现方式
- spring boot配置jndi
#spring.datasource.jndi-name=java:comp/env/jdbc/orcl
spring.datasource.jndi-name=jdbc/orcl
- 本地使用embedded tomcat,需要修改内核,配置jndi
- 配置application-dev.properties
spring.datasource.jndi-name=jdbc/orcl
# DEV
data-source.jndiName=jdbc/orcl
data-source.factory=org.apache.tomcat.jdbc.pool.DataSourceFactory
data-source.driverClassName=oracle.jdbc.driver.OracleDriver
data-source.url=jdbc:oracle:thin:@192.168.11.245:1521/Base
data-source.username=U_JSP
data-source.pwd=U_JSP
data-source.maxActive=100
data-source.maxIdle=30
data-source.maxWait=10000
- 配置PropConfig类,接受配置
@ConfigurationProperties(ignoreUnknownFields = false,prefix = "data-source")
public class DataSourceConfig {
private String jndiName;
private String factory;
private String driverClassName;
private String url;
private String username;
private String pwd;
private String maxActive;
private String maxIdle;
private String maxWait;
public String getJndiName() {
return jndiName;
}
public void setJndiName(String jndiName) {
this.jndiName = jndiName;
}
public String getFactory() {
return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getMaxActive() {
return maxActive;
}
public void setMaxActive(String maxActive) {
this.maxActive = maxActive;
}
public String getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(String maxIdle) {
this.maxIdle = maxIdle;
}
public String getMaxWait() {
return maxWait;
}
public void setMaxWait(String maxWait) {
this.maxWait = maxWait;
}
}
- 配置ServletWebServerFactory
@Configuration
public class JNDIConfig {
@Resource
private DataSourceConfig dataSourceConfig;
@ConditionalOnProperty(name = "spring.profiles.active", havingValue = "dev")
@Bean
public ServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory(){
@Override
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
// 启用默认禁用的JNDI命名
tomcat.enableNaming();
return super.getTomcatWebServer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setType(DataSource.class.getName());
resource.setName(dataSourceConfig.getJndiName());
resource.setProperty("factory", dataSourceConfig.getFactory());
resource.setProperty("driverClassName", dataSourceConfig.getDriverClassName());
resource.setProperty("url", dataSourceConfig.getUrl());
resource.setProperty("username", dataSourceConfig.getUsername());
resource.setProperty("password", dataSourceConfig.getPwd());
// 连接池配置,和测试、生产配置一致。
resource.setProperty("maxActive", dataSourceConfig.getMaxActive()); // 最大连接数
resource.setProperty("maxIdle", dataSourceConfig.getMaxIdle()); // 空闲连接数
resource.setProperty("maxWait", dataSourceConfig.getMaxWait()); // 最大等待时间
context.getNamingResources().addResource(resource);
super.postProcessContext(context);
}
};
return tomcatServletWebServerFactory;
}
}
- 生成环境
application-prod.properties
配置
spring.datasource.jndi-name=jdbc/orcl
- 问题:
- 直接使用mvn package报错
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
原因分析:
jndi必须挂载在服务器上才能使用,所以mvn package test阶段报错解决方法
先不配置jndi,打包完成后再使用jndi配置