Spring基础AOP 操作

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文件
  • 将所需类交予IOC容器
    <!--配置实体类-->
    <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切面
<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)"/>
             <!--execution表达式==: expression="execution(public * com.xzb.entity.impl.CustomerDaoImpl.logins(String,int))"
                                     expression="execution(方法(切点)修饰词 返回值 方法所处全类名.目标方法(参数列表)))
                                     *代表所有返回值类型 ..代表所有参数类型(多个)"-->
            <!--通知 : Log 通知类(切面)中的方法-->
            <!--配置前置通知-->
            <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对象
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            //通过ApplicationContext对象获取配置的bean得到目标类对象
            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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值