使用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


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