1. 实例化 bean
java bean:
public class Teacher {
private String name;
private int age;
public Teacher() {
this.name = "张老师";
this.age = 34;
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
/*getter setter toString*/
}
public class Student {
private String name;
private int age;
@Autowired
private Teacher teacher;
public Student() {
this.name = "张三";
this.age = 18;
}
public Student(String name, int age, Teacher teacher) {
this.name = name;
this.age = age;
this.teacher = teacher;
}
/*getter setter toString*/
}
1.1 使用类构造器实例化
默认使用不带参数的构造方法实例化 Bean 对像
ApplicationContext xml :
<bean id="teacher" class="com.bean.Teacher"/>
<bean id="stu" class="com.bean.Student"/>
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"/ApplicationContext.xml"})
public class MyTest {
@Autowired
Student student;
@Test
public void test(){
System.out.println(student);
}
}
执行结果:
1.2 使用静态工厂方法实例化
静态工厂:
package com.factory;
import com.bean.Teacher;
public class TeacherFactory {
public static Teacher createTeacher(){
Teacher teacher = new Teacher("老张",35);
return teacher;
}
}
ApplicationContext xml :
<bean id="teacher1" class="com.factory.TeacherFactory" factory-method="createTeacher"/>
<bean id="stu" class="com.bean.Student"/>
测试:
(测试代码同上)
执行结果:
1.3 实例工厂方法实例化
实例工厂:
package com.factory;
import com.bean.Teacher;
public class TeacherFactory {
public Teacher createTeacher2(){
Teacher teacher = new Teacher("老李",45);
return teacher;
}
}
ApplicationContext xml :
<bean id="teaFactory" class="com.factory.TeacherFactory"/>
<bean id="teacher2" factory-bean="teaFactory" factory-method="createTeacher2"/>
<bean id="stu" class="com.bean.Student"/>
测试:
(测试代码同上)
执行结果:
2. 依赖注入
- 依赖注入(Dependency Injection,DI):在运行期,由外部容器动态地将依赖对象注入到组件中。
- 依赖注入有两种方法,构造方法注入和 setter 方法注入。
2.1 构造方法注入
<bean id="teacher" class="com.bean.Teacher">
<constructor-arg name="name" value="老刘"/>
<constructor-arg name="age" value="35"/>
</bean>
<bean id="teacher2" class="com.bean.Teacher">
<constructor-arg index="0" value="老刘"/>
<constructor-arg index="1" value="36"/>
</bean>
2.2 setter 方法注入
<bean id="stu" class="com.bean.Student">
<property name="name" value="小张"/>
<property name="age" value="18"/>
<property name="teacher" ref="teacher"/>
</bean>
2.3 集合类型的装配
In the <list/>, <set/>, <map/>, and <props/> elements, you set the properties and arguments of the Java Collection types List, Set, Map, and Properties, respectively
java bean:
public class Student {
private String name;
private int age;
public Student() {
this.name = "张三";
this.age = 18;
}
/*getter setter toString*/
}
public class Teacher {
private String name;
private int age;
private Properties info;
private Set<String> hobby = new HashSet<String>();
private List<Student> stus = new ArrayList<Student>();
private Map<String, String> clazz = new HashMap<String, String>();
/*getter setter toString*/
}
并提供 getter setter
ApplicationContext xml :
<bean id="teacher" class="com.bean.Teacher">
<property name="name" value="老刘"/>
<property name="age" value="35"/>
<property name="info">
<value>
email=zzz@qq.com
phone=123456
</value>
</property>
<property name="hobby">
<set>
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
</set>
</property>
<property name="stus">
<list>
<ref bean="stu"></ref>
<ref bean="stu2"/>
</list>
</property>
<property name="clazz">
<map>
<entry key="01" value="自动化"/>
<entry key="02" value="计算机"/>
</map>
</property>
</bean>
<bean id="stu" class="com.bean.Student">
<property name="name" value="小张"/>
<property name="age" value="35"/>
</bean>
<bean id="stu2" class="com.bean.Student"/>
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"/ApplicationContext.xml"})
public class MyTest {
@Autowired
Teacher teacher;
@Test
public void test(){
System.out.println(teacher);
}
}
测试结果:
Teacher{name='老刘', age=35, info={phone=123456, email=zzz@qq.com}, hobby=[抽烟, 喝酒, 烫头], stus=[Student{name='小张', age=35}, Student{name='张三', age=18}], clazz={01=自动化, 02=计算机}}
2.4 空配置
<bean id="teacher" class="com.bean.Teacher">
<property name="name" value="老刘"/>
<property name="age" value="35"/>
<property name="info" value="">
</property>
</bean>
【注】设置value 为空或为 null
2.5 延迟初始化
<bean id="stu2" class="com.bean.Student" lazy-init="true"/>
【注】设置 lazy-init 为 true
2.6 自动装配
在<beans><bean>中配置自动装配类型
no | (Default) No autowiring |
byName | 按名称自动装配。 |
byType | 按类型自动装配。 |
constructor | 类似于byType,但适用于构造函数参数 |
1.在 Spring 配置文件中对象名和 ref=”id” id 名相同使用自动注入,可以不配置<property/>
2.两种配置办法
2.1 在<bean>中通过 autowire=” ” 配置,只对这个<bean>生效
2.2 在<beans>中通过 default-autowire=””配置,表当当前文件中所有<bean>都是全局配置内容
3.autowire=”” 可取值
3.1 default: 默认值,根据全局 default-autowire=””值.默认全局和局部都没有配置情况下,相当于 no
3.2 no: 不自动注入
3.3 byName: 通过名称自动注入.在 Spring 容器中找类的 Id
3.4 byType: 根据类型注入. spring 容器中不可以出现两个相同类型的<bean>
3.5 constructor: 根据构造方法注入.
3. Spring 中加载 properties 文件
1. 在 src 下新建 xxx.properties 文件
2. 在 spring 配置文件中先引入 xmlns:context,在下面添加
如果需要记载多个配置文件逗号分割
<context:property-placeholder
location="classpath:db.properties"/>
3. 在被Spring管理的类中通过@Value(“${key}”)取出properties中内容
4.scope 属性
- <bean>的属性,
- 作用:控制对象有效范围(单例,多例等)
- <bean/>标签对应的对象默认是单例的.
- scope 可取值
- singleton 默认值,单例
- prototype 多例,每次获取重新实例化
- request 每次请求重新实例化
- session 每个会话对象内,对象是单例的.
- application 在 application 对象内是单例
- global session spring 推 出 的 一 个 对 象 , 依 赖 于 spring-webmvc-portlet ,类似于 session