溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在service層注入mapper時(shí)報(bào)空指針怎么辦

發(fā)布時(shí)間:2021-06-18 09:10:05 來(lái)源:億速云 閱讀:1116 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下在service層注入mapper時(shí)報(bào)空指針怎么辦,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

在service層注入mapper時(shí)報(bào)空指針

今天又遇到一個(gè)極其刁鉆的問(wèn)題,廢話不多說(shuō)先上代碼,測(cè)試單元

@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ù)器,并開(kāi)放3081端口
      ServerSocket serv

RevMapper 類在測(cè)試的時(shí)候注入的好好地,為毛在service中就是空,一直空,空空空?。?!

網(wǎng)上說(shuō)的@mapperScan還有@mapper的注解我都加了一遍,這是為毛?。。。?!

在博覽全部大神的CSDN中,我發(fā)現(xiàn)大家都是抄過(guò)來(lái)抄過(guò)去,小弟佩服??!

解決!??!

因?yàn)槲以趩?dòng)類是這樣寫的

@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>

問(wèn)題出來(lái)了,當(dāng)一個(gè)對(duì)象是new出來(lái)的時(shí)候,他是不交給spring管理的,所以對(duì)象根本注入不進(jìn)去,null是理所當(dāng)然的

第二個(gè)問(wèn)題,你想一個(gè)方法隨著主啟動(dòng)類而啟動(dòng),你可以這么干

@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
 你所需要啟動(dòng)的方法XXXXXXXX
}

感覺(jué)自己又智慧了一點(diǎn)!

springmvc普通類(非control,service)注入mapper為null

在給項(xiàng)目寫一個(gè)定時(shí)器的時(shí)候,需要用到注入mapper進(jìn)行數(shù)據(jù)庫(kù)操作,用像serviceimpl里的注入

@Autowired
UserMapper usermapper;

無(wú)效,debug后發(fā)現(xiàn)usemapper為null,說(shuō)明沒(méi)有注入成功

后看到其他文章知道了new出來(lái)的thread不在spring的容器中,所以無(wú)法注入成功,獲得bean

但是按照他的方法依舊為null,他的想法是主動(dòng)注入bean,應(yīng)該是對(duì)的。

不過(guò)我這個(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();
}

以上是“在service層注入mapper時(shí)報(bào)空指針怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI