spring boot #Autowired a bean from another module - spring-boot

My question is how can i add a package to my list of component to scan #ComponentScan(basePackages = {"io.swagger", "com.company.project", like add it here }), but this package is in another module in my project,
here is the structure of my project :
springbootProject (maven project)/
module1(mavenProject, com.company.module1)
pom1.xml
module2(mavenProject, com.company.module2)
pom2.xml
pom.xml
in module 2 i have my main (#SpringbootAplication) where i want to #Autowired myRepository witch is in module 1
so how can i add the path

Import ModuleB on ModuleA, and you'll be able to use it.
Project
|__ Module A (com.test.a)
|__ Module B (com.test.b)
In pom.xml on ModuleA, add:
<dependency>
<groupId>com.test</groupId>
<artifactId>b</artifactId>
<version>1.0</version>
</dependency>
Then you should be able to add:
#ComponentScan(basePackages = {"com.test.b"})

Related

Exclude entire class if no starter present

Is there a way to exclude a class if there is no Spring Boot starter-X in pom. For example, if someone forgets to import a dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>-->
and I have a component that creates a dependency object on that starter. Is there a way to exclude the entire class and complain that there is no such file/class. I would like to compile the rest of the application without this class? Something like
#EnabledIf(value = "${is-present}")

Why importing file from another project module not working?

I have maven project with 2 modules.
root-folder:
- sub-module1:
.... - src
.... - pom.xml
-sub-module2:
.... - src
.... - pom.xml
- pom.xml
For example, i have some Test1 class from sub-module1. And in sub-module2 i have another class Test2. Trying to use Test1 in Test2, but second module cannot resolve import. What is wrong here?
In the main pom.xml, you need to add modules like below
<modules>
<module>artifact-id-of-module-1</module>
<module>artifact-id-of-module-2</module>
</modules>
And in the pom.xml of the second module, you need to add dependency like this
<dependency>
<groupId>parent-group-id</groupId>
<artifactId>artifact-id-of-module-1</artifactId>
<version>${parent.version}</version>
</dependency>
This will enable you to access all the class of module1 in module 2.

In a Maven project, a class in folder src/main/java can't resolve import org.openqa.selenium.WebElement

I have a maven project for Selenium tests project. All the necessary dependencies were defined in pom.xml like
selenium-java
webdrivermanager
assertj-core
testng
maven-surefire-plugin
maven-failsafe-plugin
slf4j-api
slf4j-simple
Now, I wrote a helper class, which has to serve as a common utility for the selenium tests located in folder src/test/java. This class has one static method with WebElement as a parameter for that I tried to import org.openqa.*.*classes. I am getting 'import org.openqa can't be resolved'
If I keep the class/method inside a selenium test class with in
src/test/java folder it is resolving well. Why error is specific to
src/main/java folder classes ?
Just adding the comment in answer . To use the selenium dependency in classes defined in src/main/java folder Make sure scope is defined for project.
`<scope>test</scope>`
Remove above scope from selenium dependency
`<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.5</version>
</dependency>
`
#Rahul L comment answered the question. I should be marking it as an answer.

NPE on autowiring from dependent Project

This is my project structure,
Parent Projectt -->
pom.xml
(sub modules core,web,services)
core prj -->
pom.xml
web prj --> has core,services dependency and has ComponentScan for com.aaa.bbb
pom.xml
services prj --> has core, web dependency
pom.xml
client Project -->
pom.xml (sub modules xxx,yyy)
xxx -->
pom.xml
yyy --> has core dependency
pom.xml
xxx and yyy jars are used in core project and war is built by web project.
when i autowire services/repositories in yyy project i am getting NPE for services/repositories.
Can any one please let me know how i can autowire services/repositories in yyy project from core project services/repositories?
Thank you,
To be able to autowire the bean from library, you have two options:
If you have access to the library code (I think this is your case):
1.1 Add annotation to mark which class is bean (using #Component, #Service,...)
1.2 In your configuration class (class which have #Configuration or #SpringBootApplication or #ComponentScan...), add path of xxx and yyy project to the classpath
#Configuration
#ComponentScan("com.my.package.xxx,com.my.package.yyy")
public class YourApplication
If you don't have access to the library code
2.1 In your configuration class, create a #Bean annotated method and initialize your bean here.
#Bean
public XRepository getXRepository() {
return new XRepositoryImpl(); // Here is your initialization logic
}

multi module maven project module expose as soap web service

I have a multi module maven-spring project. Following is the structure-
ParentService
---ChildService-service
---ChildService-core
---ChildService-web
---ChildService-wsClient
---ChildService-mongo
I have created a new module called ChildService-wsService where I will write methods and expose as Axis2 SOAP web service. I have been able to write independent methods in classes of this module project and expose as service but I want to call methods of ChildService-service module.
When I try to call methods of ChildService-service module it gives me errors like NoClassDefFoundError.
Following is sample code--
public class HelloWorld {
#Autowired
private ITestService iTestService;
#Autowired
ICommonService commonService;
public String getVal(String s){
return s+"...testing...";
}
public String getValFfmService(){
iTestService=new TestServiceImpl();
return iTestService.test();
}
I am getting error as following--
Error: java.lang.NoClassDefFoundError: com/service/test/business/ITestService at java.lang.Class.forName0
If I include following line in class then also I am getting classnotfound error.
extends SpringBeanAutowiringSupport
You need to include your other projects that you need as dependencies, for instance, in your pom.xml:
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ChildService-service</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
...
</dependencies>
Then you need to install these dependencies in your local repository (mvn clean install will do it), or, in Eclipse, you need to activate Workspace resolution (right click on your module, Maven > Enable Workspace Resolution).

Resources