【发布时间】:2018-07-25 01:22:19
【问题描述】:
我刚开始学习 Spring 并尝试制作一个基本的 Spring 程序,但我无法解决这个错误。下面是代码:
Student.java
@Component
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
配置类
@Configuration
@ComponentScan("springfirst1")
public class ConfigClass {
public Student getStudent(){
return new Student();
}
}
主类
public class SpringFirst1 {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext("ConfigClass.class");
Student s1 = context.getBean(Student.class);
s1.setName("Adam");
System.out.println(s1.getName());
}
}
错误:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [springfirst1.Student] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:318)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
at springfirst1.SpringFirst1.main(SpringFirst1.java:13)
【问题讨论】:
-
使用包声明更新 Java 类。调试问题会很有帮助
-
错误在 AnnotationConfigApplicationContext 参数中。检查文档并更正它,这将起作用。
标签: java spring spring-mvc