<?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;