Spring Aop开发基于AspectJ注解方式的案例
2016-12-16 17:03
1021 查看
工程结构:
运行效果:
代码如下:
ServiceAop.java
UserController.java
UserService.java
applicationContext.xml
springmvc.xml
web.xml
运行效果:
代码如下:
ServiceAop.java
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class ServiceAop { @Pointcut("execution(* com.sunsharing.web.service.*.*(..))") public void commonPoint() { }; public ServiceAop() { System.out.println("初始化"); } @Before("commonPoint()") public void doBefore(JoinPoint jp) { System.out.println("前置通知before++++++++++++++++++++++"); System.out.println(jp.toString());// 连接点所在位置的相关信息 System.out.println(jp.toShortString());// 连接点所在位置的简短相关信息 System.out.println(jp.toLongString());// 连接点所在位置的全部相关信息 System.out.println(jp.getThis()); // 返回AOP代理对象 System.out.println(jp.getTarget()); // 返回目标对象 System.out.println(jp.getArgs()); // 返回被通知方法参数列表 System.out.println(jp.getSignature()); // 返回当前连接点签名 System.out.println(jp.getSourceLocation());// 返回连接点方法所在类文件中的位置 System.out.println(jp.getKind()); // 连接点类型 System.out.println(jp.getStaticPart()); // 返回连接点静态部分 } @After("commonPoint()") public void doAfter() { System.out.println("最后的通知after++++++++++++++++++++++"); } }
UserController.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.sunsharing.web.service.UserService; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/test.do") public void save(){ userService.save(); } }
UserService.java
import org.springframework.stereotype.Service; @Service public class UserService { public void save() { System.out.println("--------进入service-------"); } }
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.sunsharing.web"/> <context:annotation-config/> <aop:aspectj-autoproxy proxy-target-class="true" /> </beans>
springmvc.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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <context:component-scan base-package="com.sunsharing.web"></context:component-scan> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!--视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringAop</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <!-- 配置核心控制器 --> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <!-- 定义加载的xml文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc.xml,/WEB-INF/applicationContext.xml</param-value> </init-param> <!-- 启动时加载 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
相关文章推荐
- spring框架的学习(五)——Spring使用AspectJ进行AOP的开发:注解方式
- Spring学习-22:Spring的AOP:基于AspectJ的XML配置方式开发
- Spring使用AspectJ进行AOP的开发:注解方式
- Spring AOP面向切面编程详解(基于XML方式 注解方式 注入Aspectj方式)
- (转)Spring使用AspectJ进行AOP的开发:注解方式
- Intellij IDEA创建Spring项目之基于Aspectj的AOP操作(注解方式)
- Spring(十二)AspectJ框架开发AOP(基于注解)
- Spring 【Aspectj】进行AOP开发 (【注解】方式)
- 基于AspectJ的注解方式进行AOP开发
- Spring学习-21:Spring的AOP:基于AspectJ的注解开发
- 基于AspectJ的注解方式进行AOP开发
- Spring AOP使用配置介绍(四):基于@AspectJ注解的aop
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
- Spring——AOP(面向切面编程)@AspectJ注解方式
- Spring基于注解@AspectJ的AOP
- Spring AOP的使用 基于全注解AspectJ
- Spring-注解的方式配置AOP-案例
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring的AOP的AspectJ的开发:(注解)
- Spring学习4-面向切面(AOP)之aspectj注解方式