spring 自定义类实现aop ,简单的DI依赖注入

本文介绍了如何在Spring框架中自定义切面类,并通过配置文件applicationContext.xml进行依赖注入,详细步骤包括在pom.xml中导入相关依赖,编写自定义的切面类以及设置配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.导包 pom.xml

spring-mvc中包含了大部分spring框架所需要的包
在这里插入图片描述

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
                <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.14</version>
        </dependency>

2.自定义切面类

package com.seven.aop;

/*
aop自定义实现
 */
public class MyPointCut {
    public void before(){
        System.out.println("方法执行前------------");
    }
    public void after(){
        System.out.println("方法执行后------------");
    }
}

3.配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--引入aop xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
-->
<!--引入c和p命名空间,可以使用p:id="11111"  c:_0="sssss"
来注入属性值 xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    开启注解支持-->
    <context:annotation-config/>
<!--    开启扫描包,entity下的所有包被扫描到spring ioc容器中,默认名称为类名小写-->
    <context:component-scan base-package="com.seven.entity"/>
<!--     给bean 设置别名 -->
    <alias name="user" alias="user123"/>

    <bean id="user" class="com.seven.entity.User" p:id="11111" c:_0="sssss" c:_1="122222"/>

    <bean id="userService" class="com.seven.aop.UserService"/>
<!--    自定义的切入点类-->
    <bean id="diy" class="com.seven.aop.MyPointCut"/>
<!--    自定义切面配置-->
    <aop:config>
<!--        ref:引用类,自定义的切入点类-->
        <aop:aspect ref="diy">
<!--            切入点 execution(* com.seven.aop.UserService.*(..)) UserService下的所有方法,参数不限-->
            <aop:pointcut id="point" expression="execution(* com.seven.aop.UserService.*(..))"/>
<!--            定义什么时候执行什么方法 before 方法执行前星星point1这个方法-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值