springAOP实现(含实例)
需要用到的jar包:
1.XML方式实现:
package cn.lxc.post; public class Intermediary { public void post(){ System.out.println("该房源已发布!"); } }
package cn.lxc.service; public interface Rent { public void rent(); }
package cn.lxc.service.impl; import cn.lxc.service.Rent; public class Landlord implements Rent{ @Override public void rent() { System.out.println("房东:我要出租房子了!"); } }
package cn.lxc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.lxc.service.Rent; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); Rent r = ac.getBean(Rent.class); try { r.rent(); } catch (Exception e) { e.printStackTrace(); } } }
beans.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 目标对象 --> <bean id="rent" class="cn.lxc.service.impl.Landlord"/> <!-- 切面 --> <bean id="intermediary" class="cn.lxc.post.Intermediary"/> <aop:config> <aop:aspect ref="intermediary"> <aop:before method="post" pointcut-ref="pointcut"/> <aop:pointcut id="pointcut" expression="execution(* cn.lxc.service.impl.*.*(..))" /> </aop:aspect> </aop:config> </beans>
2.注解方式:
package cn.lxc.post; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class Intermediary { @Before("execution(* cn.lxc.service.impl.*.*(..))") public void post(){ System.out.println("该房源已发布!"); } }
applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 目标对象 --> <bean id="rent" class="cn.lxc.service.impl.Landlord"/> <!-- 切面 --> <bean id="intermediary" class="cn.lxc.post.Intermediary"/> <!-- 自动代理 --> <aop:aspectj-autoproxy/> </beans>
测试类:
package cn.lxc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.lxc.service.Rent; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Rent r = ac.getBean(Rent.class); try { r.rent(); } catch (Exception e) { e.printStackTrace(); } } }