您现在的位置是:首页 > 个人日记个人日记
spring-07-AOP环境搭建
2019-02-03 18:44:39【个人日记】717人已围观
简介AOP编程,可以用在权限管理等等
AOP环境搭建
先配置pom.xml文件
增加aspectj的jar包
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.huangxin</groupId><artifactId>spring04-AOP</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--spring框架导入--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.17.RELEASE</version></dependency><!--spring-test--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.17.RELEASE</version><scope>test</scope></dependency><!--单元测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!--AspectJ AOP环境--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.17.RELEASE</version></dependency></dependencies></project>
创建一个对象
user.java
package com.spring.model;public class User {private int id;private String userName;private String userTelephone;private String userSex;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserTelephone() {return userTelephone;}public void setUserTelephone(String userTelephone) {this.userTelephone = userTelephone;}public String getUserSex() {return userSex;}public void setUserSex(String userSex) {this.userSex = userSex;}}
再创建一个对该对象的处理的方法
package com.spring.service;import com.spring.model.User;/*** 目标类*/public class UserService {public void add(User user) {// int a=1/0;System.out.println("添加用户");}public void update(User user) {System.out.println("更新用户");}public void deleteById(Integer id) {System.out.println("删除用户");}}
再次添加一个对该方法增强的方法
package com.spring.aspect;import org.aopalliance.intercept.Joinpoint;public class TransactionAdvice {/*** 前置通知*/public void brfore() {System.out.println("开启事务");}/*** 后置通知*/public void afterReturning() {System.out.println("提交事务");}/*** 最终停止*/public void after() {System.out.println("释放资源");}/*** 异常通知*/public void afterThrowing(Throwable e) {System.out.println("回滚事务" + e.getMessage());}}
重点
对spring-config.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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--目标--><bean id="UserService" class="com.spring.service.UserService"></bean><!--增强类--><bean id="TransationAdvice" class="com.spring.aspect.TransactionAdvice"></bean><!--切面,生成动态代理,将增强注入--><aop:config><!--引入增强--><aop:aspect ref="TransationAdvice"><!--切入点--><!--execution表达式仔细看--><aop:pointcut id="transationPointCut"expression="execution(* com.spring.service.UserService.*(*))"></aop:pointcut><!--前置通知--><aop:before method="brfore" pointcut-ref="transationPointCut"></aop:before><!--后置通知 如果发生错误将不再执行--><aop:after-returning method="afterReturning" pointcut-ref="transationPointCut"></aop:after-returning><!--最终通知,始终被执行--><aop:after method="after" pointcut-ref="transationPointCut"></aop:after><!--出错通知,只有出错才会执行--><aop:after-throwing method="afterThrowing" pointcut-ref="transationPointCut"throwing="e"></aop:after-throwing></aop:aspect></aop:config></beans>
在下个笔记介绍
junit测试
package com.spring.service;import com.spring.model.User;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import static org.junit.Assert.*;public class UserServiceTest {@Testpublic void add() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");UserService bean = applicationContext.getBean(UserService.class);bean.add(new User());// bean.update(new User());bean.deleteById(2);}}
控制台输出
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,add1月 10, 2019 3:56:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@482cd91f: startup date [Thu Jan 10 15:56:51 CST 2019]; root of context hierarchy1月 10, 2019 3:56:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [spring-config.xml]WARNING: An illegal reflective access operation has occurredWARNING: 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)WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operationsWARNING: All illegal access operations will be denied in a future release开启事务添加产品提交事务释放资源开启事务删除产品提交事务释放资源Process finished with exit code 0
Tags: JavaWeb
上一篇: mybatis-05-动态SQL
下一篇: 创建博客心得
相关文章
随机图文
评论区
2025-11-09 06:22:56
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信
