一、Spring Boot 2.0版本使用JNDI
- 引入pom指定外部配置文件路径:
<profile>
<id>product</id>
<properties>
<logback.loglevel>INFO</logback.loglevel>
<spring.profiles.active>product</spring.profiles.active>
<profile.product>true</profile.product>
<profile.properties>file:/tomcat/profiles/${project.artifactId}/properties</profile.properties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<argLine>-Xms512m -Xmx1024m</argLine>
<forkMode>once</forkMode>
<skip>true</skip>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
根据引入的pom可知 外部配置文件路径为 /tomcat/profiles/${project.artifactId}/properties
- 配置数据源信息并放置到指定路径下
spring:
datasource:
jndi-name: jdbc/lcs_ds
- 修改tomcat配置文件
修改server.xml加入数据源配置
<Resource name="jdbc/lcs_ds"
auth="Container"
type="javax.sql.DataSource"
username="xxxx"
password="xxxx"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://xxxx:3306/lcs?useUnicode=true&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true"
maxActive="100"
maxIdle="10"
maxWait="2000"
removeAbandoned="true"
logAbandoned="true"
logExpiredConnections="true"
maxConnLifetimeMillis="60000"
initialSize="1"
removeAbandonedTimeout="180"
validationQuery="select 1"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="3600000"
minEvictableIdleTimeMillis="18000000" />
修改context.xml引入数据源配置
<ResourceLink global="jdbc/lcs_ds" name="jdbc/lcs_ds" type="javax.sql.DataSource"/>
也可直接将数据源信息配置到context.xml中
项目打指定profile war包,修改数据源信息可直接在外部配置文件中修改 不用重新打包
二、Spring Boot老版本怎么玩?
在比较老的Spring Boot中是怎么玩的,大体的思路是:
(1)注入TomcatFactory工厂类,获取到上下文Context,往上下文中设置resource对象。
(2)注入jndi DataSource。
具体代码如下
@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", "com.mysql.jdbc.Driver");
resource.setProperty("url", "jdbc:mysql://localhost:3306/mydb");
resource.setProperty("username", "root");
resource.setProperty("password","root");
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();
}