`

关于Spring mvc 注解 拦截器实现 log 功能

 
阅读更多

前两天 项目 快要结尾的时候,我们需要 程序 需要 完善一个log 日志记录 。 日志记录 由拦截器 每次触发,log 每次 记录到 数据库中,  接下来是 注解代码的实现 和配置文件。。

 

首先 配置  数据库连接 文件。  persistence.xml

 

"

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

<persistence-unit name="sxm_cpbh" transaction-type="RESOURCE_LOCAL">// 数据持久化的 名称  数据持久化的类型

 

 <provider>org.hibernate.ejb.HibernatePersistence</provider> //hibernate持久化

       <properties>
            <property name="hibernate.connection.driver_class"
                      value="oracle.jdbc.driver.OracleDriver"/> //数据库驱动名称
          
            <property name="hibernate.connection.url" 
                      value="jdbc:oracle:thin:@192.168.96.251:1521:~~~~"/>//数据库 名称 url
            <property name="hibernate.connection.username" value="~~~~"/>
            <property name="hibernate.connection.password" value="~~~~"/>
            <property name="hibernate.dialect"
                      value="org.hibernate.dialect.Oracle10gDialect"/> // 方言

            <property name="hibernate.show_sql" value="true"/> //执行 时显示 sql语句

 

  </persistence-unit>

</persistence>

"

 

  接下来是 Spring-mvc.xml 的 配置

"

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="  
          http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context-3.0.xsd  
          http://www.springframework.org/schema/mvc      
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
          http://www.springframework.org/schema/util   
          http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
      

<!-- 自动扫描包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.cmcc.*"/>

    <aop:aspectj-autoproxy proxy-target-class="true"/>

//当配为<aop:aspectj-autoproxy 
poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。

 


    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/">
    </property>
    <property name="suffix" value=".jsp">
    </property>
</bean>



<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
 class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>
                text/html;charset=UTF-8
            </value>
        </list>
    </property>
</bean>

<!-- Validator -->
<mvc:annotation-driven validator="validator"/> //    Spring  的 控制器   (应该是  IOC)

<bean id="validator"
 class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
    <property name="validationMessageSource" ref="messageSource"/>
</bean>

<bean id="messageSource"
 class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages"/>
    <property name="fileEncodings" value="utf-8"/>
    <property name="cacheSeconds" value="120"/>
</bean>

 

-------------------------------------------------------------------------------------------------------------------
<bean id="multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding">
        <value>
            UTF-8
        </value>
    </property>
    <property name="maxUploadSize">
        <value>
            32505856
        </value>
        <!-- 上传文件大小限制为31M,31*1024*1024 -->
    </property>
    <property name="maxInMemorySize">
        <value>
            4096
        </value>
    </property>
</bean>

    <tx:annotation-driven transaction-manager="transactionManager"  />

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <!--<property name="dataSource" ref="dataSource"/>-->
        <property name="persistenceUnitName" value="sxm_cpbh"/>
        <property name="persistenceXmlLocation" value="classpath:META-INF/ct_persistence.xml"/>
    </bean>

//实体工厂 bean

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

</beans>

"

 

 

"<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                 http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                http://www.springframework.org/schema/jee
               http://www.springframework.org/schema/jee/spring-jee.xsd"
       default-autowire="byName"
       >

    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:config.properties" />
    <context:annotation-config  />
    <!-- 自动扫描(自动注入) -->
   
    <context:component-scan base-package="com.cmcc.soc"  />
   
    <aop:aspectj-autoproxy proxy-target-class="true"/>
   
</beans>"

 

 

" 注解  方法 的 接口

 

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface WSCLogging {
}
"

待续。。。

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics