sách gpt4 ai đã đi

spring - 使用 AspectJ 的 AOP 不适用于

In lại 作者:行者123 更新时间:2023-12-05 08:13:02 28 4
mua khóa gpt4 Nike

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

问题是我一添加 对于 spring.xml,我的构建失败说无法创建具有空指针异常的 bean。但是如果我在没有 的情况下运行应用程序标记然后它运行正常但没有 AOP。

以下是我的项目的一些细节。 nhập mô tả hình ảnh ở đây

这里我有 AopMain,它是运行的主类, LoggingAspect 我实际上在哪里定义了before aspect 有两个模型 Circle 和 Triangle 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;

@Diện mạo
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 来了:



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">
















我得到如下错误:

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 中的行并运行能够创建 bean 的代码。 请指导我我错过了什么吗?某些库还是某些库冲突?

有关

1 Câu trả lời

我认为您指的是来自 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

关于spring - 使用 AspectJ 的 AOP 不适用于 ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26543451/

28 4 0
Bài viết được đề xuất: wordpress - add_menu_page 不适用于类内函数
Bài viết được đề xuất: ios - 从 iPhone 获取温度
Bài viết được đề xuất: nfc - Libnfc 如何读取NFC卡内容?
Bài viết được đề xuất: jsf -

recreates @ViewScoped bean on every request

行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com