使用spring的@autowired註解,無法實例化dao,service,controller等

筆者在使用註解引入IOC容器中的bean對象的時候,報nullpointer的錯誤,查找了很多答案,有說spring配置文件application.xml中加入<context:annotation-config /> 標籤的,有說要初始化 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>這個bean對象的,但是筆者嘗試過了,都不起作用,終於發現是少了一個jar包struts2-spring-plugin .jar,坑~~~.下面是一些spring配置的總結,大家共勉。

Spring2.5的組件自動掃描 
在一個稍大的項目中通常會有上百個組件,如果都使用XML的bean定義來配置組件的話 
顯然會增加配置文件的體積,查找及維護也不方便 
而Spring2.5就爲我們引入了組件自動掃描機制 
它可以在classpath下尋找標註了@Service、@Repository、@Controller、@Component註解的類 
並把這些類納入Spring容器中管理,它的作用和在XML中使用bean節點配置組件是一樣的 
使用自動掃描機制,則需配置<context:component-scan base-package="com.jadyer"/>啓動自動掃描 
其中base-package指定需要掃描的包,它會掃描指定包中的類和子包裏面類 
@Service用於標註業務層組件 
@Repository用於標註數據訪問組件,即DAO組件 
@Controller用於標註控制層組件,如Struts中的Action 
@Component泛指組件,當組件不要好歸類時,可以使用這個註解進行標註 


1、可以使用諸如@Service("personDao")修改bean名稱,而它默認的是將首字母小寫的類名作爲<bean>名稱 
2、若要更改<bean>作用域的話,可以使用@Scope("prototype")註解來修改<bean>作用域 
3、若想讓<bean>實例化之後去執行初始化方法,可以使用@PostConstruct標註在方法上 
4、同樣@PreDestroy註解標註在方法上,可以用來指定<bean>銷燬時執行的方法 
這裏的@PostConstruct是EJB3裏面用來初始化bean的註解,它也不是Spring中的註解 
且<context:component-scan base-package=""/>的背後註冊了很多用於解析註解的處理器 
其中就包括了<context:annotation-config/>配置項裏面的註解所使用的處理器 
所以配置了<context:component-scan base-package="">之後,便無需再配置<context:annotation-config>


可在Java代碼中使用@Resource或者@Autowired註解進行裝配,但需在XML中配置以下信息 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd" 
然後顯式的配置<context:annotation-config/> 
該配置隱式註冊了多個對註解進行解析的處理器,如下列舉 
AutowiredAnnotationBeanPostProcessor      CommonAnnotationBeanPostProcessor 
PersistenceAnnotationBeanPostProcessor    RequiredAnnotationBeanPostProcessor 
其實,註解本身做不了任何事情,和XML一樣,只起到配置的作用,主要在於背後強大的處理器
 


@Resource註解 
@Resource註解和@Autowired一樣,也可以標註在字段或屬性的setter方法上 
@Resource默認按名稱裝配,名稱可以通過name屬性指定。當找不到與名稱匹配的bean時,纔會按類型裝配 
若註解標註在字段上且未指定name屬性,則默認取字段名作爲bean名稱尋找依賴對象 
若註解標註在setter上且未指定name屬性,則默認取屬性名作爲bean名稱尋找依賴對象 
如果沒有指定name屬性,並且按照默認的名稱仍找不到依賴對象時,它就會按類型匹配 
但只要指定了name屬性,就只能按名稱裝配了 

@Autowired註解 
@Autowired默認是按類型裝配對象的,默認情況下它要求依賴對象必須存在 
如果允許null值,可以設置它的required屬性爲FALSE,如@Autowired(required=false) 
若想要按名稱裝配,可以結合@Qualifier註解一起使用,如@Autowired(required=false)  @Qualifier("personDaoBean") 

spring配置文件頭部配置解析(applicationContext.xml)

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.                         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.                         http://www.springframework.org/schema/mvc  
  10.                         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  11.                         http://www.springframework.org/schema/context  
  12.                         http://www.springframework.org/schema/context/spring-context.xsd">  
  13.       
  14.     <!-- 定義controller掃描包 -->  
  15.     <context:component-scan base-package="com.example.controller" />  
  16.     <mvc:annotation-driven />  
  17.     <!-- 處理靜態資源 -->  
  18.     <mvc:default-servlet-handler/>  
  19.     <!-- 配置視圖解析器 -->  
  20.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  21.        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
  22.        <property name="prefix" value="/WEB-INF/jsp/"/>  
  23.        <property name="suffix" value=".jsp"/>  
  24.     </bean>  
  25.       
  26. </beans>  

1.xmlns=http://www.springframework.org/schema/beans  和 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 是必須有的所有的spring配置文件都一樣

2.xmlns:xxx 這個是xml的命名空間,對於命名空間不進行展開,簡單的理解其實就是你要使用spring的哪些模塊的內容,之所以第一點說xmlns="http://www.springframework.org/schema/beans 是必須的,就是因爲beans是spring的根本,如果連這個都沒有的話就算不上是spring的配置文件了,在這個配置文件裏面還有xmlns:p、 xmlns:mvc、xmlns:context 這三個命名空間,有了這些纔可以使用<context:component /> <mvc:annotation-driven /> 這樣的功能,如果沒有這些命名空間的話而使用<context:component /> <mvc:annotation-driven /> 這些功能是會報錯的哦!

3.下面就是xsi:schemaLocation了,這個是用來做什麼的呢?其實是爲上面配置的命名空間指定xsd規範文件,這樣你在進行下面具體配置的時候就會根據這些xsd規範文件給出相應的提示,比如說每個標籤是怎麼寫的,都有些什麼屬性是都可以智能提示的,以防配置中出錯而不太容易排查,在啓動服務的時候也會根據xsd規範對配置進行校驗。

4.配置文件中.XSD文件的uri是http://www.springframework.org/schema/.............xsd 那麼問題來了,真的要到網上去找這個.XSD文件嗎?當然不是

打開spring-webmvc-4.2.3.RELEASE.jar>>META-INF>>spring.schemas  會看到以下內容:

http\://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd=org/springframework/web/servlet/config/spring-mvc-3.1.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd=org/springframework/web/servlet/config/spring-mvc-4.1.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd=org/springframework/web/servlet/config/spring-mvc-4.2.xsd
http\://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-4.2.xsd

看到這些http://www.springframework.org/schema/.............xsd  內容大家就該明白了,其實http\://www.springframework.org/schema/mvc/spring-mvc.xsd 真正指向的位置是
org/springframework/web/servlet/config/spring-mvc-4.2.xsd,那麼打開這個uri看看有什麼

果然xsd文件就在這裏…………其他的也都是以此類推 

轉載:http://blog.csdn.NET/f_639584391/article/details/50167321


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章