Spring中的IOC注解:
Spring从2.5以后,正式引入基于Annotation(注解)的配置支持。
1.注入配置
为了启用依赖注入的注解,我们可以通过显示配置能够启用这些注解的类,或者通过xml提供的context 命名空间进行隐式启动。
启用配置:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- context:annotation-config标签会启用@Required and @Autowired,@PostConstruct,
@PreDestroy and @Resource (if available) 等标记-->
<context:annotation-config></context:annotation-config> </beans>
@Autowaired
作用: 自动注入。
默认先按照byType的方式进行查找,如果找到一个,直接注入,如果找到多个类型匹配的,
1.如果< bean primary=true>,@Primary,使用首选bean
2. 如果没有设置首选bean,再按照byName的方式进行查找
1.属性声明上方(直接反射获取属性,调用Field.set方法注入):
byName:属性名字
2.set方法上方(set方法注入):
byName:参数名字
3.构造器的上方:(构造注入)(多出现在一个构造器上)
byName:构造参数的名字
required = false: 默认为true,如果找不见满足(或者类型或者名字)要求的依赖bean,代码抛出异常,如果不 希望抛出异常,可以写required=false,那么spring容器不再尝试去做注入。
@Qualifier
作用: 和@Autowired标注结合 按照@Qualifier(“beanName”)中指定的名字的Bean进行注入。 如果找不见条件符合的bean对象进行注入,又不希望抛出异常,可以使用 @Autowired(required=false);
@Resource
作用: 先按照byName的方式进行注入,如果找不到名字相同的,再按照byType的方式进行注入。
1.属性声明的上方 byName:属性名
2.set方法的上方。 set方法名字去掉set之后的字符串,如果不匹配按照参数名字查找
@Value
作用: 用于给属性注入基本数据,类似于< property value="" >
属性:
value :具体的属性值
@PostConstruct
作用: 指定初始化方法
@PreDestory
作用:指定销毁方法
2.bean定义
上边讲述的注解都是基于Bean装配等的注解。但是Bean定义还是在xml文件中,如果希望bean定义也是 基于注解的,可以使用:
@Component
作用: spring2.5的注解,让spring容器来管理对象,相当于在xml中配置一个bean。
属性:
value: 指定bean的id,如果不指定,默认为当前类名且首字母小写
@Controller , @Service , @Repository
作用: 功能与 @Component 作用一样,只不过其语义更加明确
@Controller 一般作用于 表现层
@Service 一般作用于 业务层
@Repository 一般作用于 持久层
属性: value : 指定bean的id,如果不指定,默认为当前类名,且首字母小写
@Scope
作用: 指定bean的作用范围
属性:
value: 指定范围值
取值为: singleton , prototype , request , session , golbalsession
为了启用上边的注解,需要在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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="此处指定从classpath路径下的哪个package进行组件搜索">
</context:component-scan>
</beans>
component-scan会隐式的启用annotation-config启动的所有功能,因此有了这个配置后,
annotation-config就可以不写了。 context:component-scan/
允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类 型的过滤方式(知道即可): 过滤器类型 表达式范例
annotation org.example.SomeAnnotation assignable
org.example.SomeClass regex org.example.Default.* aspectj
org.example…*Service+
2.1 无参构造器
package ioc.day03;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Factory {
public Factory() {
System.out.println("Factory无参构造器。。");
}
public Factory(SourceBean sb) {
System.out.println("factory有参source:"+sb);
}
}
2.2 有参构造器
package ioc.day03;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Factory {
public Factory() {
System.out.println("Factory无参构造器。。");
}
@Autowired //我们之前讲的通过构造器注入。
public Factory(SourceBean sb) {
System.out.println("factory有参source:"+sb);
}
}
3.Java配置
Spring3.0以后提供了Java_Base配置。也就是通过Java代码代替原来xml的配置元数据,进行容器的 初始化。 主要用到的注解:
@Configuration 和 @Bean java配置,是完全注解的配置,此时需要使用的Spring容器 为:
AnnotationConfigApplicationContext
@Configuration
- 相当于xml 文件的< beans />配置
- 一个@Configuration注解的类,相当于一个xml文件
@Bean
- 相当于xml文件中< bean/>的配置
- @Bean只能用在方法上,标识这个方法的返回值是一个受Spring容器管理的Bean对象。
- 常和@Configuration注解合用,也可以定义在@Component注解的类中。
例如:
package ioc.javaConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //相当于定义了一个配置文件
public class AppConfig {
@Bean //定义了一个被Spring容器管理的String对象。如果没有指定名字,默认为方法名。
public String getString() {
System.out.println("get str....");
return "hello";
}
}
初始化容器:
public class AnnoTest {
@Test
public void java_config() {
AnnotationConfigApplicationContext ac =
new AnnotationConfigApplicationContext(
AppConfig.class);
System.out.println("-----------------");
String s = ac.getBean(String.class);
System.out.println(s);
s = ac.getBean("getString"); //默认beanName为方法名。
System.out.println(s);
}
}