當前位置:首頁 > 資訊 > 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號
广东25选5开奖结果