#ComponentScan is not applied - all Java packages are scanned - spring

I have a Spring Boot app with several dependencies:
#ComponentScan({"com.test.adapter",
"com.dependency.commons.framework.util"}
)
But during startup I see that call packages from dependency jar are applied and I get conflicts.
Do you know how I can apply only Java package: "com.dependency.commons.framework.util"?

Solution A
Use the attribute excludeFilters of #ComponentScan.
#ComponentScan(basePackages = { "com.test.adapter",
"com.dependency.commons.framework.util" },
excludeFilters = #ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.example.ignore.packages.*"))
Solution B
As your title of the question is
#ComponentScan is not applied - all Java packages are scanned
Probably your launcher class that has the #SpringBootApplication is on a upper level of a package. Spring will automatically scan all the classes that are on that level of where this class is placed and all nested subpackages.
See the following structure as an example
What you can do here to avoid that is to move the launcher class inside a specific package outside of the root package

Related

SpringBoot: #EnableJpaRepositories, basePackageClasses ... repositories not found?

I have a simple Spring Boot project, that depends on a Library where some entities and repositories (JpaRepository) are defined. Additionally, I defined some repositories inside the project. All the repositories should be used inside one service.
In the Application.class I use:
#SpringBootApplication
#EntityScan(basePackageClasses = DatabaseConnectorsCoreMarker.class)
#EnableJpaRepositories(basePackageClasses =
{ThisApplication.class, DatabaseConnectorsCoreMarker.class})
In other projects we used the same but with only one basePackageClasses. That worked out. However, in the present case we always get errors like:
Field field_associated_to_repository in service_name required a bean of type repository_class that could not be found.
Where repository_class are repository interfaces defined in ThisApplication.

Spring configuration in test overriden by regular configuration

I am having an issue where I've created a Configuration class for my source code, and a separate configuration class for my test package. I'd like some beans created differently in test than in other environments. However, when I run a build here's what's happening: (I have a bean called filesystem that gives me a virtual filesystem in test)
Overriding bean definition for bean 'fileSystem' with a different definition: replacing <testVersion from MockAppInjector> with <realVersionFromAppInjector>
I my test package I have
#Configuration
#ImportResource("classpath:/META-INF/fig-batch/spring-bootstrap.xml")
#ComponentScan(basePackages = "com.company")
class MockAppInjector {...}
and in my regular source package I have
#Configuration
#ImportResource("classpath:/META-INF/fig-batch/spring-bootstrap.xml")
#ComponentScan(basePackages = "com.company")
public class AppInjector {...}
And in my test I have #ContextConfiguration(classes = MockAppInjector.class)
Finally, all my xml has in it is <context:component-scan base-package="com.company" /> and a call to another xml that configures some data sources.
The problem is any beans defined in XML have more priority than beans defined in Java configuration for some reason. So if you test beans are defined in Java configuration it's expected behavior.
To solve it you have to either put your test beans into XML file and put it in #ImportResource after your prod XML file or mark your beans in Java configuration with #Primary annotation.
The problem was that my Test configuration class was finding my real one. So I added a filter
#ComponentScan(basePackages = "com.vanguard", excludeFilters = #ComponentScan.Filter(value=Configuration.class, type = FilterType.ANNOTATION))
And removed the component scan from the xml, since it was superfluous. Now everything gets loaded one time.

Spring Boot #SpringBootApplication Annotation is not working for classes in different packages

I am developing a sample SpringBoot Application. I have two packages
1. com.A( in which main class annotated with #springbootApplication is there)
2. com.B(other spring beans).
Now My query is : Spring beans which are in package B are not getting scanned because of that application failing.I tried using
a. #springbootapplicatio(scanBasePackages="com.B")
b. Also #componentScan(..)
c. Used #EnableConfiguration also.
However If I move B package beans under A package,then everything works fine(because #springbootapplication takes care of that).
Please help me to resolve this!
Rahul Kumar
Your configuration class containing the #SpringbootApplication annotation will scan all the classes in the same package in which it is present . You can mention the base package name with the annotation and try it ..
Something like below:
#SpringBootApplication(scanBasePackages = {"com.basepackage"})
This should work:
#SpringBootApplication(scanBasePackages = {"com.A", com.B"})

ComponentScan and Autowired not working for dependent Spring project?

I have two projects A and B. Both are built with Maven, and project A has a Maven dependency to project B. Both project have a class with #Configuration annotation where I define #Beans.
I have beans in project A, from both projects. If I use the #Autowired annotation in project A of a bean that is defined in same project, the autowiring works. However, if I use the #Autowired annotation in project A of a bean from project B, I will get an exception.
What does this mean? How can I autowire a bean in project A, that is defined in project B?
This is normally an issue with the base classpath on ComponentScan.
If you for example have the following base packages
com.myproject.a
and
com.myproject.b
in your project A and B respectively, and you're using SpringBoot with the main class
package com.myproject.a
#Configuration
#EnableAutoConfiguration
#ComponentScan
class MyApp {
// Some public static void main ...
}
it will only find your classes in the package com.myproject.a and it's children.
To solve this issue you must enhance the #ComponentScan in a way that it scans both package structures, eg.
package com.myproject.a
#Configuration
#EnableAutoConfiguration
#ComponentScan(basePackages = {"com.myproject.a", "com.myproject.b"}
// or basePackages = "com.myproject" in this example
class MyApp {
// Some public static void main ...
}

Spring Boot w/ JPA: move #Entity to different package

I'm having trouble with my first steps using Spring-Boot with JPA. I've started with a pretty minimalistic example from Git using Gradle.
Now simply moving Customer to another package, let's say to hello2 results in an exception Caused by: java.lang.IllegalArgumentException: Not an managed type: class hello2.Customer. I tried to add
#ComponentScan(basePackageClasses= {Customer.class}) // AND OR
#EnableJpaRepositories(basePackageClasses= {Customer.class})
to Application, but without success.
What am I doing wrong?
Location of entities in Spring Boot can be configured using #EntityScan.
By default, #EnableAutoConfiguration enables entity scanning in the package where it's placed (if it's not a default package).
You must locate entities and repositories pakages by using
#EnableJpaRepositories(basePackages = "your.repositories.pakage")
#EntityScan(basePackages = "your.entities.pakage")
this is what worked for me :
#EnableJpaRepositories(basePackages ={ "package1","package2"})
#EntityScan(basePackages ={ "package3","package4"})
Giving same package location (i.e base package) for below annotation worked for me :-
#SpringBootApplication(scanBasePackages = {"org.ashu.java.*"})
#EnableJpaRepositories(basePackages ={ "org.ashu.java.*"})
#EntityScan(basePackages ={ "org.ashu.java.*"})

Resources