简体   繁体   English

在 Spring 引导中无法解析上下文

[英]context cannot be resolved in Spring Boot

I am new to spring boot and facing this issue for a while now.我是 spring 引导的新手,现在面临这个问题一段时间。

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.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>Assignment</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>TestProject-3</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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>

File Structure:文件结构:

在此处输入图像描述

com.example.config.AppConfig.java --> com.example.config.AppConfig.java -->

package com.example.config; package com.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class AppConfig {

}

com.example.main --> com.example.main -->

package com.example.main;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.example.config.AppConfig;
import com.example.service.EmployeeService;

public class TestProject3Application {

    public static void main(String[] args) {

        
        ApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig.class);
        EmployeeService emp=context.getBean(EmployeeService.class);
        emp.display();
    }
}

com.example.service.AddressService --> com.example.service.AddressService -->

package com.example.service;

import org.springframework.stereotype.Service;

@Service
public class AddressService {
public String getDetails() {
    return "Electronic City";
}
}

com.example.service.EmployeeService --> com.example.service.EmployeeService -->

package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeService {
    
    @Autowired
    private AddressService add;
    
    public void display() {
        System.out.println(add.getDetails());
    }
}

And the error I am receiving is "context cannot be resolved", find the below image:我收到的错误是“无法解析上下文”,找到下图:

在此处输入图像描述

在此处输入图像描述

Your spring boot version may cause your jar package to be missing, you can enter this command to fix it.您的 spring 启动版本可能会导致您的 jar package 丢失,您可以输入此命令进行修复。

mvn dependency:purge-local-repository mvn 依赖:purge-local-repository

If the error persists, delete your (~/.m2/repository/org/springframework) folder and execute the command。如果错误仍然存在,请删除您的(~/.m2/repository/org/springframework)文件夹并执行命令。

Then run the mvn package command.然后运行mvn package命令。

It ran successfully now.现在它运行成功了。 I have not provided the package name of beans in @ComponentScan.我没有在 @ComponentScan 中提供 bean 的 package 名称。 That's it!而已!

You are using Spring Boot but try very hard to work around it.您正在使用 Spring 引导,但要非常努力地解决它。 To solve.来解决。

  1. Ditch your AppConfig class放弃您的AppConfig class
  2. Annotate TestProject3Application with @SpringBootApplication使用@SpringBootApplication注释TestProject3Application
  3. Move TestProject3Application to com.example so it will properly detect everythingTestProject3Application移至com.example以便正确检测所有内容
  4. Fix your main doing ApplicationContext ctx= SpringApplication.run(TestProject3Application.class, args);修复你的主要做ApplicationContext ctx= SpringApplication.run(TestProject3Application.class, args);
package com.example.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import com.example.service.EmployeeService;

@SpringBootApplication
public class TestProject3Application {

    public static void main(String[] args) {
        
        ApplicationContext ctx=SpringApplication.run(TestProject3Application.class, args);
        EmployeeService emp=context.getBean(EmployeeService.class);
        emp.display();
    }
}

Rebuild and run your application.重建并运行您的应用程序。 If you decide to use a framework then use it and don't work around it.如果您决定使用框架,请使用它并且不要绕过它。 Also follow the best practices for that framework or when deviating from them at least understand them..还要遵循该框架的最佳实践,或者在偏离它们时至少了解它们。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM