Spring 使用AspectJ的AOP不与<;aop:aspectj autoproxy/>;
我是spring框架的新手,尝试了一些示例来理解AOP,下面是我到目前为止所做的,但它不起作用 问题是,只要我将Spring 使用AspectJ的AOP不与<;aop:aspectj autoproxy/>;,spring,spring-aop,spring-aspects,Spring,Spring Aop,Spring Aspects,我是spring框架的新手,尝试了一些示例来理解AOP,下面是我到目前为止所做的,但它不起作用 问题是,只要我将添加到spring.xml,我的构建就会失败,无法创建带有空指针异常的bean。 但是如果我运行的应用程序没有标记,那么它运行正常,但没有AOP。 以下是我的项目的一些细节。 这里我有一个AopMain,它是运行的主要类, LoggingAspect,其中我实际定义了aspect之前的 有两个模型:圆形和三角形 实际使用上述两种模型的ShapeService 这里我给他们的代码 Ao
添加到spring.xml,我的构建就会失败,无法创建带有空指针异常的bean。
但是如果我运行的应用程序没有
标记,那么它运行正常,但没有AOP。
以下是我的项目的一些细节。
这里我有一个AopMain,它是运行的主要类,
LoggingAspect,其中我实际定义了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>
但是如果我尝试在Spring.xml中注释
行并运行代码,它就能够创建bean。
请引导我,我是否遗漏了什么?是图书馆还是图书馆冲突
与
有关的内容请更改
<aop:aspectj-autoproxy/>
到
在spring.xml文件中。希望它能正常工作。我认为您引用的代码没有问题,因为我使用相同的代码完成了示例 起初,我也面临同样的问题,但后来我提出了解决方案 1] 您需要安装aspectjjar,安装后您将获得: 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版本不同不要直接从internet包括aspectjrt.jar和aspectjweaver.jar
安装aspectj.jar,然后包括aspectj的bin文件夹中的两个jar对于初学者来说,混合aspectj版本不会这样做。接下来,您还需要cglib,因为您没有接口,并且不会应用jdk动态代理。最后,您使用的是一个相当旧的Spring里程碑版本,我建议升级到4.x版本
<aop:aspectj-autoproxy/>
<aop:aspectj-autoproxy ></aop:aspectj-autoproxy>