gpt4 book ai didi

java - Spring - 延迟注入(inject)

转载 作者:太空宇宙 更新时间:2023-11-04 06:21:25 26 4
gpt4 key购买 nike

我正在使用 Spring 4.0 版本并学习 Java 中的 DI。我有一个 Shape 接口(interface)和一个实现它的类:

@Component
@Lazy
@Scope(value="prototype")
public class Circle implements Shape {
public Circle() {
System.out.println("Ctor Circle");
}
@Override
public double GetArea() {
// TODO Auto-generated method stub
return 2.0;
}
}

和一个简单的类,它将在注入(inject)中获得形状

@Service
@Lazy
public class ShapeHolder {
@Autowired
//@Lazy
private Shape cShape;

public ShapeHolder() {
System.out.println("Ctor shapeHolder");
}
}

当字段上方的 @Lazy 被注释时,所有功能都正常工作,但是当它没有注释时,我会遇到异常

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shapeHolder': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.base.Interfaces.Shape com.base.services.ShapeHolder.cShape; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

我做错了什么吗?我希望注入(inject)是真正的 Lazy。

最佳答案

存在java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice,spring惰性初始化依赖于aopalliance jar

如果使用maven,则需要添加以下依赖。

<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>

关于java - Spring - 延迟注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27377718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com