【问题标题】:Spring Boot project not autowiring the services in another spring mvc projectSpring Boot 项目没有自动装配另一个 spring mvc 项目中的服务
【发布时间】:2018-05-24 12:47:46
【问题描述】:

我有两个 Spring boot 和 spring mvc 项目。我想得到spring mvc项目的服务。 Spring Boot 项目作为客户端工作。我添加了 spring mvc 项目名称作为 spring boot 项目中 pom.xml 的依赖项。然后我可以调用spring mvc项目的方法,但是里面的服务没有自动装配。

当我在调试时查看以下服务时它们为空。

@Autowired
    PriceResultService priceResultService; --null

    @Autowired
    PriceFactorService priceFactorService; --null

    @Autowired
    OptionalExtrasService optionalExtrasService; --null

    @Autowired
    BaseCommonRulesService baseCommonRulesService; --null

    @Autowired
    BasePartyTypeRulesService basePartyTypeRulesService; --null

    @Autowired
    MedicalCommonRulesService medicalCommonRulesService; --null

    @Autowired
    OptionalExtraRulesService optionalExtraRulesService; --null

    @Autowired
    CoverLimitRulesService coverLimitRulesService; --null

如何解决,

【问题讨论】:

    标签: spring spring-mvc spring-boot


    【解决方案1】:

    您必须添加mvc 模块才能扫描类路径。

    @SpringBootApplication(scanBasePackageClasses = {YourSpringBootApplication.class, YourMVCClassInRootPackage.class})
    public class YourSpringBootApplication {
    
    }
    

    假设您的 MVC 模块如下所示:

    /com/path/
       DummyBean.class
    /com/path/child1/
       AnotherBean.class
       AnotherBeanX.class
       AnotherBeanY.class
    /com/path/child2
       AnotherBean2.class
       AnotherBeanZ.class
    

    那么你选择YourMVCClassInRootPackage就是DummyBean

    如果你的 MVC 模块看起来像:

    /com/path/child1/
       AnotherBean.class
       AnotherBeanX.class
       AnotherBeanY.class
    /com/path/child2
       AnotherBean2.class
       AnotherBeanZ.class
    

    最顶层的包中没有类,因此您要么在顶层包中创建DummyBean.java(如/com/path/DummyBean),要么包含所有AnotherBeanAnotherBean2 以扫描类路径。 (为每个包选择一个类)

    【讨论】:

    • YourSpringBootApplication 类是指我的spring boot项目的主类吗?
    • 是的。对于 YourMVCClassInRootPackage,您必须选择 Mvc 模块根包中的 bean 类
    • 如何识别spring mvc项目中的bean类。
    • 对不起,我没有权利这样做。无论如何假设如果我在包结构的 Repository 或 Service 顶部添加任何类名可以吗?
    • 我很高兴它有帮助
    猜你喜欢
    • 1970-01-01
    • 2017-06-08
    • 2017-03-19
    • 1970-01-01
    • 2020-08-23
    • 2016-08-13
    • 2020-09-03
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多