解释说明:
types-matching中匹配到的对象,相当于会给此类添加一个父类,其中的该父类的接口为implement-interface,实现类为default-impl,这样得到bean的对象后强转为该接口对象
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:declare-parents types-matching="com.wuyonghu.spring2.*(+)" implement-interface="com.wuyonghu.spring2.Fit" default-impl="com.wuyonghu.spring2.FitImpl"/>
</aop:aspect>
</aop:config>
接口:
public interface Fit {
void filter();
}
接口实现类:
public class FitImpl implements Fit {
public void filter() {
System.out.println("這是FitImpl中的filter方法執行了");
}
}
业务类:
@Service
public class AspectBiz {
}
测试类:
@Test
public void testFit() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//此时得到aspectBiz对象,且转换为Fit后,就可以调用filter方法了
Fit fit =(Fit) context.getBean("aspectBiz");
fit.filter();
}