【发布时间】:2021-08-15 08:36:34
【问题描述】:
我是 Spring 新手,刚刚使用注释做了一个小程序,但我不断收到标题和“AnnotationConfigApplicationContext prepareRefresh”中提到的错误。
Employee.java
package com.springdemo;
import org.springframework.stereotype.Component;
@Component
public class Employee {
int id;
String name,phone,dept;
public Employee() {}
public Employee(int id,String name,String phone,String dept) {
this.id=id;
this.name=name;
this.phone=phone;
this.dept=dept;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", phone=" + phone + ", dept=" + dept + "]";
}
Main.java
package com.springdemo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(EmployeeConfig.class);
Employee emp = context.getBean(Employee.class);
System.out.println(emp);
}}
EmployeeConfig.java
package com.springdemo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages= {"com.springdemo"})
public class EmployeeConfig {
@Bean
public Employee getEmployee() {
return new Employee(101,"Pradhyumn","9057672243","Analyst");
}}
【问题讨论】:
-
通常这些错误是由于混合了来自不同版本的 Spring 的 jar。
-
如果这是一个 maven 项目,pom.xml 可能会有所帮助。或者,如果是单模块 gradle 项目,build.gradle 会有所帮助。请将它们也包括在问题中。
标签: java spring eclipse spring-boot gradle