Spring-AOP标签scoped-proxy

<aop:scoped-proxy/>介绍:

  Spring的Bean是有scope属性的,表示bean的生存周期。scope的值有prototype、singleton、session、request。那么就有个问题了,如果一个singleton的bean中引用了一个prototype的bean,结果会怎样呢?在默认情况下,单例会永远持有一开始构造所赋给它的值。

所以,为了让我们在每次调用这个Bean的时候都能够得到具体scope中的值,比如prototype,那么我们希望每次在单例中调用这个Bean的时候,得到的都是一个新的prototype,Spring中AOP名字空间中引入了这个标签。 <aop:scoped-proxy/>。下面具体看一个例子:

步骤一:创建两个bean。一个将来的生存周期是singleton,一个将来的生存周期是prototype

package org.burning.sport.model.proxy;

import java.util.Date;

public class PrototypeBean {
    private Long timeMilis;

    public PrototypeBean() {
        this.timeMilis = new Date().getTime();
    }

    public void printTime() {
        System.out.println("timeMils:" + timeMilis);
    }
}
package org.burning.sport.model.proxy;

public class SingletonBean {
    private PrototypeBean prototypeBean;

    public void setPrototypeBean(PrototypeBean prototypeBean) {
        this.prototypeBean = prototypeBean;
    }

    public void printTime() {
        prototypeBean.printTime();
    }

}

 

步骤二:新建一个xml文件scopedProxyBean.xml。用来创建bean并且添加相互的依赖关系

<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-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
       default-autowire="byName" default-lazy-init="false">
    <bean id="prototypeBean" class="org.burning.sport.model.proxy.PrototypeBean" scope="prototype">
        <aop:scoped-proxy/>
    </bean>
    <bean id="singletonBean" class="org.burning.sport.model.proxy.SingletonBean">
        <property name="prototypeBean">
            <ref bean="prototypeBean"/>
        </property>
    </bean>
</beans>

 

步骤三:写一个单元测试,观察效果

package bean;

import org.burning.sport.model.proxy.SingletonBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:scopedProxyBean.xml"})
public class ScopedProxyTest {
    @Autowired
    private SingletonBean singletonBean;
    @Test
    public void proxyTest() {
        singletonBean.printTime();
        System.out.println("===============");
        singletonBean.printTime();
    }
}

 

结果:

timeMils:1512617912901
===============

timeMils:1512617913009

总结:我们看到同一个singletonbean打印出来的时间是不一样的,得知prototypeBean是维持了自己的"prototype"生存周期


 

步骤四:把scopedProxyBean.xml中的<aop:scoped-proxy/>注释掉再运行单元测试看输出结果

结果:

timeMils:1512618144214
===============
timeMils:1512618144214

结论:输出的结果是一致的,得知prototypeBean的生存周期被改变为跟singletonbean一样的“singleton”

参考:

[1]博客,http://blog.youkuaiyun.com/Mr_SeaTurtle_/article/details/52992207

转载于:https://www.cnblogs.com/happyflyingpig/p/7998392.html

Spring AOPSpring框架中的一个重要模块,它提供了一种非常方便的方法用于实现面向切面编程。在Spring AOP中,我们可以通过声明式方式来定义切面,然后将切面应用到指定的方法上,从而实现对方法的增强。 一般来说,使用Spring AOP的步骤如下: 1. 定义一个切面类,实现对应的切面逻辑。 2. 在配置文件中声明一个切面,指定切面的类型、切点和通知。 3. 将切面应用到目标对象上,可以通过XML配置文件或者注解方式实现。 4. 测试切面的效果。 下面是一个简单的示例: ```java @Aspect public class LogAspect { @Before("execution(* com.example.service.*.*(..))") public void before(JoinPoint joinPoint) { System.out.println("before"); } @AfterReturning("execution(* com.example.service.*.*(..))") public void afterReturning(JoinPoint joinPoint) { System.out.println("afterReturning"); } } @Service public class UserServiceImpl implements UserService { @Override public void addUser(User user) { System.out.println("add user"); } } <bean id="logAspect" class="com.example.aspect.LogAspect"/> <aop:config> <aop:aspect ref="logAspect"> <aop:before pointcut="execution(* com.example.service.*.*(..))"/> <aop:after-returning pointcut="execution(* com.example.service.*.*(..))"/> </aop:aspect> </aop:config> <bean id="userService" class="com.example.service.UserServiceImpl"> <aop:scoped-proxy/> </bean> ``` 在上面的示例中,我们定义了一个切面类LogAspect,并将其声明为切面。然后,在XML配置文件中,我们指定了切点(execution(* com.example.service.*.*(..)))和通知(before和afterReturning),并将切面应用到UserService实现类上。 当调用UserService的addUser方法时,切面将会在方法执行前和执行后分别输出before和afterReturning。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值