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.InvocationTargetException-->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)
at org.springframework.aop.framework.ProxyFactoryBean.getProxy(ProxyFactoryBean.java:342)
at org.springframework.aop.framework.ProxyFactoryBean.getSingletonInstance(ProxyFactoryBean.java:297)
at org.springframework.aop.framework.ProxyFactoryBean.getObject(ProxyFactoryBean.java:227)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectFromFactoryBean(AbstractBeanFactory.java:1227)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1198)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:262)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at com.fisher.spring.CustomerMain.main(CustomerMain.java:19)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
... 11 more
Caused by: java.lang.SecurityException: class "com.fisher.spring.Customer$$EnhancerByCGLIB$$f99489e3"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
... 17 more
希望哪位大虾解决 十分感谢
[解决办法]
提示创建before失败!
<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>
上述地方配置有问题,自己对照资料,仔细看看。
自己找到原因,能学到东西,别人告诉知识会用而已。
[解决办法]
<property name="target">
<ref bean="customer"/>
这里错了....
你看清楚了,是要给谁注入
[解决办法]
这个问题我解决了,
直接使用JDK的动态代理比较好。
这样,要代理的目标类必须实现一个接口。然后通过该接口来实现代理。