解决Springboot @Autowired 无法注入问题
2018-08-07 11:14
1236 查看
特别提醒:一定要注意文件结构
WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入。
1. 添加工具类获取在 Spring 中托管的 Bean
(1)工具类
package com.common; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * @program: IPC_1P * @description: 获取在spring中托管的bean * @author: johnny * @create: 2018-08-03 16:24 **/ public class SpringContextUtil { private static ApplicationContext applicationContext; // Spring应用上下文 // 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法 public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } public static Object getBean(String name, Class requiredType) throws BeansException { return applicationContext.getBean(name, requiredType); } public static boolean containsBean(String name) { return applicationContext.containsBean(name); } public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } public static Class getType(String name) throws NoSuchBeanDefinitionException { return applicationContext.getType(name); } public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); } }
(2)使用
1)程序启动时,实例化 SpringContextUtil
@SpringBootApplication public class WebappApplication { private static ApplicationContext applicationContext; public static void main(String[] args) { applicationContext = SpringApplication.run(WebappApplication.class, args); // SpringContextUtil springContextUtil = new SpringContextUtil(); springContextUtil.setApplicationContext(applicationContext); System.out.println("服务器启动测试!"); }
2)在使用 @Service 的方法中,通过@Autowired 注入,使用SpringcontexUtil 获取Bean上下文
@Autowired SenderService senderService; public class Package_State { @Autowired SenderService senderService; @Component private Package_State() { senderService = (SenderService)SpringContextUtil.getBean("senderService"); } }
总结
以上所述是小编给大家介绍的解决Springboot @Autowired 无法注入问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
相关文章推荐
- 如何解决SpringBoot JpaRepository @Autowired 没法自动注入的问题
- 解决Intellij Idea Spring Boot Mybatis @Autowired报错的问题
- 解决spring-boot 中 bean无法注入的问题
- Spring Boot @Autowired 没法自动注入的问题
- Spring Boot @Autowired 没法自动注入的问题
- mapper注入失败问题解决 {@org.springframework.beans.factory.annotation.Autowired(required=true)}
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- 解决spring boot websocket无法注入bean的问题
- 关于spring boot无法自动注入bean问题解决方案
- 解决Spring boot使用Quartz时Job中无法注入Bean 空指针异常问题
- Spring Boot @Autowired 没法自动注入的问题
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- activiti解决实现ExecutionListener spring 自动注入@Autowired为null问题
- SpringBoot学习——@Autowired自动注入报:could not be found问题的理解和解决方案
- 解决SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题
- 解决spring boot websocket无法注入bean的问题
- SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)
- Shiro解决无法注入Service问题(包括Spring MVC和Spring Boot)
- spring boot 中@Autowired注解无法自动注入的错误
- 解决非controller使用@Autowired注解注入为null问题