最近在複習spring aop 的內容,一開始就想着用普通的java project起個demo練練手,但是遇到一對問題,導入jar包版本衝突,缺少部分jar包之類的一大堆問題。不禁感慨,maven是個好東西。
但是我這人有點認死理,看着bug就不爽,花了不少時間試錯,終於用普通的java project把一個aop的小例子跑起來了,還是挺開心的。
分享一波,需要的jar包,試錯了好久,不同版本之間差距還是挺大的,很容易衝突。
最難的不是寫代碼,而是正確導入jar包,哈哈哈哈
相關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"
default-autowire="byName"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="core" class="aop.Core"></bean>
<bean id="preCore" class="aop.PreCore"></bean>
<bean id="afterCore" class="aop.AfterCore"></bean>
<aop:config>
<aop:pointcut expression="execution(* aop.Core.*(..))" id="point"/>
<aop:aspect ref="preCore">
<aop:before method="preCore" pointcut-ref="point"/>
</aop:aspect>
<aop:aspect ref="afterCore">
<aop:after method="afterCore" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
然後直接啓動:
public class Start {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Core core = (Core) context.getBean("core");
core.core();
}
}