Spring纯简单java对象切面

来源:互联网 发布:程氏cms v4模板 编辑:程序博客网 时间:2024/05/12 01:39

1:applicationcontext03.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:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="   
             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 
             
<bean id="checkip" class="com.zn.service.impl.CheckipImpl"/>
<!-- 定义一个增强advice -->
<bean id ="checkHelper" class="com.zn.common.CheckHelperProxy"/>


<!-- 配置切点和通知--> 
<bean id ="checkHelperAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">     
 <property name="advice" ref="checkHelper"></property>     
 <property name="pattern" value=".*check"> </property>      
</bean> 


<!-- 自动代理配置 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> 


</beans>

2:测试类:MainTest03.java

package com.zn;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.zn.service.Checkipable;


public class MainTest03 {


public static void main(String args[]){
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationcontext03.xml");
Checkipable ck = (Checkipable)ct.getBean("checkip");
ck.check();
}
}

0 0
原创粉丝点击