spring学习6之使用AspectJ进行AOP开发(XML方式)

利用spring整合进行代理

第一步:引入spring相关的jar包,core,context,aop,aspectjweaver

   <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.pp</groupId>
    <artifactId>spring_day3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

第二步:在com.pp先新建个demo1包,创建一个UserDao接口类

package com.pp.demo1;

public interface UserDao {
    void save();
    void update();
}

第三步:创建UserDaoImpl 继承和实现UserDao接口类

package com.pp.demo1;

import org.springframework.stereotype.Component;


public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("保存");
    }

    public void update() {
        System.out.println("更新");
    }
}

第四步:创建MyAspectXml代理类

package com.pp.demo1;

import org.springframework.stereotype.Component;


public class MyAspectXml {
    public  void after(){
        System.out.println("后置通知");
    }

    public void before(){
        System.out.println("前置通知");
    }
}

第五步:编写测试类Demo3

package com.pp.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo3 {

    @Resource(name = "userDao")   //按照名字注入
    private UserDao userDao;

    @Test
    public void run(){
        userDao.save();
    }

}

在测视类中写一个@RunWith(SpringJUnit4ClassRunner.class),还有个@ContextConfiguration(“classpath:applicationContext.xml”)可以代替原来的注解@Test,还有先通过 那注解文件使用工厂实例对象,调用对象的方法。

第六步:配置

<?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
">
    <!--注解扫描-->
    <context:component-scan base-package="com.pp"/>
    <bean id="userDao" class="com.pp.demo1.UserDaoImpl"/>
    <bean id="myAspectXml" class="com.pp.demo1.MyAspectXml"/>

    <aop:config>
       <aop:aspect ref="myAspectXml">
           <aop:after method="after" pointcut="execution(public void com.pp.demo1.UserDaoImpl.save()
         )"></aop:after>
       </aop:aspect>
    </aop:config>
</beans>

第七步:测试结果
在这里插入图片描述

扩展:(与上面不同的是这里类是通过注解形式,而代理是通过配置的形式,了解就行)
第一步:引入jar包(略)
第二步:创建一个UserDao接口类

package com.pp.demo1;

public interface UserDao {
    void save();
    void update();
}

第三步:创建UserDaoImpl类,在改类中使用@Component并且value值为实体类的小写userDao

package com.pp.demo1;

import org.springframework.stereotype.Component;

@Component(value = "userDao")
public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("保存");
    }

    public void update() {
        System.out.println("更新");
    }
}

第四步:在这里使用注解@Component并且value值设置为myAspectXml

package com.pp.demo1;

import org.springframework.stereotype.Component;

@Component(value = "myAspectXml")
public class MyAspectXml {
    public  void after(){
        System.out.println("后置通知");
    }

    public void before(){
        System.out.println("前置通知");
    }
}

第五步:编写测试类

package com.pp.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo3 {

    @Resource(name = "userDao")
    private UserDao userDao;

    @Test
    public void run(){
        userDao.save();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值