基于注解(Annotation-based)的配置 及 几种注解标签的使用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"   //没有任何前缀(比如xsi,context)的这种标签,他的schemaLocation 是
                http://www.springframework.org/schema/beans  指定的文件是  spring-context-2.5.xsd 所以标签也是从这个文件里面来
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        //xsi 的xml命名空间
xmlns:context="http://www.springframework.org/schema/context"        
//context 的xml  ns(namespace)命名空间 ,配置了schemaLocation指定的文件之后就会有提示
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> </beans>
如果需要提示 可以按照之前的方法 引入spring-context-2.5.xsd  api

1. @Autowired

a) 默认按类型bytype

    在setter方法上面加上标签  @Autowired  

b) 如果想用byName,使用@Qulifier  括号后面是bean的name

    

c) 写在private field(第三种注入形式)(不建议,破坏封装)

d) 如果写在set上,@qualifier需要写在参数上(如上图)


2. @Resource(重要)

a) 加入:j2ee/common-annotations.jar

b) 默认按名称,名称找不到,按类型

c) 可以指定特定名称


d) 推荐使用

e) 不足:如果没有源码,就无法运用annotation,只能使用xml


3. @Component @Service @Controller @Repository

<context:component-scan base-package="com.bjsxt"/>    //从指定的包下面开始扫描 ,直到扫描到 @Coponent 标签,然后会把相应的类放到容器里面 ,默认容器的key就是类的名字 ,值是生成的对象 当然也可以自己指定名称

初始化的时候指定名字 也就是key


在setter方法处指定用哪一个


a) 初始化的名字默认为类名首字母小写

b) 可以指定初始化bean的名字


4. @PostConstruct = init-method; @PreDestroy = destroy-method;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值