- bean的生命周期:
singleton、prototype每次客户端请求prototype作用域的bean时,spring都会产生一个新的实例,spring容器无从知道它曾经创建了多少个prototype作用域bean,也无从知道这些prototype作用域bean什么时候才会被销毁。对于singleton作用域的bean,每次客户端代码请求时都返回同一个共享实例,客户端代码不能控制Bean的销毁,spring容器可以跟踪bean实例的产生、销毁。
作用域范围是单例的bean时,是在实例化容器的时候就实例化。
作用域范围是prototype的bean时,是在getBean的时候被实例化的。 - 生命周期行为
对于singleton作用域的Bean,spring容器知道Bean何时实例化结束、何时销毁,spring可以管理实例化结束之后和销毁之前的行为。管理Bean的生命周期行为主要有如下两个时机:注入依赖关系之后和即将销毁Bean之前。 - eclipse入门案例步骤
(1)导入jar包
(2)创建类
package cn.ioc;
public class User {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void add() {
System.out.println("add........");
}
}
(3)创建spring配置文件,配置创建类。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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="cn.itcast.ioc.User" scope="prototype"></bean>
</beans>
(4)测试
package cn.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIoc {
@Test
public void testUserIoc() {
ApplicationContext context = new ClassPathXmlApplicationContext("demo1.xml");
User user = (User) context.getBean("user");
System.out.println(user);
user.add();
}
}
- bean实例化三种方式(控制反转的过程)
(1)使用类的无参数构造创建(重点)(上面的例子中)
(2)使用静态工厂创建
(3)使用实例工厂创建
//使用静态工厂创建
public class BeanFactory {
//静态的方法,返回Bean对象
public static Bean getBean() {
return new Bean();
}
}
<!-- xml中使用静态工厂创建对象 -->
<bean id="bean" class="cn.bean.BeanFactory" factory-method="getBean"></bean>
//使用实例工厂创建
public class BeanFactory {
//普通的方法,返回Bean对象,bean是写的一个类
public Bean getBean() {
return new Bean();
}
}
<!-- xml中 使用实例工厂创建对象 -->
<!-- 先创建工厂对象 -->
<bean id="bean3Factory" class="cn.itcast.bean.Bean3Factory"></bean>
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
- 属性注入
(1)使用set方法注入
(2)使用有参数构造注入
(3)使用接口注入(spring不支持)
<!-- xml 使用有参数构造注入属性 -->
<bean id="demo" class="cn.property.PropertyDemo">
<!-- 使用有参构造注入 -->
<constructor-arg name="username" value="小h"></constructor-arg>
</bean>
//有参数构造注入
public class PropertyDemo {
private String username;
public PropertyDemo1(String username) {
this.username = username;
}
<!-- xml 使用set方法注入属性 -->
<bean id="book" class="cn.property.Book">
<!-- 注入属性值
name属性值:类里面定义的属性名称
value属性:设置具体的值
-->
<property name="bookname" value="Spring揭秘"></property>
</bean>
public class Book {
private String bookname;
//set方法
public void setBookname(String bookname) {
this.bookname = bookname;
}
}
- 注入复杂类型属性(数组、list集合、map集合、properties类型)
<!-- 注入复杂类型属性值 -->
<bean id="person" class="cn.property.Person">
<!-- 数组 -->
<property name="arrs">
<list>
<value>小王</value>
<value>小马</value>
<value>小宋</value>
</list>
</property>
<!-- list -->
<property name="list">
<list>
<value>小奥</value>
<value>小金</value>
<value>小普</value>
</list>
</property>
<!-- map -->
<property name="map">
<map>
<entry key="aa" value="lucy"></entry>
<entry key="bb" value="mary"></entry>
<entry key="cc" value="tom"></entry>
</map>
</property>
<!-- properties -->
<property name="properties">
<props>
<prop key="driverclass">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
</props>
</property>
</bean>
public class Person {
private String pname;
private String[] arrs;
private List<String> list;
private Map<String,String> map;
private Properties properties;
public void setPname(String pname) {
this.pname = pname;
}
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void test1() {
System.out.println("arrs:"+arrs);
System.out.println("list:"+list);
System.out.println("map:"+map);
System.out.println("properties"+properties);
}
}