前言:
在使用SpringAOP思想进行日志处理的时候遇到了一个问题,SpringAOP配置切点比较麻烦,而且通配拦截方法,对于不需要做日志的方法就多余了,于是使用自定义注解方式,在需要拦截做日志的部分加上自定义注解即可,于是顺着这个思路开始做
环境:
SpringMVC+Spring+Mybatis【测试环境,就没配置Mybatis】
Maven管理工具
具体配置:
SpringMVC配置文件【dispatcher-servlet.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 扫描Controller,此处不扫描controller,所有的请求都无法映射 -->
<context:component-scan base-package="com.rambler.controller"/>
<!-- 方法和url映射 -->
<mvc:annotation-driven/>
</beans>
Spring配置文件【applicationContext.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/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描所有包,service,dao等等 -->
<context:component-scan base-package="com.rambler.*"/>
<!-- 设置代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
最后发一下web.xml中关键的springMVC和spring的配置
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数,配置文件位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
</init-param>
<!-- 使得服务器再启动的时候就初始化 -->
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置contextLoaderListener,用于初始化Spring IoC容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener- class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
代码:
切面【LogAOP】
package com.rambler.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LogAOP {
@Before(value = "@annotation(com.rambler.aop.LogAnnotation)")
public void doBeforeStart(JoinPoint joinPoint) {
System.out.println("方法之前进行拦截...........");
}
}
自定义注解【LogAnnotation】
package com.rambler.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({java.lang.annotation.ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAnnotation {
/* 关于日志的描述信息 */
String description() default "";
/* 进行的操作 */
String operation() default "";
}
Controller层【BaseController】
package com.rambler.controller;
import com.rambler.aop.LogAnnotation;
import com.rambler.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class BaseController {
@Autowired
BaseService baseService;
@RequestMapping("getParams")
@LogAnnotation(description = "Aop日志", operation = "查询")
public void getShoppingCart() {
baseService.run();
}
}
然后是业务层【BaseService】
package com.rambler.service;
import org.springframework.stereotype.Service;
@Service("service")
public class BaseService {
public void run(){
System.out.println("service......");
}
}
如果你的配置和我基本一样,那么这篇博客你就看对了,我花了一天的时间终于把这件事整明白了
看了很多博客,做了很多修改其实和我原来也所差不大,大家都说该怎么配,却没说该配在哪里,于是一直按照我的进行测试,新建一个Test类,在Service上添加自定义注解,进行测试,发现Service上的注解生效了
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/applicationContext.xml");
BaseService baseService = (BaseService) context.getBean("service");
baseService.run();
这样是可以生效的,于是开始思考,为什么service可以被拦截到,controller无法被拦截到,后来经过百度才知道,我在SpringMVC的配置文件里扫描了所有的Controller层,所以所有的Controller都被SpringMVC进行管理,Spring中扫描的是所有的包,但是发现Controller已经被SpringMVC扫描过,因此不在重复扫描,只扫描其他注解进行管理,于是AOP在请求被代理对象【也就是Controller】的时候,发现Controller不存在【被SpringMVC管理】,所以无法织入,因此AOP失效
关键点就是:SpringMVC和Spring管理的是两个不同的容器
同理:以前遇到的一个问题,Spring配置文件中通过<bean>标签注册的bean和通过扫描也就是<context:compontent:scan>扫描的包也是两个容器进行管理的,无法互相注入(通过<bean>注册的类中无法注入通过扫描获得的对象)
解决:
将applicationContext.xml中<aop:aspectj-autoproxy proxy-target-class="true"/>这段配置放在dispatcher-servlet.xml中,问题解决,如果还有疑问,欢迎下方留言...