在使用@Autowired注解注入出現的空指針 java.lang.NullPointerException 可能存在的錯誤原因:
1.注解的掃描有問題
在xml配置了這個標簽后,spring可以自動去掃描base-pack下面或者子包下面的java文件,如果掃描到有@Component @Controller@Service等這些注解的類,則把這些類注冊為bean
注意:如果配置了<context:component-scan>那么<context:annotation-config/>標簽就可以不用再xml中配置了,因為前者包含了后者。另外<context:component-scan>還提供了兩個子標簽
1. <context:include-filter>
2. <context:exclude-filter>
在說明這兩個子標簽前,先說一下<context:component-scan>有一個use-default-filters屬性,改屬性默認為true,這就意味着會掃描指定包下的全部的標有@Component的類,並注冊成bean.也就是@Component的子注解@Service,@Reposity等。所以如果僅僅是在配置文件中這么寫
<context:component-scan base-package="tv.huan.weisp.web"/>
Use-default-filter此時為true那么會對base-package包或者子包下的所有的進行java類進行掃描,並把匹配的java類注冊成bean。
可以發現這種掃描的粒度有點太大,如果你只想掃描指定包下面的Controller,該怎么辦?此時子標簽<context:incluce-filter>就起到了勇武之地。如下所示
<context:component-scan base-package="tv.huan.weisp.web .controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
這樣就會只掃描base-package指定下的有@Controller下的java類,並注冊成bean
但是因為use-dafault-filter在上面並沒有指定,默認就為true,所以當把上面的配置改成如下所示的時候,就會產生與你期望相悖的結果(注意base-package包值得變化)
<context:component-scan base-package="tv.huan.weisp.web">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
此時,spring不僅掃描了@Controller,還掃描了指定包所在的子包service包下注解@Service的java類
此時指定的include-filter沒有起到作用,只要把use-default-filter設置成false就可以了。這樣就可以避免在base-packeage配置多個包名這種不是很優雅的方法來解決這個問題了。
另外在我參與的項目中可以發現在base-package指定的包中有的子包是不含有注解了,所以不用掃描,此時可以指定<context:exclude-filter>來進行過濾,說明此包不需要被掃描。綜合以上說明
Use-dafault-filters=”false”的情況下:<context:exclude-filter>指定的不掃描,<context:include-filter>指定的掃描
2.類的注解問題
ServiceImpl必須以@Service或@Component注解才行。
2。自動寫入的時候把接口寫成實現類了
@Autowired
private ServiceImpl ServiceImpl;
應該是
@Autowired
private Service Service ;
3。在Dao 類上加上@Repository注解 mybatisDao mapper注解
Spring 反射調用類中的@Autowired注解服務空指針
解決方案
原因:反射的類無法被Spring進行管理。
解決:在使用前初始化一下(使用@PostConstruct
注解)
public class CompanyExecute { public static CompanyExecute companyExecute; @Autowired private CompanyService companyServiceImpl; /** * 初始化當前類 */ @PostConstruct public void init() { companyExecute = this; } public boolean insertCompany(String companyVoString) { boolean insertFlag = companyExecute.companyServiceImpl.insertCompany(companyVoString); return insertFlag; } }
3.SpringBoot的@Autowired無法注入問題
SpringBoot項目的Bean裝配默認規則是根據Application類所在的包位置從上往下掃描!“Application類”是指SpringBoot項目入口類。這個類的位置很關鍵:推薦放把啟動類放在com.example下(如下圖),不要放在java下。
解決:在啟動類上加注解 @ComponentScan("com.example")
1 @SpringBootApplication 2 @ComponentScan("com.example") 3 public class YyshopApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(YyshopApplication.class, args); 7 } 8 9 }
4.controller中對外方法接口寫成private了
靜態工具類:
package org.digdata.swustoj.util; import lombok.Getter; import lombok.NonNull; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * Created by hongfei.whf on 2016/10/31. */ public class ContextUtil implements ApplicationContextAware { @Getter private static volatile ApplicationContext context = null; /** * 加載Spring配置文件時,如果Spring配置文件中所定義的Bean類實現了ApplicationContextAware 接口,那么在加載Spring配置文件時,會自動調用ApplicationContextAware 接口中的 * public void setApplicationContext(ApplicationContext context) throws BeansException * & 方法,獲得ApplicationContext對象 * * @param applicationContext * @throws BeansException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } /** * 獲取bean * * @param clazz * @param beanName * @param <T> * @return */ public static <T> T getBean(@NonNull Class<T> clazz, @NonNull String beanName) { return (T) context.getBean(beanName); } }
在xml中定義bean
<!--手動bean定義-->
<bean class="org.digdata.swustoj.util.ContextUtil"/>
1
2
使用示例
/**
* 判題隊列
*/
private DefaultJudgeQueue defaultJudgeQueue = ContextUtil.getBean(DefaultJudgeQueue.class, "defaultJudgeQueue");
5配置文件
1.異常信息
2.有可能引起的原因:
1.在applicationContext-service.xml的配置文件里的包掃描不對
2.在web.xml里沒有加載spring容器
3.分布式工程,使用dubbo通信,在服務層,或者消費層, 單詞寫錯了
4.還有一種可能,有可能是pom 里的jar包沖突