IOC
管理Bean(xml)
注入数组,List,Map,Set
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www .w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="shuzu" class="com.hncj.beans.ShuZu">
<property name="names">
<array>
<value>小王</value>
<value>小黄</value>
<value>小李</value>
<value>小江</value>
</array>
</property>
</bean>
<bean id="list" class="com.hncj.beans.LIst">
<property name="names">
<list>
<value>小王</value>
<value>小黄</value>
<value>小李</value>
<value>小江</value>
</list>
</property>
</bean>
<bean id="map" class="com.hncj.beans.Maps">
<property name="name_age">
<map>
<entry key="小王" value="12"></entry>
<entry key="小黄" value="22"></entry>
<entry key="小李" value="32"></entry>
<entry key="小江" value="42"></entry>
</map>
</property>
</bean>
<bean id="set" class="com.hncj.beans.Sets">
<property name="sets">
<set>
<value>小王</value>
<value>小黄</value>
<value>小李</value>
<value>小江</value>
</set>
</property>
</bean>
</beans>
对象注入LIst<其他对象>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.hncj.bean2.Student">
<property name="course">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
<ref bean="course3"></ref>
<ref bean="course4"></ref>
</list>
</property>
</bean>
<bean id="course1" class="com.hncj.bean2.Course">
<property name="cname" value="Spring5课程"></property>
</bean>
<bean id="course2" class="com.hncj.bean2.Course">
<property name="cname" value="Mybatis课程"></property>
</bean>
<bean id="course3" class="com.hncj.bean2.Course">
<property name="cname" value="Spring MVC课程"></property>
</bean>
<bean id="course4" class="com.hncj.bean2.Course">
<property name="cname" value="SpringBoot课程"></property>
</bean>
</beans>
IOC操作Bean管理(FactoryBean)
- Spring 分为两类Bean ,普通Bean和FactoryBean
-普通Bean : 在XMl中定义的Bean类就是其返回类型
-工厂Bean : 在XMl定义的Bean类可以和其发挥类型不一样
工厂Bean
public class MyBean implements FactoryBean<String> {
@Override
public String getObject() throws Exception {
return "Hello FactoryBean";
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mybean" class="com.hncj.bean3.MyBean"></bean>
</beans>
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
String myBean = context.getBean("mybean", String.class);
System.out.println(myBean);
}
IOC操作Bean的作用域
- 在Spring里,设置创建Bean的实例是单实例还是多实例
- 在Spring中,创建的Bean实例默认是单实例(实例的地址值相同)
- 在Spring的配置文件bean标签里有scope用于设置是单实例还是多实例
- 默认值 singleton 表示是单实例,Spring在加载配置文件时就创建
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mybean" class="com.hncj.bean3.MyBean" scope="singleton"></bean>
</beans>
- prototype 表示是多实例,在context.getBean()才会去创建对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mybean" class="com.hncj.bean3.MyBean" scope="prototype"></bean>
</beans>
IOC操作Bean的生命周期
bean的声明周期
- 1.通过构造器创建Bean实例(无参构造器)
- 2.为bean的属性设置值和对其他bean的引用(调用set方法)
- 3.调用bean的初始化的方法(需要进行配置初始化方法)
- 4.bean可以使用了(对象获取到了)
- 5.当容器关闭时,调用bean的销毁的方法(需要进行配置销毁方法)
public class Person {
private String name;
public Person() {
System.out.println("无参数构造器");
}
public Person(String name) {
this.name = name;
}
public void setName(String name) {
System.out.println("使用Set()设置对象属性");
this.name = name;
}
public void inintMethod(){
System.out.println("初始化方法");
}
public void destoryMethod(){
System.out.println("销毁方法");
}
public void user(){
System.out.println("使用对象的方法");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.hncj.bean4.Person" init-method="inintMethod" destroy-method="destoryMethod">
<property name="name" value="刘倩"></property>
</bean>
</beans>
@Test
public void test4(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
Person person = context.getBean("person", Person.class);
person.user();
context.close();
}
无参数构造器
使用Set()设置对象属性
初始化方法
使用对象的方法
销毁方法
XML自动装配
- 根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="com.hncj.bean5.Emp" autowire="byType">
</bean>
<bean id="dpt" class="com.hncj.bean5.Dpt"></bean>
</beans>