`

关于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 {
}
"

待续。。。

 

 

 

分享到:
评论

相关推荐

    ssh+aop+log4j+日志拦截器+注解

    标题中的"ssh+aop+log4j+日志拦截器+注解"涉及到的是Java Web开发中的几个核心组件和技术,这些技术在构建大型、分布式的企业级应用时常常被使用。下面将详细介绍这些知识点: 1. SSH (Spring, Struts, Hibernate)...

    spring mvc + mybatis 完整例子

    4. **拦截器**:Spring MVC支持自定义拦截器,可以实现登录验证、日志记录等功能。 5. **异常处理**:通过@ControllerAdvice和@ExceptionHandler可以全局处理异常,提供统一的错误页面。 二、MyBatis 框架 1. **...

    spring mvc 依赖包

    这个包中的核心接口包括`org.aopalliance.intercept.MethodInterceptor`和`org.aopalliance.aop.Advice`,它们定义了拦截器和通知的行为。 2. `...

    spring mvc之实现简单的用户管理四--查看用户信息

    在本教程中,我们将深入探讨如何在Spring MVC框架中实现一个简单的用户管理系统,特别是关注...同时,这只是一个起点,Spring MVC还有许多高级特性和最佳实践,如数据验证、异常处理、拦截器等,等待你去探索和学习。

    spring MVC必备jar包

    Spring MVC是Spring框架的一个核心模块,专为构建Web应用程序提供模型-视图-控制器(MVC)架构。这个“spring MVC必备jar包”包含了运行Spring MVC应用所需的所有基础组件和依赖。下面,我们将深入探讨这些jar包以及...

    Spring MVC4 整合Mybatis 所需全部Jar包

    - `spring-aop.jar`: 支持面向切面编程,用于事务管理和其他拦截器的实现。 2. **Mybatis相关Jar包** - `mybatis.jar`: Mybatis的主要库,包含SqlSessionFactory、SqlSession等核心类。 - `mybatis-spring.jar`:...

    Spring MVC创建-SSM框架整合2.pdf

    Spring MVC作为Spring框架的一部分,它提供了丰富的功能,包括请求映射、视图解析、数据绑定、表单验证以及拦截器等,使得开发者能够构建结构清晰且易于维护的Web应用。 在创建一个Spring MVC项目时,首先需要进行...

    Spring mvc +mybatis +dwr 整合

    1. **配置Spring MVC**:创建Spring MVC的配置文件,定义DispatcherServlet、视图解析器、模型-视图-适配器(MVC)组件,以及其它必要的拦截器和监听器。 2. **集成MyBatis**:在Spring配置文件中引入MyBatis的...

    springmvc+spring+hibernate

    4. **配置Spring MVC**:创建servlet-context.xml文件,配置Spring MVC的相关组件,如DispatcherServlet、视图解析器、模型视图、拦截器等。这里需要定义Controller扫描路径,以便Spring MVC能找到处理请求的控制器...

    SpringMVC框架架构介绍

    通过实现HandlerInterceptor接口,创建拦截器来执行预处理和后处理操作,例如日志记录、权限检查等。 六、全局异常处理: 通过定义@ControllerAdvice和@ExceptionHandler注解,可以实现全局异常处理,捕获并处理...

    SSM(Spring+Spring MVC+Mybatis)jar包集合

    Spring的IOC容器负责管理对象的生命周期和对象间的依赖关系,而AOP则允许开发者定义方法拦截器,实现非业务逻辑的代码分离,如日志记录、事务管理等。 在SSM中,Spring MVC作为Spring的一个子项目,是构建Web应用...

    struts2 spring ibatis整合以及拦截器日志记录

    同时,Spring还提供了AOP(面向切面编程)功能,拦截器在某种程度上就是AOP的一种实现。 iBatis作为一个轻量级的ORM框架,它简化了SQL操作,将数据库查询与业务逻辑分离。iBatis允许开发者直接编写SQL语句,提供了...

    Spring 3 MVC - Part 1 & Part 2 (with log4j)

    4. **拦截器**:学习自定义Interceptor,实现预处理和后处理功能,如权限控制、日志记录等。 5. **视图模板引擎**:除了JSP,介绍如何集成FreeMarker或Thymeleaf等模板引擎,提高视图层的可维护性。 6. **Maven...

    Spring-Spring mvc-MyBatis

    4. **配置Spring MVC**: 创建`spring-mvc.xml`配置文件,配置视图解析器、拦截器等。 #### 三、SSM框架整合实践 SSM框架整合主要是指将Spring、Spring MVC和MyBatis三个框架组合在一起,实现一个完整的Web应用程序...

    spring mVC项目

    Spring MVC 是一个强大的Java Web开发框架,它是Spring框架的一部分,专为构建Web应用程序...在实际应用中,还可能涉及到异常处理、拦截器、国际化、安全控制等多个方面,这些都可以通过Spring MVC的扩展机制进行定制。

    项目配置文件( spring-mvc.xml spring-mybatis.xml web.xml log4j.properties)

    通过这个配置,Spring可以管理MyBatis的SqlSession,实现数据库操作的事务控制,并且能够自动扫描和加载Mapper接口,使得SQL查询可以通过注解或者XML文件进行定义。 3. **web.xml**: 这是Web应用的部署描述符,定义...

    JavaSpringMvc的jar包

    5. **spring-aop.jar**:支持 AOP 面向切面编程,允许开发者定义方法拦截器和切入点表达式。 6. **spring-expression.jar**:Spring 的表达式语言(SPeL),用于在运行时查询和操作对象图。 7. **servlet-api.jar**...

    springmvc log4j2 logback 注解 jackson 日志脱敏实现源码

    这可能是通过拦截器或者自定义处理器实现的,确保对外发送的数据安全。 3. **基于注解的脱敏方式**:注解是Java中的元数据,可以用来标记代码以执行特定的操作。在日志脱敏场景下,开发者可以在敏感字段上添加注解...

    Spring3_MVC_基础实践之路

    - 通过拦截器实现权限控制,检查用户是否具有访问资源的权限,如果没有则重定向到登录页面或其他错误页面。 8. **全局异常处理**: - 可以创建一个`@ControllerAdvice`注解的类,包含`@ExceptionHandler`方法来...

    Spring MVC依赖Jar包下载

    6. **spring-aop.jar**:提供AOP(面向切面编程)支持,可以实现拦截器、通知、代理等,用于解耦关注点,如日志、事务管理等。 7. **spring-jdbc.jar**:提供了数据库访问的支持,包括JDBC模板和事务管理。虽然...

Global site tag (gtag.js) - Google Analytics