I created a spring boot application(3.0.1). Then I connected the Postgres database and executed it. The application is up on server 8080 smoothly. then I added below-mentioned annotations.
#EnableAutoConfiguration
#ComponentScan
whole file,
package com.example.SpringRecap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#SpringBootApplication
#EnableAutoConfiguration
#ComponentScan
public class SpringRecapApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRecapApplication.class, args);
}
#GetMapping("/")
public void Employee() {
}
}
Now, below mentioned error are appeared,
Redundant declaration: #SpringBootApplication already applies #EnableAutoConfiguration
Redundant declaration: #SpringBootApplication already applies given #ComponentScan
Image of InteliJ:
If anyone knows the reason, please help me. If further information is needed to solve the problem please put a comment here.
Thank You.
below mentioned error are appeared: This means that you can remove #ComponentScan and #EnableAutoConfiguration.
As the documentation on spring mentioned, it is included in #SpringBootApplication:
Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single #SpringBootApplication annotation can be used to enable those three features, that is:
#EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
#ComponentScan: enable #Component scan on the package where the application is located (see the best practices)
#Configuration: allow to register extra beans in the context or import additional configuration classes
It is a check in the plugin of Intellij. Also see https://youtrack.jetbrains.com/issue/IDEA-177632
Related
I've written an interceptor to generate service logs for a SpringBoot Java Rest API. I have the code below to define the custom WebMvcConfigurer:
package com.gem.common.interceptors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
#Configuration
public class InterceptorConfig implements WebMvcConfigurer {
#Autowired
LoggerInterceptor logInterceptor;
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor);
}
}
I'd like to use this InterceptorConfig across different modules. Can I package it and use it or do I need to define it in each module?
I suppose with "other modules" you are asking if you could make that code available to other spring boot applications too?
If that's the case - then: yes you can package it in a jar and add it as a dependency to all your other modules. I'll post the way to do this just below, however - just to warn you - if it's just for that simple class, the solution is not going to be worth it.
In short, what you'd need to do is to create your own external artifact (this usually is done via maven or gradle. You create a new maven project for your shared code. It will need to depend on some base libraries so that you have your #Configuration annotation available. Put your class as described in that project, and create a file src/main/resources/META-INF/spring.factories file. There you'll need to point to that class.
Then you build that project and upload the resulting jar to a package repository. Once that's done, you can add it as a dependency to your project. At startup, Spring boot will find the spring.factories file and automatically include the classes that are mentioned there in its initialization.
Please also note, that this is just a high level explanation and you will need more details. Spring has good documentation on this use case and they also have a demo project to show this extension mechanism.
I am following this tutorial to build a basic application with Spring. It is working flawlessly as long as I follow this sub-directory structure:
└── src
└── main
└── java
└── hello
If I move my Application.java and ScheduledTasks.java classes out of the hello package I get the following error:
** WARNING ** : Your ApplicationContext is unlikely to start due to a `#ComponentScan` of the default package.
And a few seconds later, indeed...
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext#71fa8894: startup date [Wed Jan 18 22:19:12 CET 2017]; root of context hierarchy
My question is, why do I need to put my classes into a package? What use does it have? How can I avoid this error? Do I really need to use packages if it is a really simple application?
Put your java files again to hello package.
When a class doesn’t include a package declaration it is considered to be in the “default package”. The use of the “default package” is generally discouraged, and should be avoided.
It can cause particular problems for Spring Boot applications that use #ComponentScan, #EntityScan or #SpringBootApplication annotations, since every class from every jar, will be read.
Read more here.
I moved the class annotated with #SpringBootApplication from the default package to a specific package and it worked.
I had created a blank Maven folder where Java folder was the last one. I added class MainApplication.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Further, I had to create a package within Java folder such as com.test and then I moved my MainApplication class into it. Note "package com.test;" Now this default package Spring boot is looking for.
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Then it worked fine.
I updated my Spring Boot Application from 1.3.4 to 1.4.3 and refreshed the project using the Gradle Refresh. I now have an error in my main application class:
The project was not built since its build path is incomplete. Cannot
find the class file for java.lang.Object. Fix the build path then try
building this project BeverageDataServices Unknown Java Problem
The type java.lang.Object cannot be resolved. It is indirectly
referenced from required .class
files
BeverageDataServicesApplication.java /BeverageDataServices/src/main/java/com/boelter/beverage
Here is the main application class:
package com.xxx.beverage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;
#SpringBootApplication (exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class})
#ComponentScan(basePackages = "com.xxx.xxx")
#EntityScan("com.xxx.xxx.model")
#EnableScheduling
public class BeverageDataServicesApplication {
public static void main(String[] args) {
SpringApplication.run(BeverageDataServicesApplication.class, args);
}
}
It looks like the JRE was removed or not present in the Java Build Path ... thank you both for your quick insight!! I added the Java 8 JRE to the Build Path and it works... thanks
I'm trying to use a spring getting started project, and I have a problem I cannot figure out: when I move the restController from the default "hello" package to another one (say com.mydomain.controllers) I get a 404 error page. Any ideas how to solve this?
PS: I'm using intellij + gradle
In this case, this controller is located in a package that's not scanned by Spring. In the Application class:
package hello;
#SpringBootApplication
public class Application {
//...
}
The #SpringApplication annotation is a convenience annotation - see the full explanation in the guide itself.
If you want to scan other locations and customize more your configuration, you can change your application class to this:
package hello;
#Configuration
#EnableAutoConfiguration
#ComponentScan({"hello", "com.mydomain.controllers"})
public class Application {
//...
}
I'm using spring-boot and would like to automatically import src/main/resources/applicationContext.xml file.
So far it only works if I explicit tell spring to import it:
#EnableAutoConfiguration
#Configuration
#ImportResource({"classpath*:applicationContext.xml"})
But spring-boot has so many default, maybe someone knows the "default" name for the app.xml file so that is gets picked up by spring-boot by default?
There is no such feature for importing an XML configuration by default based on it's name or location.
Check out this part of the documentation.