我是SpringAOP的新手。我想写一个简单的例子介绍,但不能清楚地知道它是如何工作的。SpringAOP引入AspectJ
在文档我发现:
我写简单的例子: 我写简单的类的一个方法
public class Test {
public void test1(){
System.out.println("Test1");
}
}
然后我写的接口和类实现此接口
public interface ITest2 {
void test2();
}
public class Test2Impl implements ITest2{
@Override
public void test2() {
System.out.println("Test2");
}
}
,最后我的方面
@Aspect
public class AspectClass {
@DeclareParents(
value = "by.bulgak.test.Test+",
defaultImpl = Test2Impl.class
)
public static ITest2 test2;
}
我的Spring配置文件是这样的:
<aop:aspectj-autoproxy/>
<bean id="aspect" class="by.bulgak.aspect.AspectClass" />
所以我的问题: 我哪有你现在这样。我需要在我的主课中写出什么样的海洋结果? 。 可能我需要写一些其他类(本书中,我读到SpringAOP我无法找到完整的例子)
UPDATE
我的主要方法是这样的:
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-configuration.xml");
Test test = (Test) appContext.getBean("test");
test.test1();
ITest2 test2 = (ITest2) appContext.getBean("test");
test2.test2();
}
当我执行我的应用程序我得到这个错误:
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy5 cannot be cast to by.bulgak.test.Test
在这一行:
Test test = (Test) appContext.getBean("test");
让我看看你的配置文件中的'Test' bean声明。 –
@RohitJain' ' –
@RohitJain但项目工作正常II评论这两行:'Test test =(Test)appContext.getBean(”test“); test.test1();' –