您的位置:首页 > 编程语言 > ASP

使用spring的aspectj框架实现aop和使用基于aspectj的传统aop开发会产生冲突吗?

2018-08-19 16:01 543 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/JamesLoganHowlett/article/details/81838735

小弟不才,最近学习spring框架的aop实现,然后在进行练习时分别使用spring框架的aspectj实现aop(以下简称框架),基于aspectj的传统aop开发(以下简称传统),我将他们都配置在了同一颗applicationContext.xml文件中,结果发现,如果当传统和框架两种配置同时存在的时候,传统的配置可以生效正常运行,框架的会报错,如果将传统的配置注释掉,框架的就可以正常运行,对此小弟很疑惑,想请教下大神们这是为什么?下面附上配置文件;

aop1.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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean name="userTarget" class="cn.itheima.aspectj.IUserImpl"></bean>
<bean name="userAdvice" class="cn.itheima.aspectj.UserAdvice"></bean>
<aop:config>
<aop:aspect ref="userAdvice">
<aop:pointcut expression="execution(* *.add(..))" id="MyCut"/>
<aop:before method="before" pointcut-ref="MyCut"/>

</aop:aspect>

</aop:config>

</beans>

aop2.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:c="http://www.springframework.org/schema/c"
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 name="helloWorld1" class="exercise.HelloWorldImo1"></bean>
<bean name="helloWorld2" class="exercise.HelloWorldImo2"></bean>

<bean name="proxyHello" class="exercise.ProxyHello"></bean>

<bean name="helloWorldPointCut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedNames">
<list>
<value>doPrint</value>
<value>printHelloWord</value>
</list>
</property>
</bean>
<bean name="helloWorldAspectj" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="proxyHello"></property>
<property name="pointcut" ref="helloWorldPointCut"></property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>

</beans>

spring的配置文件,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:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<import resource="./aop1.xml"/>
<import resource="./aop2.xml"/>

</beans>

百思不得其解,求大神指点!!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐