在XML中申明切面

代码示例:
Performance接口

public interface Performance {
	void perform(String performContent);
}

Performance实现类

@Component
public class Concert implements Performance {
	@Override
	public void perform(String performContent) {
		System.out.println(performContent + "表演中。。。");
	}
}

切面类

public class Audience {
	public void performance(String performContent) {};
	
	public void sliencingPhones(String performContent){
		System.out.println(performContent);
		System.out.println("Sliencing cell phones");
	}
	
	public void watchPerformance(ProceedingJoinPoint jp) {
		try {
			System.out.println("Taking seats");
			jp.proceed();
			System.out.println("CLAP CLAP CLAP!!!");
		} catch (Throwable e) {
			System.out.println("Demand a refund");
			e.printStackTrace();
		}
	}
}

新功能接口

public interface Encoreable {
	void performEncore();
}

新功能接口实现

public class DefaultEncoreable implements Encoreable {
	@Override
	public void performEncore() {
		System.out.println("Encoreable performEncore!!!");
	}
}

XML配置

<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context.xsd">
	
	<context:component-scan base-package="com.zachary.aop"/>
	
	<bean id="defaultEncoreable" class="com.zachary.aop.DefaultEncoreable"/>
	<aop:config>
		<!-- <aop:around pointcut="execution(com.zachary.aop.Performance.perform(..))" method="watchPerformance"/> -->
		<aop:pointcut id="performance"
               expression="execution(* com.zachary.aop.Performance.perform(..)) &amp;&amp; args(performContent)" />
		<aop:aspect ref="audience">
			
			<aop:before 
				pointcut-ref="performance"
				method="sliencingPhones"/>
			
          	<aop:around 
            	pointcut-ref="performance"
                method="watchPerformance"/>
            
            <aop:declare-parents 
            types-matching="com.zachary.aop.Concert" 
            implement-interface="com.zachary.aop.Encoreable"
            delegate-ref="defaultEncoreable" />
       <!-- <aop:declare-parents 
            types-matching="com.zachary.aop.Concert" 
            implement-interface="com.zachary.aop.Encoreable"
            default-impl="com.zachary.aop.DefaultEncoreable" /> -->
		</aop:aspect>
	</aop:config>
	
	<aop:aspectj-autoproxy />
</beans>

测试

@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(classes = ConcertConfig.class)
@ContextConfiguration(locations = "classpath:aop-config.xml")
public class ConcertConfigTest {
	@Autowired
	private Performance concert;
	
	@Test
	public void test() {
		concert.perform("perform1");
		Encoreable encoreable = (Encoreable) concert;
		encoreable.performEncore();
	}
}

Output:
pperform1
Sliencing cell phones
Taking seats
perform1
perform1表演中。。。
CLAP CLAP CLAP!!!
Encoreable performEncore!!!

通过切面引入新功能在xml中的两种形式:

 <aop:declare-parents 
  types-matching="com.zachary.aop.Concert" 
    implement-interface="com.zachary.aop.Encoreable"
    delegate-ref="defaultEncoreable" />
<!-- <aop:declare-parents 
    types-matching="com.zachary.aop.Concert" 
    implement-interface="com.zachary.aop.Encoreable"
    default-impl="com.zachary.aop.DefaultEncoreable" /> -->

问题总结
报错内容:Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut

报错原因:
切点定义为execution(* com.zachary.aop.Performance.perform(…))
sliencingPhones有一个String类型的参数,pointcut定义是没有定义导致报错。

“*”’后面没有空格,也会导致类似的报错。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值