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: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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--初测bean-->
<bean id="userService" class="com.yao.service.userServiceImpl">
</bean>
<bean id="log" class="com.yao.log.log"></bean>
<bean id="afterLog" class="com.yao.log.afterLog"></bean>
<!--配置aop 需要导入aop的约束-->
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.yao.service.userServiceImpl.*(..))"/>
<!-- 执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>
afterLog.class
public class afterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+"返回值"+o);
}
}
log.class
public class log implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
userService.interface
public interface userService {
public void add();
public void delete();
public void update();
public void query();
}
userServiceImpl.class
public class userServiceImpl implements userService {
public void add() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("修改了一个用户");
}
public void query() {
System.out.println("查找了一个用户");
}
}
client.class
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
userService userService = (userService) context.getBean("userService");
userService.add();
}
}
原文:https://www.cnblogs.com/yyyyyou/p/14631464.html