Spring boot Auto Configuration trying to load beans from dependent project - spring

Spring Boot is loading Beans from external dependent jars .
How can we stop Autoconfigration of loading Bean from dependent jar .
Below is the annotation used .
#Configuration
#SpringBootApplication(excludeName = {"com.test.core"})
#ComponentScan(basePackages = {"com.test.myhazelcast"})
#EnableJpaRepositories
public class BelHazelcastApplication { ...
I want Spring boot not to configure any bean com.test.core.* which is a dependent module .
But Take beans that are presenst in com.test.myhazelcast.* package .

The excludeName attribute of SpringBootApplication is used to exclude an autoconfiguration class by its name, not to exclude a package from component scanning.
Because you SpringBootApplication with the default for the packages to component scan, it will component scan from whatever package BelHazelcastApplication is in.
If this package is "com.test" then that will include components in "com.test.core".
I'd remove this line:
#ComponentScan(basePackages = {"com.test.myhazelcast"})
and change this:
#SpringBootApplication(excludeName = {"com.test.core"})
to be:
#SpringBootApplication(basePackages = {"com.test.myhazelcast"})

Related

Spring boot application properties are all read as null

In my application class I have
#SpringBootApplication(scanBasePackages = {"com.binance.bot.timeandsales", <some other packages>})
#Configuration
#EnableScheduling
public class BinanceTimeAndSalesApplication implements CommandLineRunner {
#Autowired
AggTradeEventSubscriptionListener aggTradeEventSubscriptionListener;
And in the bean class which is a non-public class in the same file I have
#Component
class AggTradeEventSubscriptionListener implements SubscriptionListener<AggregateTradeEvent> {
#Value("${binance.api}")
private String apiUrl;
which is coming as null.
In my application.properties I have
binance.api=https://fapi.binance.com
The application.properties in src/main/resources directory which is also appearing as the resources directory in the Intellij module configuration.
In my pom.xml I have
<properties>
<start-class>com.binance.bot.timeandsales.BinanceTimeAndSalesApplication</start-class>
</properties>
What am I doing wrong that the application properties are read as null?
Another weirdity is that in some other class in a different package, the properties are getting read. But not in the newer classes that I'm writing in the com.binance.bot.timeandsales package, which is specified as one of the packages to scan in the Configuration.
Try adding #Configuration to AggTradeEventSubscriptionListener.

How can I load additional properties file in a spring boot application packaged as a war?

I have a standard springboot web app. I want to load properties file that's not in the classpath. application.properties is in the classpath and is being read correctly.
I don't have an issue when I'm building a jar. I just put the .properties alongside the jar and it works. But when I package a war, I couldn't get the application to read the properties file .
You put the properties file parallel to application.properties file and load it in a class like this:
#PropertySource("classpath:foo.properties")
public class My class {
#Value( "${some.property}" )
String myProp;
}
You can using ClassPathResource. given Class for loading resources.
This is sample code for you
ClassPathResource resource = new ClassPathResource("/application/context/blabla.yml");
InputStream inputStream = resource.getInputStream();
File file = resource.getFile();
// using inputStream or file
ClassPathResource
You can use spring application.properties to have spring profiles and use the spring profiles to define separate properties for each environment as you like.You can even separate out the spring profiles in to different files like appication-dev.properties etc so that you can have each spring profile in different files.
You can read the properties by using #Configuration annotation :
#Configuration
#EnableConfigurationProperties(TestProperties.class)
public class MySampleConfiguration {
}
Here TestProperties.class is used to map the values from the property file or yaml .
Reference in detail : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

#Import fails to aggregate annotated config from dependent jar

I have a project setup where a common module (JPA.jar) containing Spring JPA configuration.
#Configuration
#EnableJpaRepositories({"com.db.jpa.repository"})
#EnableTransactionManagement
public class Jpa {
// ...
}
I intend to invoke the config from a webservice (spring boot) and have a config importing the JPA configuration from JPA.jar.
#Configuration
#Import(com.db.config.Jpa.class)
public class JpaApp {
}
This fails with following error:
Caused by: java.io.FileNotFoundException: class path resource [com/db/config/Jpa.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:51)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:88)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:75)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81)
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:731)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getRelated(ConfigurationClassParser.java:1007)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getAnnotationAttributes(ConfigurationClassParser.java:988)
at org.springframework.context.annotation.ConfigurationClassParser.collectImports(ConfigurationClassParser.java:536)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:509)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:300)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:635)
... 40 more
Unable to find any documentation that says this is illegal for Spring's #Import. However I see this is done for resources with #ImportResource, using a classpath prefix.
I can include a set of configs for each webservice component using the common JPA models and repos, but just wondering if aggregating #Configuration(s) specifically using #Import from dependency jars is possible.
Is it possible?
If illegal, is there any rationale to it.
Thanks in advance.
You can use #Import for configuration classes from other jars. I think that you are getting this error because your jar is not defined as dependency in your pom.xml (if you use maven of course), that's why Spring can't find it.

Change search location in Spring boot 1.3.0

I would like externalize the configuration of my aplication, I use Spring Boot 1.3.0, and I kwnow that the class ConfigFileApplicationListener has a default value DEFAULT_SEARCH_LOCATIONS. How can I change the source of my configuration, before this class load the default properties source?
You can use #PropertySource annotation on your configuration class (which is annotated #SpringBootApplication or #Configuration).
Like this:
#SpringBootApplication
#PropertySource(value = { "classpath:other.properties", "file:./other-external.properties" }, ignoreResourceNotFound = true)
public class MyApplication {
It will search first other.properties in your classpath and then looks the external file other-external.properties right outside of your jar or war file. Latest files overwrites values of other files (if they exists)
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details.

Autowire annotation is not working for different package

I am using spring3,struts2 and hibernate3 together.I have a samplemanager.java file in com.top package and another test.java file in net.top package.In this test.java file i tried to use the samplemanager.java using #Autowired.But it throws null pointer exception can any one tell me a solution for this.
You have to configure the component scan in your application context file for both packages:
<context:component-scan base-package="com.top.*, net.top.*" />
Check if your samplemanager class is annotated and if you scan the classpath for annotated beans in your configuration file. If it is not annotated you must define the bean in the configuration file.

Resources