IOC容器

描述

  • IOC:控制反转,将对象的创建过程交给Spring容器,由容器管理对象的生命周期;

Bean的作用域

  • Spring Bean实例的作用范围由配置项scope限定:scope="singleton"
  • bean是一个由Spring IoC容器实例化、组装和管理的对象
作用域 描述
singleton 单例模式(默认值);IOC容器仅创建一个Bean实例,每次返回的都是同一个Bean实例
prototype 原型模式;可以创建多个Bean实例,每次调用Bean时都返回一个新的Bean实例
以下适用于ApplicationContext环境中(WebApplicationContext继承ApplicationContext接口,是它的扩展)
request HTTP请求;每次HTTP请求都会创建一个新的Bean实例
session 会话;同一个session共享一个Bean实例,不同会话使用不同的Bean实例
global-session 全局会话;一般运用于Portlet应用环境(Portlet是基于Java的Web组件,由Portlet容器管理,并由容器处理请求,生产动态内容)

DI依赖注入

<1> 基于构造函数的依赖注入

	<constructor-arg ref="..."/>
	
	<constructor-arg>
        <ref bean="anotherExampleBean"/>
    constructor-arg>

 	<constructor-arg  type="int"  value="..."/>
	<constructor-arg  index="0"  value="..."/>

注:使用type和index来处理多个构造器的注入

<2> 基于设值函数(setter)的依赖注入
  • 传递引用

 	<property  name="..."  ref="..."/>

	<property  name="..."  value="..."/>
注:使用p-namespace实现xml配置
<bean id="..."  class="...">
	<property name="a" ref="c"/>
	<property name="b" value="3"/>
bean>

 <bean  id="..."  class="..."
 	p: b="3"
 	p: a-ref="c"/>
注:注入高级属性-集合
<property>


	<list>
		<value>...value>
		<value>...value>
	list>

	<map>
		<entry key="..." value="..."/>
	map>

	<props>
		
		<prop key="...">...prop>
	props>
	
property>

自动装配

  • 在标签中配置autowire="..."
  • 在不使用和注入数据的情况下,Spring容器自动装配相互协作的bean 的关系
	public class School {
     
		private Student student; //使用byName方式装装配
		        ......
	}
装配方式 描述
no (默认)无自动装配;Bean引用必须由ref元素定义
byName 按属性名称自动装配
byType 按属性类型自动装配
constructor 类似于byType但适用于构造函数参数

基于注解的容器配置

  • 提供了XML设置的替代方法,该配置依赖字节码元数据来连接组件,而不是尖括号声明
  • 通过使用相关类,方法或字段声明上的注释,开发人员无需使用XML来描述bean的连接,而是将配置移入组件类本身
  • 使用注解时要在xml文件中配置:开启注解注入功能
  • :扫描这个包下被注解标识的类,并自动注册为bean,配置它时可省去,它包含功能
  • @Required
    应用于bean的setter方法上,用于检查一个Bean的属性的值在配置期间是否被赋予或设置
  • @Autowired
    自动装配(byType),应用于setter方法、构造函数、成员变量
  • @Resource
    自动装配(默认byName),有name和type属性来指定装配方式
  • @Qualifier
    当使用@Autowired且出现多个类型一样的bean时,使用@Qualifier("…")来指定名称,表明用byName方式进行注入
	@Autowired
    @Qualifier(value = "user1")
    private User user;
  • @Value
    (1)将一个SpEL(SpEL:spring表达式,类似于ognl)表达式结果映射到功能处理方法的参数上;
    (2)要指定默认值,可以将@Value注释放在字段,方法和方法或构造函数参数上。
    (3)定义表达式的语法都是表单#{ }
@Value(#{...}) @Value(${...})
表示常量,获取bean的属性,或者调用bean的某个方法 从配置properties文件中读取值
 @Value("#{'hello world!'}")  
    private String str; //获取一个字符串,也可以获取一个数字常量 
    
    @Value("#{dataSource.url}") //通过bean id获取bean的属性值  
    private String jdbcUrl; 
     
	@Value("#{ systemProperties['user.region'] }")	//也可以用在方法上...
    public void setDefaultLocale(String defaultLocale) {
     
        this.defaultLocale = defaultLocale;
    }

基于Java的容器配置

  • @Configuration
    类似于xml里面的标签,用于配置类上,作为bean定义的来源
  • @Bean
    类似于xml里面的标签,用于方法之上,告诉方法让其产生一个Bean对象,然后把这个Bean对象交给Spring管理
	@Configuration
	public class AppConfig {
     
    	@Bean
    	public TransferServiceImpl transferService() {
     
        return new TransferServiceImpl();
    	}
	}

等效于:

	<beans>
    	<bean id="transferService" class="com.acme.TransferServiceImpl"/>
	beans>
  • @Import
    用于类上,表示从另外一个配置类中加载@Bean定义
  • @Scope
    定义作用域,默认为单例模式

你可能感兴趣的:(Spring)