您现在的位置是:首页 > 个人日记个人日记

spring-07-AOP环境搭建

2019-02-03 18:44:39【个人日记】284人已围观

简介AOP编程,可以用在权限管理等等

AOP环境搭建

先配置pom.xml文件

增加aspectj的jar包

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.huangxin</groupId>
  7. <artifactId>spring04-AOP</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!--spring框架导入-->
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-context</artifactId>
  14. <version>4.3.17.RELEASE</version>
  15. </dependency>
  16. <!--spring-test-->
  17. <dependency>
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring-test</artifactId>
  20. <version>4.3.17.RELEASE</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <!--单元测试-->
  24. <dependency>
  25. <groupId>junit</groupId>
  26. <artifactId>junit</artifactId>
  27. <version>4.12</version>
  28. <scope>test</scope>
  29. </dependency>
  30. <!--AspectJ AOP环境-->
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-aspects</artifactId>
  34. <version>4.3.17.RELEASE</version>
  35. </dependency>
  36. </dependencies>
  37. </project>

创建一个对象
user.java

  1. package com.spring.model;
  2. public class User {
  3. private int id;
  4. private String userName;
  5. private String userTelephone;
  6. private String userSex;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getUserName() {
  14. return userName;
  15. }
  16. public void setUserName(String userName) {
  17. this.userName = userName;
  18. }
  19. public String getUserTelephone() {
  20. return userTelephone;
  21. }
  22. public void setUserTelephone(String userTelephone) {
  23. this.userTelephone = userTelephone;
  24. }
  25. public String getUserSex() {
  26. return userSex;
  27. }
  28. public void setUserSex(String userSex) {
  29. this.userSex = userSex;
  30. }
  31. }

再创建一个对该对象的处理的方法

  1. package com.spring.service;
  2. import com.spring.model.User;
  3. /**
  4. * 目标类
  5. */
  6. public class UserService {
  7. public void add(User user) {
  8. // int a=1/0;
  9. System.out.println("添加用户");
  10. }
  11. public void update(User user) {
  12. System.out.println("更新用户");
  13. }
  14. public void deleteById(Integer id) {
  15. System.out.println("删除用户");
  16. }
  17. }

再次添加一个对该方法增强的方法

  1. package com.spring.aspect;
  2. import org.aopalliance.intercept.Joinpoint;
  3. public class TransactionAdvice {
  4. /**
  5. * 前置通知
  6. */
  7. public void brfore() {
  8. System.out.println("开启事务");
  9. }
  10. /**
  11. * 后置通知
  12. */
  13. public void afterReturning() {
  14. System.out.println("提交事务");
  15. }
  16. /**
  17. * 最终停止
  18. */
  19. public void after() {
  20. System.out.println("释放资源");
  21. }
  22. /**
  23. * 异常通知
  24. */
  25. public void afterThrowing(Throwable e) {
  26. System.out.println("回滚事务" + e.getMessage());
  27. }
  28. }

重点

spring-config.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  5. <!--目标-->
  6. <bean id="UserService" class="com.spring.service.UserService"></bean>
  7. <!--增强类-->
  8. <bean id="TransationAdvice" class="com.spring.aspect.TransactionAdvice"></bean>
  9. <!--切面,生成动态代理,将增强注入-->
  10. <aop:config>
  11. <!--引入增强-->
  12. <aop:aspect ref="TransationAdvice">
  13. <!--切入点-->
  14. <!--execution表达式仔细看-->
  15. <aop:pointcut id="transationPointCut"
  16. expression="execution(* com.spring.service.UserService.*(*))"></aop:pointcut>
  17. <!--前置通知-->
  18. <aop:before method="brfore" pointcut-ref="transationPointCut"></aop:before>
  19. <!--后置通知 如果发生错误将不再执行-->
  20. <aop:after-returning method="afterReturning" pointcut-ref="transationPointCut"></aop:after-returning>
  21. <!--最终通知,始终被执行-->
  22. <aop:after method="after" pointcut-ref="transationPointCut"></aop:after>
  23. <!--出错通知,只有出错才会执行-->
  24. <aop:after-throwing method="afterThrowing" pointcut-ref="transationPointCut"
  25. throwing="e"></aop:after-throwing>
  26. </aop:aspect>
  27. </aop:config>
  28. </beans>

在下个笔记介绍

junit测试

  1. package com.spring.service;
  2. import com.spring.model.User;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import static org.junit.Assert.*;
  7. public class UserServiceTest {
  8. @Test
  9. public void add() {
  10. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
  11. UserService bean = applicationContext.getBean(UserService.class);
  12. bean.add(new User());
  13. // bean.update(new User());
  14. bean.deleteById(2);
  15. }
  16. }

控制台输出

  1. D:\Java\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\IntelliJ\lib\idea_rt.jar=59134:D:\IntelliJ\bin -Dfile.encoding=UTF-8 -classpath D:\IntelliJ\lib\idea_rt.jar;D:\IntelliJ\plugins\junit\lib\junit-rt.jar;D:\IntelliJ\plugins\junit\lib\junit5-rt.jar;D:\ideaProjects\spring04AOP\target\test-classes;D:\ideaProjects\spring04AOP\target\classes;D:\maven-repository\org\springframework\spring-context\4.3.17.RELEASE\spring-context-4.3.17.RELEASE.jar;D:\maven-repository\org\springframework\spring-aop\4.3.17.RELEASE\spring-aop-4.3.17.RELEASE.jar;D:\maven-repository\org\springframework\spring-beans\4.3.17.RELEASE\spring-beans-4.3.17.RELEASE.jar;D:\maven-repository\org\springframework\spring-core\4.3.17.RELEASE\spring-core-4.3.17.RELEASE.jar;D:\maven-repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\maven-repository\org\springframework\spring-expression\4.3.17.RELEASE\spring-expression-4.3.17.RELEASE.jar;D:\maven-repository\org\springframework\spring-test\4.3.17.RELEASE\spring-test-4.3.17.RELEASE.jar;D:\maven-repository\junit\junit\4.12\junit-4.12.jar;D:\maven-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\maven-repository\org\springframework\spring-aspects\4.3.17.RELEASE\spring-aspects-4.3.17.RELEASE.jar;D:\maven-repository\org\aspectj\aspectjweaver\1.8.9\aspectjweaver-1.8.9.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.spring.service.UserServiceTest,add
  2. 1 10, 2019 3:56:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  3. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@482cd91f: startup date [Thu Jan 10 15:56:51 CST 2019]; root of context hierarchy
  4. 1 10, 2019 3:56:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  5. 信息: Loading XML bean definitions from class path resource [spring-config.xml]
  6. WARNING: An illegal reflective access operation has occurred
  7. WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/D:/maven-repository/org/springframework/spring-core/4.3.17.RELEASE/spring-core-4.3.17.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
  8. WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
  9. WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
  10. WARNING: All illegal access operations will be denied in a future release
  11. 开启事务
  12. 添加产品
  13. 提交事务
  14. 释放资源
  15. 开启事务
  16. 删除产品
  17. 提交事务
  18. 释放资源
  19. Process finished with exit code 0

Tags: JavaWeb  

评论区

    2020-11-26 13:57:08

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我