方式一
1.导入pom中
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
2.导入aop
快捷键:<aop:config+enter键
<?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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.zy.pojo.service.UserServiceImpl"></bean>
<bean id="beforLogo" class="com.zy.pojo.logo.BeforLogo"></bean>
<bean id="afterLogo" class="com.zy.pojo.logo.AfterLogo"></bean>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.zy.pojo.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="beforLogo" pointcut-ref="pointCut"></aop:advisor>
<aop:advisor advice-ref="afterLogo" pointcut-ref="pointCut"></aop:advisor>
</aop:config>
</beans>
test
public class MyTest {
@Test
public void testAop(){
ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
方式二
1.自定义类
public class DiyPoint {
public void before(){
System.out.println("....方法执行前....");
}
public void after(){
System.out.println("....方法执行后....");
}
}
2.bean
<?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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.zy.pojo.service.UserServiceImpl"></bean>
<bean id="beforLogo" class="com.zy.pojo.logo.BeforLogo"></bean>
<bean id="afterLogo" class="com.zy.pojo.logo.AfterLogo"></bean>
<!-- 方式二:自定义类-->
<bean id="diy" class="com.zy.pojo.diy.DiyPoint"></bean>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.zy.pojo.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"></aop:before>
<aop:after method="after" pointcut-ref="point"></aop:after>
</aop:aspect>
</aop:config>
</beans>
方式三
1.自定义类
@Aspect
public class AnnotationPointCut {
@Before("execution(* com.zy.pojo.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("....方法执行前....");
}
@After("execution(* com.zy.pojo.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("....方法执行后....");
}
}
2.beans
<?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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.zy.pojo.service.UserServiceImpl"></bean>
<bean id="beforLogo" class="com.zy.pojo.logo.BeforLogo"></bean>
<bean id="afterLogo" class="com.zy.pojo.logo.AfterLogo"></bean>
<!-- 方式三 -->
<bean id="annotationPointCut" class="com.zy.pojo.diy.AnnotationPointCut"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>