Springboot use tomcat JNDI
Springboot use tomcat JNDI
[use database pool : dbcp Druid bonecp C3P0 proxool]
[1]apache-tomcat-9.0.0.M9\conf\context.xml
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- dbcp
<Resource name="jdbc/pg"
auth="Container"
factory="org.apache.commons.dbcp.dbcp2.BasicDataSourceFactory"
type="javax.sql.DataSource"
username="postgres"
password="admin"
url="jdbc:postgresql://localhost/ABC"
driverClassName="org.postgresql.Driver"
initialSize="20"
maxWaitMillis="15000"
maxTotal="75"
maxIdle="20"
maxAge="7200000"
testOnBorrow="true"
validationQuery="select 1"
/>
--> <!-- Druid put druid-1.1.10.jar to tomcat lib dir
<Resource name="jdbc/pg"
auth="Container"
factory="com.alibaba.druid.pool.DruidDataSourceFactory"
type="javax.sql.DataSource"
username="postgres"
password="admin"
url="jdbc:postgresql://localhost/ABC"
driverClassName="org.postgresql.Driver"
maxActive="50"
maxWait="10000"
removeabandoned="true"
removeabandonedtimeout="60"
logabandoned="false"
filters="stat"
/>
--> <!--bonecp put
guava-25.1-jre.jar slf4j-api-1.7.25.jar bonecp-0.8.0.RELEASE.jar
to tomcat lib dir <Resource
name="jdbc/pg"
auth="Container"
type="com.jolbox.bonecp.BoneCPDataSource"
factory="org.apache.naming.factory.BeanFactory"
driverClass="org.postgresql.Driver"
username="postgres"
password="admin"
jdbcUrl="jdbc:postgresql://localhost/ABC"
idleConnectionTestPeriod="0"
idleMaxAge="10"
partitionCount="1"
maxConnectionsPerPartition="5"
minConnectionsPerPartition="1"
connectionTestStatement=""
initSQL="select 1"
/>
-->
<!-- C3P0 put c3p0.jar -java.jar to tomcat lib dir <Resource name="jdbc/pg"
auth="Container"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
driverClass="org.postgresql.Driver"
jdbcUrl="jdbc:postgresql://localhost/ABC"
user="postgres"
password="admin"/>
--> <!-- proxool put com.springsource.org.logicalcobwebs.proxool-0.9.1.jar、proxool-cglib.jar、commons-logging-xxx.jar to tomcat lib dir --> <Resource name="jdbc/pg"
auth="Container"
type="javax.sql.DataSource"
factory="org.logicalcobwebs.proxool.ProxoolDataSource"
proxool.alias="test"
user="postgres"
password="admin"
delegateProperties="foo=1,bar=true"
proxool.jndi-name="postgrejndi"
proxool.driver-url="jdbc:postgresql://localhost/ABC"
proxool.driver-class="org.postgresql.Driver"
proxool.house-keeping-sleep-time="900000"
proxool.maximum-active-time="5"
proxool.prototype-count="3"
proxool.statistics="1m,15m,1d"
proxool.simultaneous-build-throttle="10"
proxool.minimum-connection-count="5"
proxool.maximum-connection-count="15"
/> </Context>
[2] springboot 建立Application.java
package demo; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
private static Class<Application> applicationClass = Application.class; @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
} @RestController
class GreetingController { @RequestMapping("/hello/{name}")
String hello(@PathVariable String name) { StringBuffer sb=new StringBuffer();
Connection c=GreetingController.getConnection();
String sqlr="select * from company";
PreparedStatement ps=null; try {
ps = c.prepareStatement(sqlr);
ResultSet rs= ps.executeQuery();
sb.append("<table style='border-width:1px;border-color:#666666;color:#f4f4f4'>"); while(rs.next()){
System.out.println(rs.getString()+"~~~~"+rs.getString()+"~~~~"+rs.getString()+"~~~~"+rs.getString()+"~~~~"+rs.getString());
sb.append("<tr>") .append("<td>").append(rs.getString())
.append("</td>")
.append("<td>").append(rs.getString())
.append("</td>")
.append("<td>").append(rs.getString())
.append("</td>")
.append("<td>").append(rs.getString())
.append("</td>")
.append("<td>").append(rs.getString())
.append("</td>") .append("</tr>");
}
sb.append("</table>"); } catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close();
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return "Hello, " + name + "!"+"<br>"+sb.toString();
} public static Connection getConnection(){
Connection conn = null;
Context initCtx = null;
try {
initCtx = (Context) new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/pg");
conn=ds.getConnection();
System.out.println("获取数据库连接成功!");
} catch (NamingException e) {
System.out.println("命名空间连接失败!");
e.printStackTrace();
}catch (SQLException e){
System.out.println("SQL异常!");
e.printStackTrace();
}
return conn;
}
}
[3]pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.</modelVersion> <artifactId>spring-boot-web-thymeleaf</artifactId>
<packaging>war</packaging>
<name>Spring Boot Web Thymeleaf Example</name>
<description>Spring Boot Web Thymeleaf Example</description>
<url>https://www.mkyong.com</url>
<version>1.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4..RELEASE</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> <!-- Optional, for bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.</version>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>1.1..RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
[4] mark error
javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSourceFactory]
The name of the default factory changed between Tomcat 7 and Tomcat 8. A couple of attribute names also changed.
Please switching from DBCP 1.x to DBCP 2.x in Tomcat 8.
factory="org.apache.tomcat.dbcp.BasicDataSourceFactory" Tomcat7 OR early
factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory" ------>Tomcat8 OR 9
Springboot use tomcat JNDI的更多相关文章
- Linux - tomcat -jndi数据源配置
Linux - tomcat -jndi数据源配置 tomcat/conf/context .xml 文件中修改如下 <Resource name="/jdbc/--" au ...
- SpringBoot切换Tomcat容器,SpringBoot使用Jetty容器
SpringBoot切换Tomcat容器, SpringBoot修改为Jetty容器, SpringBoot使用undertow容器, SpringBoot使用Jetty容器 ============ ...
- SpringBoot系列六:SpringBoot整合Tomcat
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Tomcat 2.背景 SpringBoot 本身支持有两类的 WEB 容器:默认的 To ...
- Tomcat JNDI + spring配置
http://hi.baidu.com/lzpsky/item/f9a727ba823257eb4ec7fd27 一.简介 JNDI : Java Naming and Directory Inter ...
- 【MyBatis】MyBatis Tomcat JNDI原理及源码分析
一. Tomcat JNDI JNDI(java nameing and drectory interface),是一组在Java应用中访问命名和服务的API,所谓命名服务,即将对象和名称联系起来,使 ...
- SpringBoot 配置 Tomcat SSL
SpringBoot 配置 Tomcat SSL SSL(Secure Sockets Layer , 安全套接层)是为网络通信提供安全及数据完整性的一种安全协议,SSL在网络传输层对网络连接进行加密 ...
- Springboot以Tomcat为容器实现http重定向到https的两种方式
1 简介 本文将介绍在Springboot中如何通过代码实现Http到Https的重定向,本文仅讲解Tomcat作为容器的情况,其它容器将在以后一一道来. 建议阅读之前的相关文章: (1) Sprin ...
- 基于Tomcat + JNDI + ActiveMQ实现JMS的点对点消息传送
前言 写了一个简单的JMS例子,之所以使用JNDI 是出于通用性考虑,该例子使用JMS规范提供的通用接口,没有使用具体JMS提供者的接口,这样可以保证我们编写的程序适用于任何一种JMS实现(Activ ...
- Tomcat - JNDI 配置
1. Create Your JavaBean Class Create the JavaBean class which will be instantiated each time that th ...
随机推荐
- SPL类
用途:对类,方法,属性,参数的提取生成文档:自动加载插件 实列化类同于new:$ref = new ReflectionClass($classname);$class = $ref->newI ...
- Algorithms算法题<1.1>
1.1.27 二项分布.估计用一下代码计算binomial(100,50,0.25)将会产生的递归调用次数: public static double binomial(int N,int k,dou ...
- 03--(二)编程实践中C语言的一些常见细节
编程实践中C语言的一些常见细节(转载) 对于C语言,不同的编译器采用了不同的实现,并且在不同平台上表现也不同.脱离具体环境探讨C的细节行为是没有意义的,以下是我所使用的环境,大部分内容都经过测试,且所 ...
- Swift语法3.03(类型Types)
类型 在Swift中,有两种类型:命名型类型和复合型类型.命名型类型是在定义时可以给定的特定名字的类型.命名型类型包括类,结构体,枚举和协议.例如,自定义的类MyClass的实例拥有类型MyClass ...
- spring boot (一)
spring boot 启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIM ...
- centos7 redmine安装过程(转载)
redmine 部署过程 redmin官方文档写的太烂加上不熟悉ruby搞了半天,回到家后觉得还是记录下好,希望可以帮助有需要的人,少走弯路. 版本说明 下面的版本很重要redmine 版本 3.3 ...
- 路飞学城Python-Day78
17-静态文件配置1 静态的文件的配置不能直接将CSS.JS文件直接放在templates的文件夹中 要将所有的静态文件放在static的文件夹中,然后配置上静态文件static的路径 要想访问Dja ...
- Tab 切换效果
今天写的两个小效果都是为了测试我写的单参函数,结果还是有点成功的~~此处是不是想发表情包! Tab效果很简单,这里我就不赘述了,直接上代码: html代码: <div class="c ...
- HDU1058 - Humble Numbers
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...
- [剑指offer] 29. 顺时针打印矩阵 (for循环条件)
思路: 先定义左上和右下角点坐标,打印可分为从左到右,从上到下,从右到左,从下到上.依次判断最后一圈的四个循环条件. #include "../stdafx.h" #include ...