org.springframework.boot.autoconfigure cannot be resolved in eclipse - spring

I imported the spring-boot-1.1.10.RELEASE.jar into my j2ee project.
But i can not found the class of autoconfigure.
Here is code:
import org.springframework.boot.autoconfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The Compile error:
The import org.springframework.boot.autoconfigure cannot be resolved

I had the same problem and I tried above solutions. But I have found a different issue.
In latest springframework, this org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder has been moved to
org.springframework.boot.jdbc.DataSourceBuilder. so you have to edit in import
And then you will notice that you have to use DataSourceBuilder.create(classLoader)
instead of new DataSourceBuilder(classLoader)

Go to your maven repository directory
For windows on below path
C:\Users\YourUser\.m2\repository\org\springframework\boot
Then delete spring-boot-autoconfigure folder.
Now go to eclipse and do maven update.

There is a copy/paste error - the code from the official documentation (https://spring.io/guides/gs/rest-service/) is:
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The code in the question is missing ".EnableAutoConfiguration" in the first line.

In case anyone else has this issue, I was able to solve this by deleting the spring-boot-autoconfigure artifacts from my local Maven repository and updating my project in Eclipse (which re-downloads the artifacts).

if you are using eclise then Right button on project -> Maven -> Update Project

If you are using IntelliJ IDEA Right click on project -> Maven -> Reimport
Also, try "Generate source and update folder" below the reimport if above method doesn't work. No need to delete any spring or maven folder or files.

Related

org.springframework.boot.SpringApplication import throwing error

I am working on this tutorial using Spring Tool Suites.
I've copied what it says and my code and pom look the same (copied for easy review)
package com.javatpoint.spring_boot_example_sts;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootExampleSts
{
public static void main(String[] args){
SpringApplication.run(SpringBootExampleSts.class, args);
}
}
My issue is that I am getting a red line under ' org.springframework.boot.SpringApplication. To my knowledge, this library is not deprecated and I can't seem to find any instance of it being moved.
Thanks in advance.
May be maven either maven dependency is not downloaded or your IDE has some issues.
You can try to restart your IDE.
If issue still persist, Please build using mvn clean package.

Errors right after creating Spring Boot Application in IntelliJ

After creating the Spring Boot application in IntelliJ IDEA, it has errors. I didn't edit the code. It can't find org.spring.boot.SpringApplication. I've tried to create the app several times but no effect. All needed plugins are in the IDE, switched on. Maybe someone already had the problem
Errors in code
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
Errors in console
Cannot resolve org.springframework.boot:spring-boot-autoconfigure:2.4.4
Cannot resolve jakarta.annotation:jakarta.annotation-api:1.3.5
Cannot resolve org.ow2.asm:asm:5.0.4
Cannot resolve org.junit.platform:junit-platform-engine:1.7.1
Cannot resolve com.fasterxml.jackson.core:jackson-core:2.11.4
Cannot resolve jakarta.xml.bind:jakarta.xml.bind-api:2.3.3
Cannot resolve org.apache.tomcat.embed:tomcat-embed-websocket:9.0.44
Cannot resolve com.fasterxml.jackson.core:jackson-databind:2.11.4
Cannot resolve org.glassfish:jakarta.el:3.0.3
Cannot resolve org.springframework.security:spring-security-web:5.4.5
Cannot resolve org.springframework.boot:spring-boot-test:2.4.4
Cannot resolve org.springframework:spring-aspects:5.3.5
Cannot resolve org.springframework:spring-web:5.3.5
Right click on the pom.xml and select Maven -> Reload Project

In springboot error sayingThe declared package "com.example.demo" does not match the expected package ""

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootEdurekaTrialApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootEdurekaTrialApplication.class, args);
}
}
[1]: https://i.stack.imgur.com/78XDP.png
I have shared the screenshot of project package in above image. I have created the class under same package but I don't know why I am getting error. please guide.
Check your directory structure once in the file system. It should contain these three folders com/example/demo and under demo the file should be present. Sometimes folder structure is created as a single folder com.example.demo instead of the com/example/demo(3 folders). This should be the root cause of the issue.
For me I got the same error. The first time around I put the "demo" folder inside another folder that I called "Spring Web". I deleted everything and unzipped "demo" directly to C: and after importing it into Eclipse the error did not appear again.
In my case, the directory structure is fine. It could be that the source path of the main application is pointing to a different directory than the default path such as "src/main/java". Since this file was automatically created by Spring Boot, there seemed to be no way to fix it.
The way I resolved this compiler error was to add a new class using a different name from scratch and then copy the original content into the new class before removing the old one. You can then rename the class afterward.

Spring Boot Application error: Exception in thread "main" java.lang.NoSuchMethodError

Why i'm getting this error while running simple SpringBoot Application.
Exception in thread "main" java.lang.NoSuchMethodError:
org.springframework.boot.SpringApplication.run(Ljava/lang/Class;[Ljava/lang/String;)Lorg/springframework/context/ConfigurableApplicationContext;
at
io.javabrain.springbootstarter.CourseApiApp.main(CourseApiApp.java:10)
package io.javabrain.springbootstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
It should be running successfully
For gradle project in gradle\wrapper\gradle-wrapper.properties file change distributionUrl variable to:
distributionUrl=https://services.gradle.org/distributions/gradle-4.7-bin.zip
If it is don't helps download project from https://start.spring.io/ and do the same.
If you use maven, just download maven project from https://start.spring.io/ . It should work fine.

Spring JUnit test getting NullPointerException with TestContext.retrieveContextLoaderClass(TestContext .java:197)

Using Helios, spring 3.0.5 (TestContext Framework) and JUnit 4.7. I am getting an initialization error indicating that it cannot find the ContextConfiguration. I ran ProcMon in the background and determined it is not apparently looking at all. I have tried the logical locations for the xml file to no avail. I am unclear of what I am doing incorrectly. Here is the code:
package com.hwcs.veri.agg.dao;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.hwcs.veri.jpa.License;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/JpaIntegrationTests-context.xml" })
#TransactionConfiguration( transactionManager = "transactionManager",
defaultRollback = true )
#Transactional
public class JpaIntegrationTests
extends AbstractTransactionalJUnit4SpringContextTests
{
#Autowired
protected LicenseDao licenseDao;
#Test
public void getLicenses()
{
List<License> licenses = this.licenseDao.getLicenses();
assertEquals( "Expecting 1 license from the query",
super.countRowsInTable( "product_schema.license" ),
licenses.size() );
}
}
Is there some particular step that needs to be done to run this as a JUnit test inside Eclipse?
First and foremost, set the log level for org.springframework.test.context to DEBUG. That should tell you everything that the Spring TestContext Framework (TCF) is doing.
Note that with your above configuration, the TCF will attempt to load your application context from classpath:/JpaIntegrationTests-context.xml (i.e., in the root of your classpath). So make sure that the JpaIntegrationTests-context.xml file in fact exists in the root of the classpath (e.g., /src/test/resources/JpaIntegrationTests-context.xml for a Maven project layout). For the Maven project layout, you need to make sure that /src/test/resources is configured as a source folder in your IDE.
If this doesn't help you solve your problem, post the DEBUG output from the log.
Regards,
Sam (author of the Spring TestContext Framework)
Quoting Java Project: Failed to load ApplicationContext
"From the Sping-Documentation: A plain path, for example "context.xml", will be treated as a classpath resource from the same package in which the test class is defined. A path starting with a slash is treated as a fully qualified classpath location, for example "/org/example/config.xml".

Resources