【发布时间】:2014-10-24 07:37:18
【问题描述】:
我是 Spring 框架的新手,正在尝试一些示例示例来理解 AOP,这是我到目前为止所做的,但它不起作用。
问题是,一旦我将<aop:aspectj-autoproxy /> 添加到spring.xml,我的构建就会失败,说无法创建带有空指针异常的bean。
但是如果我在没有<aop:aspectj-autoproxy /> 标签的情况下运行应用程序,那么它运行良好但没有 AOP。
以下是我的项目的一些细节。
这里我有 AopMain,它是运行的主类, LoggingAspect 我在哪里实际定义了 before aspect 有两个模型圆形和三角形 实际使用上述两种模型的ShapeService
这里给他们代码
AopMain:
package com.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.ShapeService;
public class AopMain {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring.xml");
ShapeService shapeService = ctx.getBean("shapeService", ShapeService.class);
System.out.println(shapeService.getCircle().getName());
}
}
日志方面:
package com.spring.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(public String getName())")
public void loggingAdvice(){
System.out.println("Advice run, Get method called");
}
}
圈子:
package com.spring.model;
public class Circle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
三角形:与圆相同,属性名称单一
形状服务:
package com.spring.service;
import com.spring.model.Circle;
import com.spring.model.Triangle;
public class ShapeService {
private Circle circle;
private Triangle triangle;
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
public Triangle getTriangle() {
return triangle;
}
public void setTriangle(Triangle triangle) {
this.triangle = triangle;
}
}
现在重要的文件 Spring.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy />
<bean name="triangle" class="com.spring.model.Triangle">
<property name="name" value="Triagnle Name"/>
</bean>
<bean name="circle" class="com.spring.model.Circle">
<property name="name" value="Circle Name"/>
</bean>
<bean name="shapeService" class="com.spring.service.ShapeService" autowire="byName" />
<bean name="loggingAspect" class="com.spring.aspect.LoggingAspect"/>
</beans>
收到如下错误:
Error creating bean with name 'triangle' defined in class path resource [Spring.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException
但是,如果我尝试在 Spring.xml 中注释 <aop:aspectj-autoproxy /> 行并运行它能够创建 bean 的代码。
请指导我我错过了什么吗?某些库还是某些库冲突?
与<aop:aspectj-autoproxy />有关
【问题讨论】:
-
对于初学者,您正在混合 aspectj 版本,请不要这样做。接下来,您还需要 cglib,因为您没有接口,并且不会应用 jdk 动态代理。最后,您使用的是相当旧的 Spring 里程碑版本,我建议升级到 4.x 版本。
标签: spring spring-aop spring-aspects