解决非controller使用@Autowired注解注入报错为java.lang.NullPointerException问题

在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在controller层中注入service接口,在service层中注入其它的service接口或者mapper接口都是可以的,但是如果我们要在我们自己封装的Utils工具类中或者非controller普通类中使用@Autowired注解注入Service或者Mapper接口,直接注入是不可能的,因为Utils使用了静态的方法,我们是无法直接使用非静态接口的,当我们遇到这样的问题,我们就要想办法解决了。

我们有两种方法解决这个问题,第一种是注解方式,第二种是xml配置方式,下面是我们在utils中使用@Autowired注解的方法:

@Component
public class TestUtils {
    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemMapper itemMapper;

    public static TestUtils testUtils;

    @PostConstruct  //用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化
    public void init() {
        testUtils = this;
    } 

    //utils工具类中使用service和mapper接口的方法例子,用"testUtils.xxx.方法" 就可以了
    public static void test(Item record){
        testUtils.itemMapper.insert(record);
        testUtils.itemService.queryAll();
    }
}

第二种方法就是xml配置的方式,也是很简单的,我们可以把init()方法上的@PostConstruct注解去掉,在spring-comtext.xml中配置以下bean就好了,里面什么内容都不用写,是不是很简单?

<bean id="testUtils" class="这里写utils类的包全路径名" init-method="init"></bean>

原文地址:https://www.cnblogs.com/dukedu/p/11801561.html

时间: 2024-07-31 12:01:57

解决非controller使用@Autowired注解注入报错为java.lang.NullPointerException问题的相关文章

解决非controller使用@Autowired注解注入为null问题

在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在controller层中注入service接口,在service层中注入其它的service接口或者mapper接口都是可以的,但是如果我们要在我们自己封装的Utils工具类中或者非controller普通类中使用@Autowired注解注入Service或者Mapper接口,直接注入是不可能的,因为Utils使用了静态的方法,我们是无法直接使用非静态接口的,当我们遇到这样的问

解决kylin build cube第一步报错:java.lang.NullPointerException

报错栈: 2017-06-19 10:27:35,757 ERROR [pool-9-thread-4] threadpool.DefaultScheduler:140 : ExecuteException job:933bc47a-302c-48fa-8ec9-ae8730057175 org.apache.kylin.job.exception.ExecuteException: org.apache.kylin.job.exception.ExecuteException: java.la

struts2 报错:java.lang.NullPointerException: Source must not be null

今天使用strut2+ajax进行异步上传时出错,控制台没有打印信息,ajax回调函数中alert返回值得到如下信息,大概就是空指针的意思. 实际上是上传的input name值和action变量名不一致造成.input name为myfile,变量为myFile,一个字母的大小写造成的错误!注意这个地方! HTTP Status 500 - type Exception report message description The server encountered an internal

Weblogic 启动报错:java.lang.NoClassDefFoundError

Weblogic 启动报错:java.lang.NoClassDefFoundError  ####<2015-6-17 下午03时30分47秒 CST> <Error> <HTTP> <HDQSDOCMTAPP4> <zonyappserver> <[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS

对象逆序列化报错:java.lang.ClassNotFoundException

简单的想从保存的对象中又一次解析出对象.用了逆序列化,但是报错: java.lang.ClassNotFoundException: xxxxxxxxxxxx at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native

报错: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

描述:使用Hibernate登陆验证时故意输入没有的用户名,所产生的错误. 错误代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /*      * 根据客户的用户名查找客户信息      */     @Override     public User getUserByUserName(String userName) {         String hql="from User where userName=?";         Session s

Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderL

Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderL     严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.co

【原】tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决

现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig 解决: 把tomcat目录下的lib/servlet-api.jar文件拷贝到$JAVA_HOME/jre/lib/ext目录下,再启动tomcat问题解决

spring 切面织入报错:java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to...

报这个错,只有一个原因,就是转化的类型不对. 接口过父类的子类,在强制转换的时候,一定要用接口父类来定义. 代码示例: package com.luoluo.dao.impl; import java.sql.Connection; import java.sql.SQLException; import javax.annotation.Resource; import javax.sql.DataSource; import com.luoluo.dao.UserDAO; import co