jetty 配置数据源以及应用

来源:互联网 发布:excel2013破解软件 编辑:程序博客网 时间:2024/06/05 15:29

1、pom.xml:


    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-jdbc</artifactId>
       <version>${spring.version}</version>
    </dependency>

    <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.14</version>
     </dependency>


     <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.19.v20160908</version>
            <configuration>
                 <jettyXml>${basedir}/jetty.xml</jettyXml>
                 <webApp>
                     <jettyEnvXml>${basedir}/jetty-env.xml</jettyEnvXml>     <!--这个一定要有,这里面配的是数据源-->
                 </webApp>

                 <httpConnector>
                     <port>9080</port>
                 </httpConnector>
            </configuration>
        </plugin>


2、jetty.xml:


<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">


<!-- =============================================================== -->
<!-- Configure the Jetty Server -->
<!-- -->
<!-- Documentation of this file format can be found at: -->
<!-- http://wiki.eclipse.org/Jetty/Reference/jetty.xml_syntax -->
<!-- -->
<!-- Additional configuration files are available in $JETTY_HOME/etc -->
<!-- and can be mixed in. For example: -->
<!-- java -jar start.jar etc/jetty-ssl.xml -->
<!-- -->
<!-- See start.ini file for the default configuraton files -->
<!-- =============================================================== -->




<Configure id="Server" class="org.eclipse.jetty.server.Server">
    <!-- =========================================================== -->
    <!-- Set handler Collection Structure -->
    <!-- =========================================================== -->
    <Set name="handler">
        <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
            <Set name="handlers">
                <Array type="org.eclipse.jetty.server.Handler">
                    <Item>
                        <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
                    </Item>
                    <Item>
                        <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
                    </Item>
                </Array>
            </Set>
        </New>
    </Set>
</Configure>


3、jetty-env.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">


<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="semanticDB" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg>java:jboss/jdbc/ps_admin</Arg>    <!--这个是连接下面的数据源的标识-->
        <Arg>
            <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
                <Set name="Url">jdbc:mysql://101.27.19.191:3306/ps_admin</Set>
                <Set name="User">***</Set>
                <Set name="Password">****</Set>
            </New>
        </Arg>
    </New>
</Configure>


4、web.xml:

<init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/springMVC-servlet.xml 
             /WEB-INF/config/authority_applicationContext.xml</param-value>
        </init-param>


5、authority_applicationContext.xml:


<jee:jndi-lookup id="csdataSource" jndi-name="java:jboss/jdbc/ps_admin" />  <!--对应数据源配置中的标识-->
    <bean id="urlJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="csdataSource" />
    </bean>


6、应用:

   @Autowired
    private JdbcTemplate jdbcTemplate;