简体   繁体   English

Spring:不断收到 java.lang.ClassNotFoundException:org.springframework.expression.ParserContext

[英]Spring: Keep getting java.lang.ClassNotFoundException: org.springframework.expression.ParserContext

I am new to Spring and I just made a small program using annotations but I keep getting the error mentioned in the title and "AnnotationConfigApplicationContext prepareRefresh".我是 Spring 的新手,我刚刚使用注释做了一个小程序,但我不断收到标题和“AnnotationConfigApplicationContext prepareRefresh”中提到的错误。

Employee.java员工.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主.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 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");
}}

Why do you want to make things complicated?你为什么要把事情复杂化? Simply create individual beans either using @Component, @Service, @Repository or @Configuration annotated classes.只需使用@Component、@Service、@Repository 或@Configuration 注释类创建单个bean。 Autowire them at any place you need.在您需要的任何地方自动连接它们。 Instead of having main method implement AnnotationConfigApplicationContext, why not use @SpringBootApplication over your application class, get the ApplicationContext directly.与其让主方法实现 AnnotationConfigApplicationContext,不如直接在应用程序 class 上使用 @SpringBootApplication,直接获取 ApplicationContext。

Also, in your code, you have Employee class annotated with @Component, and then again creating a bean for Employee within your configuration class.此外,在您的代码中,您有使用 @Component 注释的 Employee class,然后在您的配置 class 中再次为 Employee 创建一个 bean。

You might want to look into the below representation for your code.您可能想查看以下代码的表示形式。

@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 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassNotFoundException: org.springframework.expression.ParserContext Spring HelloWorld? - java.lang.ClassNotFoundException: org.springframework.expression.ParserContext Spring HelloWorld? java.lang.ClassNotFoundException:org.springframework.expression.ExpressionParser - java.lang.ClassNotFoundException: org.springframework.expression.ExpressionParser Spring java.lang.ClassNotFoundException:org.springframework.context.ApplicationContext - spring java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext 使用spring-web.jar获取java.lang.ClassNotFoundException:org.springframework.web.filter.DelegatingFilterProxy - Getting java.lang.ClassNotFoundException: org.springframework.web.filter.DelegatingFilterProxy with spring-web.jar present 迁移Spring引导1.5到2.0,无法启动应用程序获取:java.lang.ClassNotFoundException:org.springframework.boot.bind.RelaxedPropertyResolver - Migrated Spring boot 1.5 to 2.0, unable to start app getting: java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver Spring,Tomcat 6和ANT Build Scripts:java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener - Spring, Tomcat 6, and ANT Build Scripts: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.core.annotation.MergedAnnotations - java.lang.ClassNotFoundException: org.springframework.core.annotation.MergedAnnotations java.lang.ClassNotFoundException: org.springframework.core.io.Resource - java.lang.ClassNotFoundException: org.springframework.core.io.Resource java.lang.ClassNotFoundException:org.springframework.servlet.DispatcherServlet - java.lang.ClassNotFoundException: org.springframework.servlet.DispatcherServlet java.lang.classnotfoundexception org.springframework.web.servlet.dispatcherservlet - java.lang.classnotfoundexception org.springframework.web.servlet.dispatcherservlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM