11.通过名字进行自动代理AutoProxyByName

目标:假设现在有好几个类中都有同一个方法,我们想要的效果是只想代理某一个类中的该方法,其他的类中的该方法不想被代理。
主要是BeanNameAutoProxyCreator类的使用


AccountServiceImpl.java

public class AccountServiceImpl implements IAccountService{
	private AccountDao accountDao;
	private Account account;
	
	@Override
	public void bankAction() {
		accountDao.withdraw(account, 100);
		accountDao.deposit(account, 100);
	}
	
	public AccountDao getAccountDao() {
		return accountDao;
	}
	
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public Account getAccount() {
		return account;
	}
	
	public void setAccount(Account account) {
		this.account = account;
	}
}

自增一个新的测试类com.briup.aop.autoProxy.MyTest

public class MyTest {
	public void findUser(long id) {
		System.out.println("findUser方法执行了");
	}
	public void findStudent(long id) {
		System.out.println("findStudent方法执行了");
	}
	public void findTeacher(long id) {
		System.out.println("findTeacher方法执行了");
	}
	public void bankTest(long id) {
		System.out.println("bankTest方法执行了");
	}
}

在AccountServiceImpl和MyTest类中,分别有bankAction(),bankTest()方法,这两个方法用<value>.∗bank.∗</value>就能够被表示出来,不想同时被代理,只想让其中的一个类称为代理类

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- 配置切面类 -->
	<bean name="logger" class="com.briup.aop.aspect.MyLogger"></bean>
	<!-- 配置前置通知 -->
	<bean name="beforeAdvice" class="com.briup.aop.before.BeforeAdvice">
		<!-- 注入切面类对象 -->
		<property name="logger" ref="logger"></property>
	</bean>
	<!-- 配置目标对象 -->
	<bean name="target" class="com.briup.aop.service.AccountServiceImpl">
		<!-- 注入相关的类 -->
		<property name="accountDao" ref="accountDao"></property>
		<property name="account" ref="account"></property>
	</bean>
	<!-- 配置dao层对象和account对象,为了给service注入 -->
	<bean name="accountDao" class="com.briup.aop.dao.AccountDaoImpl"></bean>
	<bean name="account" class="com.briup.aop.pojo.Account">
		<property name="id" value="1"></property>
		<property name="name" value="tom"></property>
		<property name="balance" value="2000"></property>
	</bean>
	<!-- 自动代理 新加的类测试:多个类中的方法同时被代理 -->
	<bean name="test" class="com.briup.aop.autoProxy.MyTest"></bean>
	<!-- 增强器 -->
	<!--作用:筛选出要代理的方法 -->
	<bean id="advisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<!-- 注入通知 -->
		<property name="advice" ref="beforeAdvice"></property>
		<property name="patterns">
			<list>
				<!-- <value>.*bankAction</value> -->
				<value>.*bank.*</value>
			</list>
		</property>
	</bean>

	<!-- 通过名字进行自动代理,自动代理byName的方式 -->
	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<!-- 注入被代理的Bean的名字 -->
		<property name="beanNames"> 
			<!-- 一次指定多个需要被代理的对象 -->
			<list>
				<value>target</value>   <!-- 只代理上面的target,而不代理test -->
				<!-- <value>test</value> --><!-- 只代理test,而不代理target -->
			</list>
		</property>
		<!-- 注入使用的advice和advisor -->
		<property name="interceptorNames">
			<list>
				<value>advisor</value>
			</list>
		</property>
	</bean>
</beans>

图解:
在这里插入图片描述
测试:

// 知识点:通过名字进行自动代理
// 可以通过名字指的代理xml文件中配置的哪些对象
// 注意:这个配置即使没有advisor也可以使用
@Test
public void aop_autoproxybyname() {
	try {
		String path = "com/briup/aop/autoProxyByName/autoProxyByName.xml";
		ApplicationContext container = new ClassPathXmlApplicationContext(path);
				
		IAccountService proxy=(IAccountService) container.getBean("target");
		proxy.bankAction();		
		System.out.println("------------");
		//第二个类中的方法代理
		com.briup.aop.autoProxy.MyTest t= (MyTest) container.getBean("test");
		t.bankTest(1);
		System.out.println(t.getClass());
	} catch (Exception e) {
		e.printStackTrace();
	}
}

图解:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值