spring autoconfiguration class is missing in META-INF/spring.factories - java

I'm trying to create spring based application but after the build i'm getting exception while initializing the spring context -> No auto configuration classes found in META-INF/spring.factories.
I'm working heavily with spark in my application and i'm forced to use maven-assembly-plugin to package my jar (otherwise i'm unable to run spark job).
sample of my main class:
#SpringBootApplication
#EnableAutoConfiguration
public class MyMainClass {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(MyMainClass.class).web(false)
.run(args);
SparkJob job = ctx.getBean(SparkJob.class);
job.prepareJobAndRun();
ctx.close();
}
}
when i add
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mypackage.MyMainClass
everything works as expected, but i don't want to add them manually.
Any chance to make this work without the spring-boot-maven-plugin?

I was able to found out, that you can add your own META-INF/spring.factories to src/main/resources. This custom spring.factories will be then packed to jar. Tested, working.

Related

How to disable all Kafka related auto configuration from yaml/properties file in spring-boot-2 without removing dependencies?

I have create a spring-boot-2 gradle project, also in build.gradle file i have added Kafka related dependency which given below.
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-zipkin'
compile 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
}
now i want to disable all Kafka related Auto configuration from application.yaml
file for that i have tried given below code in my yaml file.
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
After implementing above things still the Kafka got Auto-configured and start integration of Kafka with the application.
Also i have tried below code but this is also not working for me.
#SpringBootApplication
#EnableAutoConfiguration(exclude = KafkaAutoConfiguration.class)
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
Now please can any one help me out, how can i disable all auto configuration related to kafka from yaml/properties file ?
Thanks,
Instead of #EnableAutoConfiguration(exclude = KafkaAutoConfiguration.class)
You should do #SpringBootApplication(exclude = KafkaAutoConfiguration.class)

Spring boot - exclude group of config classes

I'm new to Spring Boot so sorry if this is too basic. My code takes a lot of time to build. I figured out that it's because Spring is loading a lot of config files that are not really needed.
My main file :
#EnableSwagger2
#SpringBootApplication
#IntegrationComponentScan
#EnableIntegration
public class MySystem {
public static void main(String[] args) {
run(MySystem.class, args);
}
}
I tried using #EnableAutoConfiguration(exclude = {classes to exclude}) but the unused classes imported by Spring boot on build time are too much to include here individually, there are hundreds of them. Is there a way to exclude unused config files in batch or in group? Or maybe I'm doing something wrong?

Spring Boot Autowiring of beans is not working in maven multi module project

While modularising our project into different independent maven projects using spring boot and maven, we have came across a issue where autowiring of beans in multi module maven project is not working.
Just to give you an overview of the issue, below are the independent maven projects developed so far
Coreservices – Contains spring boot domain objects of whole application : Output JAR
DBservices1-Contains spring boot repositories and services(Database Services) to access database : Output JAR
Rewards -Contains Rewards module related files(Controllers, services(Business Logic Services), Views) : Output JAR
RewardsApp- Independent deployable maven project : Output WAR
Below is the dependency structure
RewardsApp-> Rewards -> DBservices1 -> Coreservices
The problem is #Autowired annotation used in Rewards and DBservices1 to fetch the mapped services annotated with #Service/#Repository are not available in RewardsApp Project.
As a workaround we have configured the beans in RewardsApp with #Bean annotation, then the services are available to the server to start successfully.
With this approach we need to manually configure all the beans in RewardsApp used in dependent projects.
We have many services and repositories in our application and we think creating beans like this not a proper way as many beans need to be created.
Please note that we have created all the spring boot controllers,services,repositorys across all projects under
package com.company.application
Below is the snippet of main class:
#SpringBootApplication
#ComponentScan(basePackages = {"com.company.application"})
public class RewardsApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(RewardsApp.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RewardsApp.class);
}
}
/**
*Manual beans in RewardsApp
**/
#Bean
public SomeService someService()
{
return new SomeService();
}
By adding below annotation in RewardsApp.java did the trick for me, now autowiring was working for the classes inside the jars
#ComponentScan(basePackages = {"com.company"})
#EntityScan(basePackages = {"com.company"})
#EnableJpaRepositories(basePackages = {"com.company"})
I guess above are for Services,Entities(Domains),Repositories
How about having a configuration class (with relevant comp scans) for each module and importing those configs into your application class?
#SpringBootApplication
#ComponentScan(...)
#Import({RewardsContext.class, DBservicesContext.class})
...
Import docs here

What is the difference between spring cloud config server exec.jar and .jar suffix

We are currently using spring cloud config server spring-cloud-config-server-1.1.0.M2-exec.jar and would like to upgrade to the latest version. However, i've noticed that beyond 1.1.0.M2 version there's only standard jars and no exec.jar in Maven repo http://maven.springframework.org/milestone/org/springframework/cloud/spring-cloud-config-server/
Could someone please explain what the difference is? Will I be able to just substitute the exec one with the standard non exec one?
Thanks
The exec jar contains an executable version of the config server (as a Spring Boot application). The non-exec jar contains only the config server classes. So you can't just replace the exec jar with the other one. What you basically have to do is to create a basic Spring Boot application with the config server dependencies and the appropriate annotations (like in the example):
#SpringBootApplication
#EnableDiscoveryClient
#EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}

How can I access 'spring.application.name' when defined in bootstrap.properties?

I have the following spring-boot 1.4.2.RELEASE sample app
#SpringBootApplication
public class Application {
#Value("${spring.application.name}")
private String applicationName;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And I have the following configuration defined in bootstrap.properties:
spring.application.name=sample-app
When run it I get the following error:
java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.name' in string value "${spring.application.name}"
Any hint on why it fails to inject 'spring.application.name'?
Need to define it there to support other spring boot cloud.
The first answer is correct. The default properties file is application.properties or application.yml.
The bootstrap file is properly for Spring Cloud.
See http://projects.spring.io/spring-cloud/spring-cloud.html#_the_bootstrap_application_context
If you are using spring cloud, and the bootstrap file is not working, you need to enable the "cloud" Spring profile.
For example using:
./gradlew -Dspring.profiles.active=cloud bootrun
or
./mvnw spring-boot:run -Dspring.profiles.active=cloud
By default, if you don't specify any properties source, spring boot will lookup for your property in the file application.properties. Therefore, you should rename your property file to that default name or manually specify a properties source for your bootstrap.properties

Categories

Resources