.
package com.itheima.service.impl;
import com.itheima.service.UserService;
public class UserServiceImpl implements UserService {
public void save() {
System.out.println("user service running.....");
}
}
package com.itheima.aop;
public class AOPAdvice {
public void before(){
System.out.println("before");
}
}
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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
<bean id="myAdvice" class="com.itheima.aop.AOPAdvice"/>
<!--<aop:config>
<aop:pointcut id="pt" expression="execution(* *..*(..))"/>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>-->
<!--<aop:config>
<aop:pointcut id="pt" expression="execution(* *..*(..))"/>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pt"/>
</aop:aspect>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pt"/>
</aop:aspect>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>-->
<aop:config>
<aop:pointcut id="pt" expression="execution(* com..UserService.save(..))"/>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>
</beans>
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
}
}