spring配置bean的生命周期
本文内容纲要:spring配置bean的生命周期" title="bean的生命周期">bean的生命周期
配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-lazy-init="false">
<bean id="user1" class="com.colorlight.springscope.User" scope="singleton"
lazy-init="true">
<property name="id" value="1">
<property name="name" value="lijun">
<property name="password" value="199192">
<bean id="user2" class="com.colorlight.springscope.User" scope="prototype">
<property name="id" value="1">
<property name="name" value="lijun">
<property name="password" value="199192">
User类:
package com.colorlight.springscope;
public****class User {
private****intid;
private String name;
private String password;
public User(){
System.out.println("创建了User的实例!");
}
public****int getId() {
returnid;
}
public****void setId(int id) {
this.id = id;
}
public String getName() {
returnname;
}
public****void setName(String name) {
this.name = name;
}
public String getPassword() {
returnpassword;
}
public****void setPassword(String password) {
this.password = password;
}
}
测试类:
package com.colorlight.springscope;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public****class MainTest {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml", this.getClass());
// 单例
@Test
public****void test1() {
System.out.println("在getBean()调用之前");
User user1 = (User) ac.getBean("user1");
User user2 = (User) ac.getBean("user1");
System.out.println(user1 != null);
System.out.println(user1 == user2);
}
// 多例
@Test
public****void test2() {
System.out.println("在getBean()调用之前");
User user1 = (User) ac.getBean("user2");
User user2 = (User) ac.getBean("user2");
System.out.println(user1 != null);
System.out.println(user1 == user2);
}
}
本文内容总结:spring配置bean的生命周期
原文链接:https://www.cnblogs.com/fabulousyoung/p/4073877.html
以上是 spring配置bean的生命周期 的全部内容, 来源链接: utcz.com/z/362480.html