@Autowired注入其他模块的bean失败

编程入门 行业动态 更新时间:2024-10-27 00:33:41

@Autowired注入其他<a href=https://www.elefans.com/category/jswz/34/1771428.html style=模块的bean失败"/>

@Autowired注入其他模块的bean失败

一、在Spring Boot中,如果要自动扫描和创建bean,需要在应用的启动类上使用@ComponentScan注解来指定要扫描的包。确保你已经将GlobalConfig类所在的包和父包都添加到了@ComponentScan注解的value属性中。 例如,如果GlobalConfig类所在的包为com.demo.config,则可以使用以下方式来扫描整个包路径及其子包:

@SpringBootApplication
@ComponentScan(basePackages = "com.demo")
public class MyApp {public static void main(String[] args) {SpringApplication.run(MyApp.class, args);}
}

确保上述代码中的com.demo是你的包路径的根路径。如果com.demo.config是在com.demo包路径下的一个子包,那么它也应该被正确扫描到。 另外,请检查是否有其他方式明确定义了GlobalConfig的bean,如在XML配置文件中使用元素定义了该bean。如果有,请将其移除,并使用@ComponentScan注解来自动扫描bean。

二、使用applicationContext.xml文件配置

<beans xmlns=""xmlns:xsi=""xmlns:context=""xsi:schemaLocation=" .xsd  .xsd"><context:component-scan base-package="com.demo"/><bean id="globalConfig" class="com.demomon.config.GlobalConfig"><!--${platform.name}获取yml配置文件中platform的name的值--><property name="name" value="${platform.name}"/><property name="version" value="${platform.version}"/><property name="copyrightYear" value="${platform.copyrightYear}"/><property name="profile" value="${platform.profile}"/></bean>
</beans>

然后使applicationContext.xml生效
在启动类上添加注解

@ImportResource("applicationContext.xml")

更多推荐

@Autowired注入其他模块的bean失败

本文发布于:2023-11-15 07:41:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   Autowired   bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!