项目抛弃Tomcat容器,用代码启动Tomcat插件

Posted 花语苑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目抛弃Tomcat容器,用代码启动Tomcat插件相关的知识,希望对你有一定的参考价值。

tomato启动代码如下:

package tomcat;

import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

/**
 * The Class StartMainTomcat.
 * 
 * @author nibili
 */
public class StartTomcat {

	/** The Constant PORT. */
	public static final int PORT = 80;

	/** The Constant CONTEXT. */
	public static final String CONTEXT = "";

	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 * @throws Exception
	 *             the exception
	 */
	public static void main(String[] args) throws Exception {
		System.setProperty("catalina.base", System.getProperty("user.dir") + "/target");
		System.setProperty("log.sql.port", "80");
		Tomcat server = new Tomcat();
		server.setBaseDir(System.getProperty("catalina.base"));
		server.setPort(PORT);
		server.addWebapp(CONTEXT, System.getProperty("user.dir") + "/src/main/webapp");
		Connector connector = server.getConnector();
		connector.setURIEncoding("UTF-8");
		server.start();
		System.out.println("Hit Enter in console to stop server");
		if (System.in.read() != 0) {
			server.stop();
			System.out.println("Server stopped");
		}
	}
}

  

需要的maven的jar包如下:

<?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.0</modelVersion>
    <groupId>cn.com.easy</groupId>
    <artifactId>company-yyg-web-front</artifactId>
    <packaging>war</packaging>
    <version>0.1.0</version>
    <name>yyg-web-front</name>
    <repositories>
        <repository>
            <id>public</id>
            <url>http://120.76.29.54:8080/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
        <repository>
            <id>central</id>
            <name>Maven Central Repository</name>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
        <repository>
            <id>file-repo</id>
            <name>Local file Repository</name>
            <url>file://${project.basedir}/lib</url>
            <releases>
                <enabled>true</enabled>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <checksumPolicy>ignore</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>

    <properties>
        <!-- 主要依赖库的版本定义 -->
        <spring.version>4.0.9.RELEASE</spring.version>
        <tomcat.version>7.0.55</tomcat.version>
        <slf4j.version>1.7.7</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <hibernate.version>4.3.6.Final</hibernate.version>
        <httpclient.version>4.3.5</httpclient.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- 微信开发包 -->
        <dependency>
            <groupId>com.github.sd4324530</groupId>
            <artifactId>fastweixin</artifactId>
            <version>1.3.10</version>
        </dependency>
        <!-- 开源工具包 -->
        <dependency>
            <groupId>cn.com.easy</groupId>
            <artifactId>easy-commons</artifactId>
            <version>0.1.0-SNAPSHOT</version>
        </dependency>
        <!-- 验证码 -->
        <dependency>
            <groupId>com.github.cage</groupId>
            <artifactId>cage</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- mysql 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</version>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
        <!-- 阿里短信接口 -->
        <dependency>
            <groupId>ali</groupId>
            <artifactId>sdk</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- assembly -->
        <dependency>
            <groupId>tanukisoft</groupId>
            <artifactId>jsw</artifactId>
            <version>3.5.4</version>
            <scope>provided</scope>
            <type>zip</type>
        </dependency>
        <!-- qq接口 -->
        <dependency>
            <groupId>qq</groupId>
            <artifactId>Sdk4j</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- spring start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.6.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!-- spring end -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.12</version>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- mybatis end -->
        <!-- utils -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</以上是关于项目抛弃Tomcat容器,用代码启动Tomcat插件的主要内容,如果未能解决你的问题,请参考以下文章

jvm容器的关系

springboot如何使用外部tomcat容器

如何使用 Java 启动和停止 Tomcat 容器?

Tomcat 7“严重:子容器在启动期间失败”

Tomcat是咋启动SpringMVC的?

在java中怎么能让tomcat启动完后,spring配置文件加载完后自动运行某个类?