Spring--IOC容器--Bean管理(XML)
Bean管理(xml方式)
1.1、创建对象及set注入(property标签)
Book.java
public class Book {
private String name;
private String author;
private String address;
//set方法注入
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setAddress(String address) {
this.address = address;
}
public void show(){
System.out.println(name+"::"+author+"::"+address);
}
}
bean1.xml
<!--2.set方法注入属性-->
<bean id = "book" class="spring5.Book">
<!--property完成属性注入
name:类里面的属性名称
value:向属性注入的值
-->
<property name="name" value="降龙十八掌"></property>
<!--属性的特殊符号怎么表示-->
<property name="author">
<value><![CDATA[<<郭靖>>]]></value>
</property>
<!--null值-->
<property name="address" >
<null></null>
</property>
</bean>
1.2、P名称空间注入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
<!--2.set方法注入属性 xml的简化版本-->
<bean id ="book2" class="spring5.Book" p:name="九阴白骨爪" p:author="周芷若">
</bean>
1.3、有参构造注入属性(constructor-arg标签)
Oreders.java
//使用有参数构造输入
public class Oreders {
private String oname;
private String address;
public Oreders(String oname,String address){
this.oname =oname;
this.address = address;
}
public void show(){
System.out.println(oname+"::"+address);
}
}
bean.xml
<!--3.有参数构造注入属性-->
<bean id="order" class="spring5.Oreders">
<constructor-arg name="oname" value="abc"></constructor-arg>
<constructor-arg name="address" value="杭州"></constructor-arg>
</bean>
1.4、注入外部Bean,内部Bean 级联
接口 UserDao.java
public interface UserDao {
public void update();
}
实现类 UserDaoImpl.java
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("Dao update...");
}
}
Userservice.java
public class Userservice {
//创建UserDao类型属性,生成set方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("add....");
userDao.update();
}
}
1.外部Bean
<!--外部bean-->
<bean id = "userService" class="spring5.service.Userservice">
<property name="userDao" ref ="userDaoImpl"></property>
</bean>
<bean id = "userDaoImpl" class="spring5.Dao.UserDaoImpl"></bean>
2.内部Bean
<!--内部类-->
<bean id = "emp" class="spring5.bean.Emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="dept">
<bean id="dept" class="spring5.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
3.级联赋值
Dept.java
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}
Emp.java
public class Emp {
private String ename;
private String gender;
private Dept dept;
public Dept getDept() {
return dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void add(){
System.out.println(ename+"::"+gender+"::"+dept.toString());
}
}
<bean id = "emp" class="spring5.bean.Emp">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--极值赋联-->
<property name="dept" ref ="depts"></property>
<property name="dept.dname" value="技术部"></property>
</bean>
<bean id ="depts" class="spring5.bean.Dept"></bean>
1.5、注入集合属性类型
1.注入数组类型的属性
2.注入List集合类型的属性
3.注入Map集合类型的属性
4.注入Set集合类型的属性
5.List集合的值是对象
<bean id ="stu" class="collectiontype.Stu">
<property name="course">
<array>
<value>spring5课程</value>
<value>springBoot</value>
</array>
</property>
<property name="list">
<list>
<value>张世杰</value>
<value>小张</value>
</list>
</property>
<property name="map">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<property name="sets">
<set>
<value>MYSQL</value>
<value>Redis</value>
</set>
</property>
<property name="coureList">
<list>
<ref bean="couse1"></ref>
<ref bean="couse2"></ref>
</list>
</property>
</bean>
<!--list的值是对象-->
<bean id = "couse1" class="collectiontype.Course">
<property name="cname" value="Spring5框架课程"></property>
</bean>
<bean id = "couse2" class="collectiontype.Course">
<property name="cname" value="SpringBoot框架"></property>
</bean>
1.6、工厂Bean
在配置文件定义bean类型可以和返回类型不一样。
import org.springframework.beans.factory.FactoryBean;
//工厂bean 用泛型 返回泛型中的类
public class Mybean implements FactoryBean<Course> {
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("多多学习spring");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
1.7、Bean的作用域
singleton单实例,prototype多实例对象
1.当设置scope是singleton时,在加载spring配置文件时会创建单实例对象。
2.当设置scope是prototype时,在调用getBean方法时创建多实例对象。
<bean id = "booklist" class="collectiontype.Book" scope = "prototype"><!--作用域 多实例对象-->
<property name="booklist" ref="listbook"></property>
</bean>
1.8、Bean的生命周期
bean的后置处理器
- 通过构造器创建bean实例(无参数构造)
- 为bean的属性设置值和对其他bean引用(调用set方法)
- 把bean实例传递bean的后置处理器方法 postProcessBeforeInitialization()
- 调用bean的初始化的方法(需要进行配置初始化的方法)
- 把bean实例传递bean的后置处理器方法 postProcessAfterInitialization()
- 可以使用bean(对象获取到了)
- 当容器关闭时候,调用bean的销毁方法(需要进行配置销毁的方法)
1.9、自动装配
<!--自动装配
bean标签属性值autowrite,配置自动装配
autowrite属性值常用两个值:
byName根据属性名称注入:注入bean的id值要与类属性名字相同 这里都是(dept)
byType根据属性类型称注入
-->
<bean id ="emp" class="collectiontype.autowrite.Emp" autowire="byName"></bean>
<bean id = "dept" class="collectiontype.autowrite.Dept"></bean>