bean的部分属性
2016-11-23 20:47
295 查看
下面 我们介绍 bean中的部分属性:
1.
lazy-init(默认为“default”):用来定义这个Bean是否实现懒初始化。
也就是说lazy-init=”true”的bean,IoC容器启动时不会实例化该bean,只有当容器需要用到时才实例化它。lazy-init有利于容器效率,对于不需要的bean可以先不管。
2.autowire(自动装配,默认为“default”):它定义了Bean的自动装载方式。
1、“no”:不使用自动装配功能。
2、“byName”:通过Bean的属性名实现自动装配。
3、“byType”:通过Bean的类型实现自动装配。
4、“constructor”:类似于byType,但它是用于构造函数的参数的自动组装。
5、“autodetect”:通过Bean类的反省机制(introspection)决定是使用“constructor”还是使用“byType”。
3.scope 就是用来配置 spring bean 的作用域,它标识 bean 的作用域。
(1).当一个bean的 作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。
(2).prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的 getBean()方法)都会产生一个新的bean实例,相当与一个new的操作。
将这几个属性用到代码中:
进行如下 配置
在Test中进行如下测试 :
测试 scope
当 配置bean 时 没有 配置scope 默认是单例模式,即scope=”singleton”
我们进行如下测试:
结果显示为:
当添加这个属性
结果为:
1.
lazy-init(默认为“default”):用来定义这个Bean是否实现懒初始化。
也就是说lazy-init=”true”的bean,IoC容器启动时不会实例化该bean,只有当容器需要用到时才实例化它。lazy-init有利于容器效率,对于不需要的bean可以先不管。
2.autowire(自动装配,默认为“default”):它定义了Bean的自动装载方式。
1、“no”:不使用自动装配功能。
2、“byName”:通过Bean的属性名实现自动装配。
3、“byType”:通过Bean的类型实现自动装配。
4、“constructor”:类似于byType,但它是用于构造函数的参数的自动组装。
5、“autodetect”:通过Bean类的反省机制(introspection)决定是使用“constructor”还是使用“byType”。
3.scope 就是用来配置 spring bean 的作用域,它标识 bean 的作用域。
(1).当一个bean的 作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。
(2).prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的 getBean()方法)都会产生一个新的bean实例,相当与一个new的操作。
将这几个属性用到代码中:
package test_spring2; public class User { private int age; private String name; private Dog dog1; public User() { } public User(int age,String name){ this.age = age; this.name =name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [age=" + age + ", name=" + name + ", dog=" + dog1 + "]"; } public void setDog1(Dog dog) { this.dog1 = dog; } }
package test_spring2; public class Dog { public Dog() { System.out.println("dog。。。。。。。"); } }
进行如下 配置
<bean id="dog" class="test_spring2.Dog" lazy-init="true" scope="prototype"/> <!--byName byType --> <bean id="user" class="test_spring2.User" autowire="byType"> </bean>
在Test中进行如下测试 :
测试 scope
当 配置bean 时 没有 配置scope 默认是单例模式,即scope=”singleton”
我们进行如下测试:
结果显示为:
当添加这个属性
结果为:
相关文章推荐
- 根据部分属性判断List<实体bean>中是否含有某个实体bean-重写equals方法
- Spring框架--bean标签部分属性的学习
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- Spring初学之Bean标签中的abstract和parent属性
- spring bean标签的属性配置
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
- Spring boot将配置属性注入到bean 专题
- TextView使用SpannableString设置某部分文本的各种属性(点击、颜色、下划线...)
- javabean内省-处理类属性、事件
- Spring - bean的autowire属性(自动装配)
- Spring中如何向Bean注入系统属性或环境变量
- js中部分属性不能用于if条件判断
- 熟悉css部分语法属性
- bean标签的属性和spring属性注入
- Spring-Bean的属性(1.单独定义BEAN,ID为之的BEAN参考之 2.在BEAN中直接定义参考的BEAN)
- firefox、IE之部分属性用法区别
- Spring 配置文件 属性 ref bean和local的区别
- 类。字段。方法。属性之间的定义及部分区别。
- 谈谈Spring配置中<bean>的id和name属性的花拳秀腿
- Spring-bean属性-1