我有一个模块/ jar我已经创建并用作util 库.我在那里创建了一个服务:
@Service
public class PermissionsService { ... }
Run Code Online (Sandbox Code Playgroud)
...这里有一个包在这里:com.inin.architect.permissions,在我的主应用程序中,我正在引用/加载这个jar(即设置为app的maven POM.xml文件中的依赖项)所以:
<dependency>
<groupId>com.inin.architect</groupId>
<artifactId>permissions</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在应用程序中我想使用该服务,如:
@Autowired
PermissionsService permissions
Run Code Online (Sandbox Code Playgroud)
在应用程序的弹簧设置中,我有这个:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.inin.generator", "com.inin.architect.permissions" })
public class WebConfig extends WebMvcConfigurerAdapter implements ServletContextAware { }
Run Code Online (Sandbox Code Playgroud)
但是,当我在tomcat下运行我的应用程序时,它抱怨PermissionsService没有bean:"org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型的限定bean ..."
那么,我怎样才能将lib从lib中带到我的应用程序中?当然有办法.你是否必须将库设置为一个完整的弹簧MVC应用程序,以便这可以工作?即你是否必须在lib中设置@Configuration和@ComponentScan?
我有一个Spring Boot应用程序(Y),它依赖于一组打包为x.jar的库文件,并在应用程序Y的pom.xml中作为依赖项提到.
x.jar有一个名为(User.java)的bean应用程序Y有一个名为(Department.java)的java类
当我尝试在Department.java中自动装配User.java的实例时,我收到以下错误
我不能@Autowire存在于依赖的Library Jar中的Bean吗?
无法自动装配字段:private com.User user; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[com.User]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
没有找到[com.User]类型的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}**
这是Spring Boot应用程序'Y'中的代码
package myapp;
@Component
public class Department {
@Autowired
private com.User user;
//has getter setters for user
}
Run Code Online (Sandbox Code Playgroud)
这是Library x.jar中User.java的代码
package com;
@Component
@ConfigurationProperties(prefix = "test.userproperties")
public class User {
private String name;
//has getter setters for name
}
Run Code Online (Sandbox Code Playgroud)
这是应用程序Y的pom.xml中x.jar的依赖项
<groupId>com.Lib</groupId>
<artifactId>x</artifactId>
<version>001</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是应用程序'Y'中的Main类
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableZuulProxy
@EnableGemfireSession(maxInactiveIntervalInSeconds=60)
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
public class ZuulApplication {
public static void main(String[] args) …Run Code Online (Sandbox Code Playgroud)