Spring AOP 前置运行示例 遇到一个问题
这是源码
//CustomerMain.java
package com.fisher.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class CustomerMain {
public static void main(String[] args) {
ClassPathResource resource=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
System.out.println(">>>>>>>>>>>>>>>>>>>创建Customer之前");
Customer customer=(Customer)factory.getBean("before");
System.out.println(">>>>>>>>>>>>>>>>>>>创建Customer之后");
customer.buy();
}
}
//Welcome.java
package com.fisher.spring;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class Welcome implements MethodBeforeAdvice {
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("Welcome to out shop ........");
}
}
//Customer.java
package com.fisher.spring;
public class Customer {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void buy(){
System.out.println("Customer "+this.name+" buy something........");
}
}
配置文件:
//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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="customer" class="com.fisher.spring.Customer">
<property name="name">
<value>Gates</value>
</property>
</bean>
<bean id="welcome" class="com.fisher.spring.Welcome">
</bean>
<bean id="before" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="customer"/>
</property>
<property name="interceptorNames">
<list>
<value>welcome</value>
</list>
</property>
</bean>
</beans>
异常:
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'before': FactoryBean threw exception on object creation;
nested exception is org.springframework.aop.framework.AopConfigException: Couldn't generate CGLIB subclass of class [class com.fisher.spring.Customer]: Common causes of this problem include using a final class or a non-visible class; nested exception is net.sf.cglib.core.CodeGenerationException:
java.lang.reflect.InvocationTargetException-->null
Caused by: org.springframework.aop.framework.AopConfigException: Couldn't generate CGLIB subclass of class [class com.fisher.spring.Customer]: Common causes of this problem include using a final class or a non-visible class; nested exception is net.sf.cglib.core.CodeGenerationException: java.lang.reflect.
InvocationTargetException-->null
Caused by: net.sf.cglib.core.CodeGenerationException: java.lang.reflect.Invocation
TargetException-->null
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:196)