解决 SpringMvc 非controller类使用@Autowired注解 service注入为null的问题
2018-02-02 13:44
921 查看
原地址:http://blog.sina.com.cn/s/blog_98721cfd0102xj96.html
使用SpringMVC框架,在开发的过程中有一些工具类、静态非controller类需要调用由spring管理的service层。但是使用@Autowired注解注入Service,会报null异常;搜索了下网上的一些方法,又实际测试了下,发现网上说的还缺少了点东西,所以自己总结下写下来作为学习笔记吧,也希望能帮助下其他遇到这个问题的童鞋~好了下面说下具体实现步骤:
1、在配置文件(springmvc.xml)中加入工具类所在包的扫描配置,我一开始按照网上的说法改造时就是因为这里没有配置,所以一直是null。"com.zzzy.bms.util"是我的工具类所在的包路径
2、改造工具类,这里的代码是拷贝的网上其他大神的,我稍微加了下注释
@Component// 将工具类声明为spring组件,这个必须不能忘
public class TestUtils {
@Autowired
private ItemService itemService;
@Autowired
// private ItemMapper itemMapper;
// 静态初使化当前类
public static TestUtils testUtils;
// 在方法上加上注解@PostConstruct,这样方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。
@PostConstruct
public void init() {
testUtils = this;
}
// utils工具类中使用service或mapper接口的方法例子,用"testUtils.xxx.方法" 就可以了
public static void test(Item record) {
// 调用service的方法
testUtils.itemService.insert(record);
}
}
好了,以上就是我的改造过程~我在自己的项目中测试通过了。
还有一些其他的方法,这边也记录下,不过没有测试。觉得不太符合springmvc的使用习惯啊:
//applicationContext-service.xml为声明service的配置文件名
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-service.xml");
//"userService"为配置文件中定义的service的ID,
UserService uService = (UserService) ac.getBean("userService");
使用SpringMVC框架,在开发的过程中有一些工具类、静态非controller类需要调用由spring管理的service层。但是使用@Autowired注解注入Service,会报null异常;搜索了下网上的一些方法,又实际测试了下,发现网上说的还缺少了点东西,所以自己总结下写下来作为学习笔记吧,也希望能帮助下其他遇到这个问题的童鞋~好了下面说下具体实现步骤:
1、在配置文件(springmvc.xml)中加入工具类所在包的扫描配置,我一开始按照网上的说法改造时就是因为这里没有配置,所以一直是null。"com.zzzy.bms.util"是我的工具类所在的包路径
2、改造工具类,这里的代码是拷贝的网上其他大神的,我稍微加了下注释
@Component// 将工具类声明为spring组件,这个必须不能忘
public class TestUtils {
@Autowired
private ItemService itemService;
@Autowired
// private ItemMapper itemMapper;
// 静态初使化当前类
public static TestUtils testUtils;
// 在方法上加上注解@PostConstruct,这样方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。
@PostConstruct
public void init() {
testUtils = this;
}
// utils工具类中使用service或mapper接口的方法例子,用"testUtils.xxx.方法" 就可以了
public static void test(Item record) {
// 调用service的方法
testUtils.itemService.insert(record);
}
}
好了,以上就是我的改造过程~我在自己的项目中测试通过了。
还有一些其他的方法,这边也记录下,不过没有测试。觉得不太符合springmvc的使用习惯啊:
//applicationContext-service.xml为声明service的配置文件名
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-service.xml");
//"userService"为配置文件中定义的service的ID,
UserService uService = (UserService) ac.getBean("userService");
相关文章推荐
- 解决非controller使用@Autowired注解注入为null问题
- SSM SpringMVC 非Controller类使用@Autowired注解Service为null的解决办法
- 解决非controller使用@Autowired注解注入为null问题
- @webservice 注解的类中使用@Autowired 进行bean注入,注入的bean调用时为空的问题
- servlet中利用spring的注解@Autowired自动注入service失败,优雅的解决方法
- 解决SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题
- SpringBoot集成Quartz(解决@Autowired空指针Null问题即依赖注入的属性为null)
- activiti解决实现ExecutionListener spring 自动注入@Autowired为null问题
- Spring 使用注解注入,调用类实例出现 java.lang.NullPointException 问题的解决方法
- JAVA解决在@autowired,@Resource注入为null的问题
- 静态方法使用@Autowired注解报null问题
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- 静态 方法中使用Autowired注解的service或dao的问题
- 关于Struts在Spring中使用autowired 自动注入的问题
- 实际编写中的注解问题,@Autowired位置写错,报错java.lang.NullPointerException
- Spring与springmvc整合,@Autowired注解的类被创建两次的问题。
- spring@Autowired注入为null的问题,2017年9月14日21点41分记录
- springmvc框架定时器功能实现和service注入问题解决
- 使用dubbo注解@Service注册服务后使用aop或者申明式事物导致无法注册的问题解决办法