Spring Boot:从库项目自动装配bean [英] Spring Boot: autowire beans from library project

查看:330
本文介绍了Spring Boot:从库项目自动装配bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力从我的自定义库(通过gradle导入)自动装配bean. 阅读了几个类似的主题之后,我仍然找不到解决方法.

I'm struggling to autowire beans from my custom library, imported with gradle. after reading couple of similar topics I am still unable to find solution.

我有一个Spring Boot项目,该项目依赖于其他项目(我的自定义库以及Components,Repositories等...).这个库是一个Spring不可运行的jar,主要由域Entities和Repository组成.它没有可运行的Application.class和任何属性...

I have Spring Boot project that depends on other project(my custom library with Components, Repositories etc...). This library is a Spring non-runnable jar, that consists primarily from domain Entities and Repositories. It doesn't have runnable Application.class and any properties...

启动应用程序时,我可以看到我的'CustomUserService'bean(来自库)正在尝试初始化,但是自动装配到其中的bean无法加载(接口UserRepository)...

When I starting the application I can see that My 'CustomUserService' bean(from the library) is trying to be initialized, but the bean autowired in it failed to load (interface UserRepository)...

错误:

构造函数的参数0 com.myProject.customLibrary.configuration.CustomUserDetailsS​​ervice 需要一个类型的bean 'com.myProject.customLibrary.configuration.UserRepository'不能 被发现.

Parameter 0 of constructor in com.myProject.customLibrary.configuration.CustomUserDetailsService required a bean of type 'com.myProject.customLibrary.configuration.UserRepository' that could not be found.

我什至尝试设置'Order',显式加载(使用'scanBasePackageClasses'),使用包和标记类进行扫描,添加其他'EnableJPARepository'注释,但无济于事...

I've even tried to set 'Order', to load it explicitly(with 'scanBasePackageClasses'), scan with packages and marker classes, add additional 'EnableJPARepository' annotation but nothing works...

代码示例(为简单起见,更改了软件包名称)

Code example(packages names were changed for simplicity)

package runnableProject.application;

import runnableProject.application.configuration.ServerConfigurationReference.class
import com.myProject.customLibrary.SharedReference.class

//@SpringBootApplication(scanBasePackages = {"com.myProject.customLibrary", "runnableProject.configuration"}) 
//@EnableJpaRepositories("com.myProject.customLibrary")  

@SpringBootApplication(scanBasePackageClasses = {SharedReference.class, ServerConfigurationReference.class})   
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

库中的类:

package com.myProject.customLibrary.configuration;

import com.myProject.customLibrary.configuration.UserRepository.class;

@Service
public class CustomUserDetailsService implements UserDetailsService {
    private UserRepository userRepository;    

    @Autowired
    public CustomUserDetailsService(UserRepository userRepository) {
        this.userRepository = userRepository;       
    }
...

package myProject.customLibrary.configuration;

@Repository
public interface UserRepository extends CustomRepository<User> {
    User findByLoginAndStatus(String var1, Status var2);

    ...
}

推荐答案

刚刚找到了解决方案. 我没有定义要从单独的库进行扫描的基本包,而是仅在该库中创建了带有全部注释的配置类,并将其导入到我的主MyApplication.class中.

Just found the solution. Instead of defining base packages to scan from separate library, I've just created configuration class inside this library with whole bunch of annotation and imported it to my main MyApplication.class:

package runnableProject.application;    

import com.myProject.customLibrary.configuration.SharedConfigurationReference.class

@SpringBootApplication
@Import(SharedConfigurationReference.class)
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

package com.myProject.customLibrary.configuration;

@Configuration
@ComponentScan("com.myProject.customLibrary.configuration")
@EnableJpaRepositories("com.myProject.customLibrary.configuration.repository")
@EntityScan("com.myProject.customLibrary.configuration.domain")
public class SharedConfigurationReference {}

这篇关于Spring Boot:从库项目自动装配bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆