Can't I @Autowire Bean, which is present in the dependent library library?

I have a Spring (Y) boot application that relies on a set of library files packaged as x.jar and is referred to as a dependency in pom.xml of the Y application.

x.jar has a bean named (User.java) Application Y has a java class named (Department.java)

While I try to Autowire an instance of User.java inside Department.java, I get the following error

Can't I @Autowire bean, which is present in the dependent Jar library?

Failed to create autowire field: private com.User user; nested exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualification bean of type [com.User] found for a dependency: at least 1 bean is expected that qualifies as a candidate for auto-bracing for this dependency. Dependency Annotations: {@ Org.springframework.beans.factory.annotation.Autowired (required = true)}

No qualified bean type [com.User] found for the dependency was found: at least 1 bean is expected that qualifies as a candidate for auto-attachment for this dependency. Dependency Annotations: {@ Org.springframework.beans.factory.annotation.Autowired (required = true)} **

Here is the code in the Spring Y 'boot application

package myapp; @Component public class Department { @Autowired private com.User user; //has getter setters for user } 

Here is the User.java code in the x.jar library

  package com; @Component @ConfigurationProperties(prefix = "test.userproperties") public class User { private String name; //has getter setters for name } 

This is a dependency entry for x.jar in pom.xml of application Y

  <groupId>com.Lib</groupId> <artifactId>x</artifactId> <version>001</version> </dependency> 

This is the main class in the application "Y"

 @Configuration @EnableAutoConfiguration @ComponentScan @EnableZuulProxy @EnableGemfireSession(maxInactiveIntervalInSeconds=60) @EnableCircuitBreaker @EnableHystrixDashboard @EnableDiscoveryClient public class ZuulApplication { public static void main(String[] args) { new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args); } } 

Both departments and the user are under different packages.

Decision. I applied the following 2 steps and now Autowiring is working fine.

Step 1: added the following class to the jar file

 package com @Configuration @ComponentScan public class XConfiguration { } 

Step 2. Imported this configuration class into the main project class Y

 @Configuration @EnableAutoConfiguration @ComponentScan @EnableZuulProxy @EnableGemfireSession(maxInactiveIntervalInSeconds=60) @EnableCircuitBreaker @EnableHystrixDashboard @EnableDiscoveryClient @Import(XConfiguration.class) public class ZuulApplication { public static void main(String[] args) { new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args); } } 
+8
java spring autowired
source share
1 answer

You will need to add the package names of your main class, and the User class is 100% sure, but most likely the User class is not included in the same package (or subpackage) of your main class. This means that scanning components will not pick it up.

You can get spring to look at other packages as follows:

 @ComponentScan(basePackages = {"org.example.main", "package.of.user.class"}) 
+7
source share

All Articles