错误报告
一月 19, 2019 8:21:14 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7106e68e: startup date [Sat Jan 19 20:21:14 CST 2019]; root of context hierarchy
一月 19, 2019 8:21:14 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/aopfactory/beanfactory.xml]
模拟检查权限
Exception in thread "main" org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method [public abstract void com.aopfactory.UserDao.add()] on target [userDao]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:341)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at com.aopfactory.Proxy.invoke(Proxy.java:10)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy2.add(Unknown Source)
at com.aopfactory.chapter01Test.main(chapter01Test.java:16)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
... 7 more
UserDao接口
package com.aopfactory;
public interface UserDao {
public void add();
public void delete();
}
UserDaoImpl实现类
package com.aopfactory;
public class UserDaoImpl implements UserDao {
@Override
public void add() {
// TODO Auto-generated method stub
System.out.println("开始添加");
}
@Override
public void delete() {
// TODO Auto-generated method stub
System.out.println("开始删除");
}
}
Proxy切面类
package com.aopfactory;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class Proxy implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {
check_first();
Object obj = mi.proceed();
log();
return obj;
}
public void check_first() {
System.out.println("模拟检查权限");
}
public void log() {
System.out.println("模拟记录日志");
}
}
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-4.3.xsd">
<!-- 目标类 -->
<bean id="userDao" class="com.aopfactory.UserDaoImpl"></bean>
<!-- 切面类 -->
<bean id="myAspect" class="com.aopfactory.Proxy"></bean>
<bean id="userDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理类实现接口 -->
<property name="proxyInterfaces" value="com.aopfactory.UserDao"></property>
<!-- 织入对象 -->
<property name="target" value="userDao"></property>
<!-- 织入通知 -->
<property name="interceptorNames" value="myAspect"></property>
<property name="proxyTargetClass" value="false"></property>
</bean>
</beans>
chaptor1Test测试类
package com.aopfactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class chapter01Test {
public static void main(String args[]) {
String xmlPath="com/aopfactory/beanfactory.xml";
ApplicationContext applicationcontext=new
ClassPathXmlApplicationContext(xmlPath);
UserDao userdao=(UserDao) applicationcontext.getBean("userDaoProxy");
userdao.add();
userdao.delete();
}
}