Home > Enterprise >  context cannot be resolved in Spring Boot
context cannot be resolved in Spring Boot

Time:08-09

I am new to spring boot and facing this issue for a while now.

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:

enter image description here

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

package com.example.config;

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

@Configuration
@ComponentScan
public class AppConfig {

}

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

package com.example.service;

import org.springframework.stereotype.Service;

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

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:

enter image description here

enter image description here

CodePudding user response:

Your spring boot version may cause your jar package to be missing, you can enter this command to fix it.

mvn dependency:purge-local-repository

If the error persists, delete your (~/.m2/repository/org/springframework) folder and execute the command。

Then run the mvn package command.

CodePudding user response:

You are using Spring Boot but try very hard to work around it. To solve.

  1. Ditch your AppConfig class
  2. Annotate TestProject3Application with @SpringBootApplication
  3. Move TestProject3Application to com.example so it will properly detect everything
  4. Fix your main doing 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!.

CodePudding user response:

It ran successfully now. I have not provided the package name of beans in @ComponentScan. That's it!

  • Related