懒得笔记4 spring annotation

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 也可不写,默认为类名小写


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值