<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id="user" class="com.qhit.aop2.User"></bean> <bean id="aop2" class="com.qhit.aop2.Aop2"></bean> <!-- 配置aop --> <aop:config> <!-- 切点 expression 表达式===》通过表达式说明切点在哪 --> <aop:pointcut expression="execution(* com.qhit.aop2.User.*(..))" id="pt"/> <!-- 切面 --> <aop:aspect ref="aop2"> <aop:before method="begin" pointcut-ref="pt"/> <aop:after method="end" pointcut-ref="pt"/> </aop:aspect> </aop:config> </beans>
package com.qhit.aop2;
/*
* spring配置完成AOP编程
* 切面
* */
public class Aop2 {
public void begin(){
System.out.println("用户开始登陆。。");
}
public void end(){
System.out.println("用户已经下线。。");
}
}
package com.qhit.aop2;
public interface UserDao {
void login();
}
继承接口
package com.qhit.aop2;
public class User implements UserDao{
//切点
@Override
public void login() {
System.out.println("登陆成功。。");
}
}
package com.qhit.aop2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("/com/qhit/aop2/applicationContext.xml");
UserDao u=(UserDao) context.getBean("user");
u.login();
}
}