有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Spring:继续使用java。lang.ClassNotFoundException:org。springframework。表示ParserContext

我是Spring的新手,我刚刚使用注释编写了一个小程序,但我不断遇到标题和“AnnotationConfigApplicationContext prepareRefresh”中提到的错误

雇员。爪哇

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 + "]";
}

梅因。爪哇

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);
}}

雇员配置。爪哇

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");
}}

共 (1) 个答案

  1. # 1 楼答案

    你为什么要把事情复杂化?只需使用@Component、@Service、@Repository或@Configuration注释类创建单个bean。在任何需要的地方自动连线。与其让main方法实现AnnotationConfigApplicationContext,不如在应用程序类上使用@SpringBootApplication,直接获取ApplicationContext

    此外,在代码中,您的Employee类用@Component注释,然后再次在配置类中为Employee创建一个bean

    您可能需要查看下面代码的表示形式

    @SpringBootApplication public class DemoApplication
    {
    
        public static void main(String[] args)
        {
            ApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
            SomeService service = applicationContext.getBean(SomeService.class);
            service.doSth(args);
        }
    
    }
    
    @Service
    @ConditionalOnBean({EnableConfiguration.class, Employee.class})
    class SomeService {
    
        @Autowired
        private Employee employee;
    
        public void doSth(String[] args){
            System.out.println(employee.toString());
        }
    }
    
    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 + "]";
        }
    }
    
    @Configuration
    public class EnableConfiguration
    {
    
        @Bean
        public Employee getEmployee() {
            return new Employee(101,"Pradhyumn","91223123222","Analyst");
        }
    }
    

    pom。xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.0</version>
            <relativePath/> <!  lookup parent from repository  >
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>