一.??bean的作用域
1. Spring默认是单实例对象
package pojo;
public class User {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?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="user" class="pojo.User"></bean>
</beans>
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.User;
public class testSpring {
@Test
public void testUser() {
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("User.xml");
//获取配置文件创建的对象
User user1 = context.getBean("user", User.class);
User user2 = context.getBean("user",User.class);
System.out.println(user1); //pojo.User@3ce1e309 地址相同
System.out.println(user2); //pojo.User@3ce1e309
}
}
2. 设置单实例还是多实例
<?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">
<!-- 创建对象 -->
<!-- scope默认是singleton(单实例) 可以设置为prototype(多实例)-->
<bean id="user" class="pojo.User" scope="prototype"></bean>
</beans>
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.User;
public class testSpring {
@Test
public void testUser() {
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("User.xml");
//获取配置文件创建的对象
User user1 = context.getBean("user", User.class);
User user2 = context.getBean("user",User.class);
System.out.println(user1); //pojo.User@15ff3e9e 地址不同
System.out.println(user2); //pojo.User@5fdcaa40
}
}
二.??bean的生命周期
1.执行无参构造器创建bean实例;2.调用set方法,设置属性的值;3.执行初始化方法;4.获取创建的bean对象;5.执行销毁的方法?
package pojo;
public class User {
private String userName;
public User() {
System.out.println("1.执行无参构造器创建bean实例");
}
public void setUserName(String userName) {
this.userName = userName;
System.out.println("2.调用set方法,设置属性的值");
}
//创建执行的初始化方法
public void initMethod(){
System.out.println("3.执行初始化方法");
}
//创建执行的销毁的方法
public void destroyMethod(){
System.out.println("5.执行销毁的方法");
}
}
<?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="user" class="pojo.User" init-method="initMethod" destroy-method="destroyMethod">
<property name="userName" value="Tom"></property>
</bean>
</beans>
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.User;
public class testSpring {
@Test
public void testUser() {
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("User.xml");
//获取配置文件创建的对象
User user = context.getBean("user", User.class);
System.out.println("4.获取创建的bean对象");
//销毁bean实例
((ClassPathXmlApplicationContext) context).close();
}
}
// 1.执行无参构造器创建bean实例
// 2.调用set方法,设置属性的值
// 3.执行初始化方法
// 4.获取创建的bean对象
// 5.执行销毁的方法
|