- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我还是 Spring Framework 的初学者,所以我尝试在 Spring AOP 中编写一个有关“简介”的程序,但在编译时遇到错误。请在下面的concert
包中找到类:
PerformanceImp.java
package concert;
import org.springframework.stereotype.Component;
@Component
public class PerformanceImp implements Performance {
public void perform() {
System.out.println("This is the performance function");
}
}
Performance.java
package concert;
public interface Performance {
public void perform();
}
Encoreable.java
package concert;
public interface Encoreable {
void performEncore();
}
DefaultEncoreable.java
package concert;
import org.springframework.stereotype.Component;
@Component
public class DefaultEncoreable implements Encoreable {
public void performEncore() {
System.out.println("This is the performEncore function");
}
}
EncoreableIntroducer.java
package concert;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class EncoreableIntroducer {
@DeclareParents(value="concert.Performance+",
defaultImpl=DefaultEncoreable.class)
public static Encoreable encoreable;
}
ConcertConfig.java
package concert;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan("concert")
public class ConcertConfig {
}
主类:
Main.java
package concert;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ConcertConfig.class);
PerformanceImp pi = (PerformanceImp) context.getBean(PerformanceImp.class);
((Encoreable) pi).performEncore();
pi.perform();
}
}
我收到错误:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'concert.PerformanceImp' available
请问有什么帮助吗?
最佳答案
默认情况下,您无法访问实现 (PerformanceImp
),因为您启用了 AOP,它设置为目标接口(interface)而不是实现。如果您删除 EnableAspectJAutoProxy
,您会发现代码可以正常工作。
要更多地了解 AOP 定位的工作原理,请查看此 Spring Documentation
Spring AOP can also use CGLIB proxies. This is necessary to proxy classes rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes; business classes normally will implement one or more business interfaces. It is possible to force the use of CGLIB, in those (hopefully rare) cases where you need to advise a method that is not declared on an interface, or where you need to pass a proxied object to a method as a concrete type.
所以你有两个选择:
ApplicationContext
获取 bean 时获取接口(interface)。要执行第 2 点,请按如下方式修改注释:
@EnableAspectJAutoProxy(proxyTargetClass = true)
关于java - 没有可用的 'concert.PerformanceImp' 类型的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308666/
我还是 Spring Framework 的初学者,所以我尝试在 Spring AOP 中编写一个有关“简介”的程序,但在编译时遇到错误。请在下面的concert包中找到类: PerformanceI
我是一名优秀的程序员,十分优秀!