• guice整合struts2与jpa,guice的使用(九)


    传统我们开发一般使用ssh,但是有些微服务应用的项目我们不需要这么臃肿的框架做开发,于是采用了guice+struts2+guice作为框架组合进行了开发。

    先看我们项目引用的jar包:

    使用的时候一定要主要jar的版本问题.我项目在jdk1.7上面开发的

    然后看一下web.xml的配置:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
     3   <filter>
     4     <filter-name>guiceFilter</filter-name>
     5     <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
     6   </filter>
     7   <filter-mapping>
     8     <filter-name>guiceFilter</filter-name>
     9     <url-pattern>/*</url-pattern>
    10   </filter-mapping>
    11   <listener>
    12     <listener-class>com.ming.core.web.listener.GoogleGuiceServletContextListener</listener-class>
    13   </listener>
    14   
    15   <welcome-file-list>
    16     <welcome-file>index.jsp</welcome-file>
    17   </welcome-file-list>
    18 </web-app>

    然后是struts.xml的配置:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7     <!-- 引用guice代理 -->
     8     <constant name="struts.objectFactory" value="guice" />
     9     <constant name="struts.i18n.encoding" value="UTF-8" />    
    10     <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    11     <include file="com/ming/user/action/userStruts.xml"></include>
    12 </struts>

    然后是jpa的persistence.xml的配置:

     1 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
     2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
     4     version="2.0">
     5     <persistence-unit name="myunit" transaction-type="RESOURCE_LOCAL">
     6 
     7         <provider>org.hibernate.ejb.HibernatePersistence</provider>
     8         <class>com.ming.user.entity.Student</class>
     9         <properties>
    10             <property name="hibernate.connection.provider_class"
    11                 value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
    12             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    13             <property name="hibernate.c3p0.max_size" value="2" />
    14             <property name="hibernate.c3p0.min_size" value="1" />
    15             <property name="hibernate.c3p0.timeout" value="120" />
    16             <property name="hibernate.c3p0.max_statements" value="100" />
    17             <property name="hibernate.c3p0.idle_test_period" value="120" />
    18             <property name="hibernate.c3p0.acquire_increment" value="1" />
    19             <property name="hibernate.show_sql" value="true" />
    20             <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
    21             <property name="hibernate.connection.url"
    22                 value="jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=gbk" />
    23             <property name="hibernate.connection.username" value="root" />
    24             <property name="hibernate.connection.password" value="root" />
    25             <property name="hibernate.temp.use_jdbc_metadata_defaults"
    26                 value="false" />
    27         </properties>
    28 
    29         <!-- <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" 
    30             /> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" 
    31             /> <property name="hibernate.connection.driver" value="com.mysql.jdbc.Driver" 
    32             /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8" 
    33             /> <property name="hibernate.connection.user" value="root" /> <property name="hibernate.connection.password" 
    34             value="root" /> <property name="hibernate.temp.use_jdbc_metadata_defaults" 
    35             value="false"/> </properties> -->
    36     </persistence-unit>
    37 </persistence>

    然后是module的一些配置:

    package com.ming.core.web.listener;
    
    import com.google.inject.Guice;
    import com.google.inject.Injector;
    import com.google.inject.servlet.GuiceServletContextListener;
    import com.ming.user.UserModule;
    
    public class GoogleGuiceServletContextListener extends GuiceServletContextListener {
    
        @Override
        protected Injector getInjector() {
            
            return Guice.createInjector(new UserModule());
            //如果绑定多个module,需要像下面这样就可以了
            //return Guice.createInjector(new UserModule(),new UserModule());
        }
    
    }
    package com.ming.user;
    
    import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
    
    import com.google.inject.AbstractModule;
    import com.google.inject.Singleton;
    import com.google.inject.persist.PersistFilter;
    import com.google.inject.persist.jpa.JpaPersistModule;
    import com.google.inject.servlet.ServletModule;
    import com.google.inject.struts2.Struts2GuicePluginModule;
    public class UserModule extends AbstractModule {
        @Override
        protected void configure() {
            install(new ServletModule(){
                @Override
                protected void configureServlets() {
                    install(new JpaPersistModule("myunit")); //这个一定要写最前面
                    install(new Struts2GuicePluginModule());//这个是struts2与guice组件结合的注入
                    bind(StrutsPrepareAndExecuteFilter.class).in(Singleton.class);//这个类似struts2的那个过滤
                    filter("/*").through(StrutsPrepareAndExecuteFilter.class);
                    filter("/*").through(PersistFilter.class);
                }
            });
        
        }
        
    }

    以上是几个重要的配置文件。

    下面是项目结构:

     注意persistence.xml这个配置文件要放在src下的META-INF下。

    框架例子下载:源码下载

  • 相关阅读:
    Python(2.7.6) 特殊方法
    Python(2.7.6) 列表推导式
    代码神注释鉴赏,喜欢拿去用
    为什么说重启能解决90%的问题
    为什么说重启能解决90%的问题
    编程语言简史
    编程语言简史
    Shell脚本中循环select命令用法笔记
    程序员职业规划
    程序员职业规划
  • 原文地址:https://www.cnblogs.com/huzi007/p/5821594.html
Copyright © 2020-2023  润新知