在service層注入mapper時(shí)報(bào)空指針的解決
在service層注入mapper時(shí)報(bào)空指針
今天又遇到一個(gè)極其刁鉆的問題,廢話不多說先上代碼,測試單元
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBoot_Run.class) @ContextConfiguration(locations = { "classpath:mybatis/mappers/RevMapper.xml" }) public class TestTransaction { @Autowired RevMapper remapper; @Test public void testInsert() { ReData data = new ReData(); data.setReTime(new Date()).setSeID("fdewfcdsfdssdfdsf").setSendDate(new Date()); remapper.insertObject(data); }
然后是service代碼
public class ReService { @Autowired private RevMapper reMapper; private Socket socket=null; private BufferedReader br=null; private PrintWriter pw=null; public void recevice() { try { //創(chuàng)建服務(wù)器,并開放3081端口 ServerSocket serv
RevMapper 類在測試的時(shí)候注入的好好地,為毛在service中就是空,一直空,空空空!??!
網(wǎng)上說的@mapperScan還有@mapper的注解我都加了一遍,這是為毛?。。。?!
在博覽全部大神的CSDN中,我發(fā)現(xiàn)大家都是抄過來抄過去,小弟佩服?。?/p>
解決?。?!
因?yàn)槲以趩宇愂沁@樣寫的
@SpringBootApplication(exclude=DataSourceAutoConfiguration.class) @MapperScan(“cn.yungtay.mapper”) public class SpringBoot_Run { public static void main(String[] args) { SpringApplication.run(SpringBoot_Run.class, args); ReMapper re=new ReMapper(); re.receive; } }
厲害的歐巴們不要噴,我第一反應(yīng)是這樣的?。?/p>
問題出來了,當(dāng)一個(gè)對象是new出來的時(shí)候,他是不交給spring管理的,所以對象根本注入不進(jìn)去,null是理所當(dāng)然的
第二個(gè)問題,你想一個(gè)方法隨著主啟動類而啟動,你可以這么干
@Service public class ReService implements ApplicationRunner{ @Autowired private RevMapper reMapper; private Socket socket=null; 。。。。。。。。。。。。。 @Override public void run(ApplicationArguments args) throws Exception { // TODO Auto-generated method stub 你所需要啟動的方法XXXXXXXX }
感覺自己又智慧了一點(diǎn)!
springmvc普通類(非control,service)注入mapper為null
在給項(xiàng)目寫一個(gè)定時(shí)器的時(shí)候,需要用到注入mapper進(jìn)行數(shù)據(jù)庫操作,用像serviceimpl里的注入
@Autowired UserMapper usermapper;
無效,debug后發(fā)現(xiàn)usemapper為null,說明沒有注入成功
后看到其他文章知道了new出來的thread不在spring的容器中,所以無法注入成功,獲得bean
但是按照他的方法依舊為null,他的想法是主動注入bean,應(yīng)該是對的。
不過我這個(gè)可能有點(diǎn)特殊,于是最后只能使用終極大法
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); usermapper = (UserMapper) ac.getBean("UserMapper"); usermapper.deleteAllCookies();
不要忘了給mapper個(gè)名字,例
@Repository(value="UserMapper") public interface UserMapper { public List<User> selectByExample(@Param("username1")String username,@Param("password")String password); public int insertToken(@Param("username1")String username,@Param("token")String token); public String checkToken(String token); public int logout(@Param("username1")String username,@Param("token")String token); public int deleteAllCookies(); }
這個(gè)方法主觀上感覺不是很好,先這樣吧!
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot Event 事件如何實(shí)現(xiàn)異步延遲執(zhí)行
這篇文章主要介紹了Spring Boot Event 事件如何實(shí)現(xiàn)異步延遲執(zhí)行問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02SpringBoot中動態(tài)更新@Value配置方式
這篇文章主要介紹了SpringBoot中動態(tài)更新@Value配置方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Spring?MVC實(shí)現(xiàn)GET請求接收Date類型參數(shù)
這篇文章主要介紹了Spring?MVC實(shí)現(xiàn)GET請求接收Date類型參數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07SpringSecurity 默認(rèn)表單登錄頁展示流程源碼
本篇主要講解 SpringSecurity提供的默認(rèn)表單登錄頁 它是如何展示流程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-01-01Quarkus中ConfigSourceInterceptor的加密配置實(shí)現(xiàn)
這篇文章主要為大家介紹Quarkus中ConfigSourceInterceptor加密配置的實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02基于Java數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列的兩種方法小結(jié)
下面小編就為大家分享一篇基于Java數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列的兩種方法小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12詳解Spring Kafka中關(guān)于Kafka的配置參數(shù)
這篇文章主要介紹了詳解Spring Kafka中關(guān)于Kafka的配置參數(shù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08springboot 默認(rèn)靜態(tài)路徑實(shí)例解析
這篇文章主要介紹了springboot 默認(rèn)靜態(tài)路徑實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11