SSM SpringMVC 非Controller類使用@Autowired註解Service為null的解決辦法
在SSM專案開發中,遇到了一個問題,在非Controller類中使用@Autowired註解的Service類一直報NullPointerException,經過一番搜尋,由於這個專案情況比較特殊,網上的說的大部分解決方法基本大同小異,並不能解決我的問題,但有了一些解決問題的思路,做一個學習筆記。
解決思路:
1.考慮過在使用Service的類中,直接new一個service物件,但又遇到一個問題就是,如果你Service中使用的Dao也是通過@Autowired註解注入的,那麼如果你直接new service同樣會拋異常。
總結就是:如果你的Dao是使用@Autowired注入,那麼包括呼叫它的servcie都要進行@Autowired注入,否則之後的注入就會失敗。
2.下面是我的解決辦法,相信會有更好的辦法:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd" >
...
...
...
<!-- 在applicationContext使用配置Service -->
<bean class="com.wzzc.service.UserService" id="userService"></bean>
</beans>
要用到Service的類
public class GuacamoleTunnelServlet extends GuacamoleHTTPTunnelServlet {
//注意這裡不使用註解
private UserService userService;
@Override
public void init() throws ServletException {
//通過ApplicationContext載入userService
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
userService=(UserService) applicationContext.getBean("userService");
}
...
...
...
}
相關推薦
SSM SpringMVC 非Controller類使用@Autowired註解Service為null的解決辦法
在SSM專案開發中,遇到了一個問題,在非Controller類中使用@Autowired註解的Service類一直報NullPointerException,經過一番搜尋,由於這個專案情況比較特殊,網上的說的大部分解決方法基本大同小異,並不能解決我的問題,但有了
解決quartz的Job類中使用註解Service為null的辦法
專案中需要繼承Quartz框架。專案的環境是SSM框架,spring4.3,Quartz1.6版本,Intellij IDEA 2017.1 遇到的問題是:在quartz的Job中使用@Autowired自動注入service時候報錯,報service為nul
解決非controller使用@Autowired註解注入為null問題
在SpringMVC框架中,我們經常要使用@Autowired註解注入Service或者Mapper介面,我們也知道,在controller層中注入service介面,在service層中注入其它的service介面或者mapper介面都是可以的,但是如果我們要在我們自己封
解決 SpringMvc 非controller類使用@Autowired註解 service注入為null的問題
使用SpringMVC框架,在開發的過程中有一些工具類、靜態非controller類需要呼叫由spring管理的service層。但是使用@Autowired註解注入Service,會報null異常;搜尋了下網上的一些方法,又實際測試了下,發現網上說的還缺少了點東西,所以
spring mvc在非Controller類無法使用Service bean解決方案
1、思路 因為不是在@Controller類中,使用@Autowired註解是得不到Service類的,所以可以通過手動方式進行獲取。 2、配置(如果已經配置好了,並且能在@Controller中獲得service類可以跳過這個) 為了更好的區分,所以sp
SpringMvc中,普通類注入Service為null,解決方案
場景:使用Quartz定時器時,普通的java類需要注入spring的service類,在呼叫時報錯! 解決方式: /** * 定時獲取課程的service */ @Autowired protected QuartzGetCo
【SpringBoot】攔截器使用@Autowired注入介面為null解決方法
public class TokenInterceptor implements HandlerInterceptor { @Autowired private ITokenDao tokenDao; @Override public
Spring @Autowired註解在非Controller注入為null,Springboot @Reference注入為null解決方案
今天使用activiti的執行流程,使用dubbo想要去呼叫service,發現@Reference為null,研究了好久,嘗試直接連線dao層,注入的也為null。. 可能是我的這個不是controller裡面的 解決辦法 通過新增三個關鍵地方即可解決: 1、在cl
SpringMVC學習——Controller類的方法返回值
返回ModelAndView Controller類方法中定義ModelAndView物件並返回,物件中可新增model資料、指定view。之前我就已講過,在此並不過多贅述。 返回void 在Controller類方法形參上可以定義request和response,使用request或r
反射呼叫Service層方法,@Autowired註解失效。問題解決
反射呼叫方法時,裡面的dao層註解失敗。空指標異常。 原因很簡單,反射和new有點像。沒通過spring管理。 解決方案1: @Service public class service { @Autowired private Mapper mapper; //新
ssh中spring向Action類@Autowired注入service空指標問題解決
使用servlet注入時,上一篇博文已經解釋了,但是在使用ssh框架時,就不能用同樣的方法了。 下面是解決方案: (1)pom.xml檔案中有沒有引入struts2-spring-plugin包 <!-- https://mvnrepository.com/artifact/
spring 中自定義工具類 @Autowired引入service或者mapper為null
在SMM專案中,經常使用@Autowired引入service或者mapper,但是在自定的utils包下引入service或者mapper的時候,會出現null的情況,下面就來解決這個問題。 首先,我們在spring的配置檔案中加上 掃描utils工具類的配置 <context:
SSM整合html使用Ajax時,success獲取不到controller類的返回值的問題解決。
使用ajax對資料庫的執行刪除,資料可以正常刪除,就是controller方法可以執行,但是Ajax的success卻接收不到controller類刪除方法的返回值。卻一直執行Ajax的error。 我的ajax是這樣寫的: $.ajax({
SSM在普通Java類裡使用service、dao等
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationConte
【SpringMVC】@Controller和@RequestMapping註解說明
一、@Controller註解 1、說明: 用於指示Spring類的例項是一個控制器,控制器可以支援同時處理多個請求動作。 2、保證Spring能找到控制器: (1)在SpringMVC的配置檔案的標頭檔案中引入spring-context (2)使用
SpringMVC Controller中的service為null
整合SSH框架的時候遇了Controller中的service為null的問題,結果發現純屬配置檔案錯誤。 錯誤條件: 在Tomcat下啟動通過url請求到Controller層。 applicationContext.xml檔案中的配置如下 自認為沒有錯,而且自己也
SpringMVC中Controller的@ResponseBody註解分析
需求分析:需要 利用 out 物件返回給財付通是否接收成功 。那麼將需要如下程式碼: /** * 返回處理結果給財付通伺服器。 * @param msg: Success or fail。 * @throws IOExcepti
用反射判斷實體類的屬性是否為null(附帶註解版本)
這個是用來驗證表單傳來的json物件裡是否必填,如果值為null或者“”時返回屬性的名字 程式碼 /** * java反射機制判斷物件所有屬性是否全部為空 * @param obj * @return 返回屬性
java junit測試,註解引入service報錯解決辦法
作為程式設計師,搞後端開發,當然離不開單元測試了。舉個例子,測試下mapper介面,service介面等等。當用到spring容器的時候,在測試類直接寫 如下程式碼: @Autowired private AccountService accountServiceIm
pymysql在讀取bit類型時顯示x00的解決辦法
nec pymysql 類型 轉換 問題 username ons connect 解決辦法 #用於轉換bit在pymysql中顯示為x00的問題from pymysql import convertersconverions = converters.conversion