当前位置:首页 > 资讯 > info6 > 正文

懒得笔记4 spring annotation

发表于: 2015-08-13   作者:chen_xinjia   来源:转载   浏览:
摘要: 1,要加入aop包,不然会报  org.springframework.beans.factory.BeanDefinitionStoreException:UnexpectedexceptionparsingXMLdocumentfromclasspathresource        [beans.xml];nested       exceptionisjava.lang.NoClassDe

1, 要加入 aop 包, 不然会报

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource                [beans.xml]; nested              exception is java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource

2,xml 文档

    

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>


3,注解

    1,@Autowired

     按类型再按名字@qualifier

        在setter之前标志(As expected, you can apply the @Autowired annotation to "traditional" setter methods:)

    2,@Required

      This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws an exception if the affected bean property has not been populated; this allows for eager and explicit failure, avoiding NullPointerExceptions or the like later on. It is still recommended that you put assertions into the bean class itself, for example, into an init method. Doing so enforces those required references and values even when you use the class outside of a container.


     仅仅在配置时就用于检测是否有合适的值与bean配置。防止出现NullPointerExceptions 

  3,@Resource(name="UserDAO")

     按名字再按类型

 4,@component

       在xml中加入以下这句话,便可以不用在xml里定义 bean 系统自动到指定的地方去扫描找到对应的bean,写在对应的类里

     <context:component-scan base-package="com.bjsxt"/>

     在bean中加入 @Component("u")  来指定bean 的id 也可不写,默认为类名小写


懒得笔记4 spring annotation

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号