9.IOC的annotation什么有什么怎么用?
首先介绍xml的文件头:
xmlns:xml name space
xsi:schemalocation: ......xsd
xsd文件:元数据文件定义xml的语法。(替代dtd文件)
xml引用多个xsd文件。
开始使用annotation
xml文件中头配置:<context:annotation-config/> 会初始化四个processor的bean的实例化。
@Autowire 默认bytype
@Autowire 如果想用byname使用@qualifier
如:
@Autowire
public viod setX(@qualifier(value="u") X x){
this.x = x;
}
@required 编译时检查提示错误。
@resource jsr250
默认按名称,名称找不到,按类型
跟Autowire一样用法,但是可以用@resource(name="u")
推荐使用。
@component
xml文件头写上<contex:component-scan base-pakage="com.bjsxt"/>
会在com.bjsxt包下检查有没有类名上家有@component如果有自动注入,
等同于:<bean name="mybean" class="classpath"/>
- @Component @Service @Controller @Repository
a) 初始化的名字默认为类名首字母小写
b) 可以指定初始化bean的名字
以上注解
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/>
<context:component-scan base-package="org.example"/>
</beans>
- @Scope
- @PostConstruct = init-method; @PreDestroy = destroy-method;
annotation不足之处必须要有源码进行注解。

浙公网安备 33010602011771号