【问题标题】:AOP using AspectJ not working with <aop:aspectj-autoproxy />使用 AspectJ 的 AOP 不能与 <aop:aspectj-autoproxy /> 一起使用
【发布时间】:2014-10-24 07:37:18
【问题描述】:

我是 Spring 框架的新手,正在尝试一些示例示例来理解 AOP,这是我到目前为止所做的,但它不起作用。

问题是,一旦我将&lt;aop:aspectj-autoproxy /&gt; 添加到spring.xml,我的构建就会失败,说无法创建带有空指针异常的bean。 但是如果我在没有&lt;aop:aspectj-autoproxy /&gt; 标签的情况下运行应用程序,那么它运行良好但没有 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 中注释 &lt;aop:aspectj-autoproxy /&gt; 行并运行它能够创建 bean 的代码。 请指导我我错过了什么吗?某些库还是某些库冲突?

&lt;aop:aspectj-autoproxy /&gt;有关

【问题讨论】:

  • 对于初学者,您正在混合 aspectj 版本,请不要这样做。接下来,您还需要 cglib,因为您没有接口,并且不会应用 jdk 动态代理。最后,您使用的是相当旧的 Spring 里程碑版本,我建议升级到 4.x 版本。

标签: spring spring-aop spring-aspects


【解决方案1】:

我认为您指的是来自https://javabrains.io/ 的代码,您的代码没有问题,因为我已经使用相同的代码完成了示例。

一开始我也遇到了同样的问题,后来我想出了解决办法。

1]你需要安装aspectj jar,安装后你会得到:

i)aspectjrt.jar ii)aspectjtools.jar iii)aspectjweaver.jar iv)org.aspectj.matcher.jar

2] 现在我们需要使用 aspectjrt.jar 和 aspectjweaver.jar

在您的代码中,您还包含了不需要的 aspectj.jar,还有一件事 aspectj.jar 和 aspectjrt.jar 版本不同不包括 aspectjrt.jar 和 aspectjweaver .jar 直接来自互联网。

安装aspectj.jar,然后包含aspectj的bin文件夹中的两个jar

【讨论】:

    【解决方案2】:

    请更改

     <aop:aspectj-autoproxy/>
    

    <aop:aspectj-autoproxy ></aop:aspectj-autoproxy>
    

    spring.xml 文件中。希望它能正常工作。

    【讨论】:

    • 它们是一样的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多