AOP实现方式3——通过<aop:config>来配置(纯POJO切面)

1.定义接口Perform

package com.show;

/**
 * Created by kenneth on 2017/4/6.
 */
public interface Perform {
    void sing();
}

2.接口Perform的实现类Boy

package com.show;

/**
 * Created by kenneth on 2017/4/6.
 */
public class Boy implements Perform {
    @Override
    public void sing() {
        System.out.println("男孩在唱歌");
    }
}

3.定义通知PerformHelper

package com.show;

/**
 * Created by kenneth on 2017/4/6.
 */
public class PerformHelper {
    public void beforePerform() {
        System.out.println("表演之前要整理衣服");
    }

    public void afterPerform() {
        System.out.println("表演之后要行礼");
    }
}

4.Spring配置文件

<?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
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--目标对象-->
    <bean id="boy" class="com.show.Boy"/>

    <!--切面-->
    <bean id="performHelper" class="com.show.PerformHelper"/>

    <!--配置切入点-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(public * *..Perform.sing(..))"/>
        <aop:aspect ref="performHelper">
            <aop:before method="beforePerform" pointcut-ref="pointcut"/>
            <aop:after method="afterPerform" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

</beans>

5.测试类

package com.show;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by kenneth on 2017/4/6.
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Perform performer = (Perform) context.getBean("boy");
        performer.sing();
    }
}

6.测试结果

四月 06, 2017 2:02:38 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@533ddba: startup date [Thu Apr 06 14:02:38 CST 2017]; root of context hierarchy
四月 06, 2017 2:02:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicationContext.xml]
表演之前要整理衣服
男孩在唱歌
表演之后要行礼



<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itheima</groupId> <artifactId>books-management-system</artifactId> <packaging>pom</packaging> <version>0.0.1-SNAPSHOT</version> <name>books-management-system</name> <description>books-management-system</description> <modules> <module>book-common</module> <module>book-pojo</module> <module>book-server</module> </modules> <properties> <mybatis.spring>2.2.0</mybatis.spring> <lombok>1.18.38</lombok> <fastjson>2.0.51</fastjson> <commons-lang>2.6</commons-lang> <druid>1.2.1</druid> <pagehelper>2.1.0</pagehelper> <knife4j>3.0.2</knife4j> <aspectjweaver>1.9.24</aspectjweaver> <jjwt>0.9.1</jjwt> <aliyun-sdk-oss>3.17.4</aliyun-sdk-oss> <jaxb-api>2.3.1</jaxb-api> <poi>3.16</poi> <poi-ooxml>3.16</poi-ooxml> <spring-boot-starter-json>3.5.4</spring-boot-starter-json> <jackson-databind>2.19.2</jackson-databind> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok}</version> </dependency> <!--Java 对象与 JSON 字符串之间的相互转换。--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson}</version> </dependency> <!--简化常见的基础编程操作--> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons-lang}</version> </dependency> <!--Druid 数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pagehelper}</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j}</version> </dependency> <!--AOP(面向切面编程)--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver}</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>${jjwt}</version> </dependency> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>${aliyun-sdk-oss}</version> </dependency> <!-- Java 对象与 XML 数据之间的双向转换能力--> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>${jaxb-api}</version> </dependency> <!--在 Java 中操作 Microsoft Office 文档--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi-ooxml}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>${spring-boot-starter-json}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson-databind}</version> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.5.0</version> </dependency> </dependencies> </dependencyManagement> <repositories> <repository> <id>central</id> <url>https://repo1.maven.org/maven2</url> </repository> <repository> <id>spring-releases</id> <url>https://repo.spring.io/release</url> </repository> </repositories> </project> 这是我的pom文件,就是找不到依赖
08-22
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值