我在向我的简单项目添加 AOP 功能时出现以下错误,有人可以为我解释一下吗?我还在下面提到了代码的相关部分。
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.AOP.Car' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:346)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:337)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
at com.AOP.App.main(App.java:13)
package com.AOP;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.AOP")
public class AppConfig {
}
package com.AOP;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Car car = context.getBean(Car.class);
car.drive();
}
}
package com.AOP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Car implements Vehicle{
@Autowired
private Tyre tyre;
public Tyre getTyre() {
return tyre;
}
public void setTyre(Tyre tyre) {
this.tyre = tyre;
}
public void drive()
{
System.out.println("driving a car");
System.out.println(tyre);
}
}
package com.AOP;
public interface Vehicle {
void drive();
}
如果我在没有实现“Vehicle”接口的情况下得到一个简单的类“Car”,那么一切正常。但是添加该扩展名将导致 menterror。
冉冉说
郎朗坤
随时随地看视频慕课网APP
相关分类