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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
Maven:pom.xml文件所需依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
1.创建一个环境
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +'}';
}
}
public interface CustomerDao {
public User logins(String name,int age);
}
public class CustomerDaoImpl implements CustomerDao {
@Override
public User logins(String name,int age) {
System.out.println(name+"已经登陆!!!!");
return new User(name, age);
}
}
2.创建通知类(切面)
public class Log {
public void before(JoinPoint jp){
System.out.println("前置");
}
public void afterR(JoinPoint jp,User user) {
System.out.println("后置");
}
public User rund(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
User user2 = (User)joinPoint.proceed();
System.out.println("环绕后");
return user2;
}
public void ex(JoinPoint jp,Throwable e){
System.out.println("异常"+e.getMessage());
System.exit(0);
}
public void endl(JoinPoint jp){
System.out.println("最终");
}
}
3.配置applicationContext.xml文件
<bean id="user" class="com.xzb.entity.User"/>
<bean id="customerDaoImpl" class="com.xzb.entity.impl.CustomerDaoImpl"></bean>
<bean id="ting" class="com.xzb.aop.Log"></bean>
<aop:config>
<aop:aspect ref="ting">
<aop:pointcut id="logins"
expression="execution(public * com.xzb.entity.impl.CustomerDaoImpl.logins(String,int))
and args(String,int)"/>
<aop:before method="before" pointcut-ref="logins"/>
<aop:after-returning method="afterR" pointcut-ref="logins" returning="user"/>
<aop:after method="endl" pointcut-ref="logins"/>
<aop:after-throwing method="ex" pointcut-ref="logins" throwing="e"/>
<aop:around method="rund" pointcut-ref="logins"/>
</aop:aspect>
</aop:config>
public class Test
{
public static void main( String[] args ){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerDao customerDaoImpl = (CustomerDao) ac.getBean("customerDaoImpl");
System.out.println(customerDaoImpl.logins("红红", 123456));
}
}
结果:
前置
环绕前
红红已经登陆!!!!
环绕后
最终
后置
User{name='红红', age=123456, car=null, love=null, account=null, pwd=null, sss=null}