bean.xml
<?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-3.0.xsd">
<!--
bean标签:
id属性:
Bean实例的名称(唯一)
name属性:
和id属性相似,都能代表一个bean实例,可以设置多个,用、隔开
class属性:
Bean类全名
scope属性:
singleton 单例模式,默认值
prototype 原型模式
request 一次请求
session 一次会话
init-method属性:
用于指定对象实例化后,调用的初始化方法
destroy-method:
用于指定对象在销毁时,调用的方法
parent:
设置该bean配置的父bean配置,子配置必须向上兼容父配置,可以通过构造参数、自动装配属性、生命周期等与父配置进行区别
factory-bean:
工厂bean名称
factory-method
工厂方法名称,要求方法不能是静态方法,参数名称应与构造参数名称相对应
depends-on
指定在创建该bean之前应该创建的bean对象,它们两个之间可能没有任何关系,多个用“,”隔开
lazy-init
创建bean的模式,默认为false
primary
在有多个匹配的bean时,优先使用该bean
-->
<bean id="person" class="cn.xiedacon.test.domain.Person" name="user1,user2">
<!--
construor-arg标签:
用于指定一个构造参数,通过传入相应的构造参数进行Bean实例化
index属性:声明当前是第几个构造参数(从0开始)
name属性:构造参数名称
type属性:声明构造参数的类型,不设置使默认进行强转,设置时按设置类型注入不进行强转
如:设置为Integer,构造函数中为int时,报错
value属性:直接指定属性值
ref属性:将属性值指定为某个存在bean的引用 id,name
-->
<constructor-arg index="" type="" value="" ref=""/>
<!--
property标签:
用于对Bean实例中的属性注入值
name:属性名
value:直接指定属性值
ref:将属性值指定为某个存在bean的引用 id,name
-->
<property name="" value="" ref="">
<!-- 将属性值设为null -->
<null></null>
<!-- 与value属性相同 -->
<value type=""></value>
<!-- 与ref属性相同 -->
<ref/>
<!-- 将属性值指定为对应的id,是String类型 -->
<idref/>
<!-- 将属性值指定为一个内部匿名Bean实例,该匿名Bean实例无法被外界访问 -->
<bean class="Bean类全名"/>
<!-- 数组标签:
value-type:值类型
merge:是否合并,默认为否
-->
<array>
<value></value>
</array>
<!-- set集合标签:与数组标签差不多 -->
<set>
<value></value>
</set>
<!-- list集合标签,与数组标签差不多 -->
<list>
<value></value>
</list>
<!-- map集合标签:
key-type:键类型
value-type:值类型
-->
<map key-type="" value-type="">
<!-- 键值对标签:
key:键
key-ref:键引用
value:值
value-ref:值引用
-->
<entry key="" key-ref="" value="" value-ref="">
<!-- 与键属性相同 -->
<key></key>
</entry>
</map>
<!-- Properties类型标签 -->
<props>
<prop key="properties元素的key">properties元素的value</prop>
</props>
</property>
</bean>
</beans>
scope属性演示:
bean.xml
<?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-3.0.xsd">
<bean id="singletonPerson" class="cn.xiedacon.test.domain.Person" scope="singleton"></bean>
<bean id="prototypePerson" class="cn.xiedacon.test.domain.Person" scope="prototype"></bean>
<bean id="requestPerson" class="cn.xiedacon.test.domain.Person" scope="request"></bean>
<bean id="sessionPerson" class="cn.xiedacon.test.domain.Person" scope="session"></bean>
</beans>
Person.java
package cn.xiedacon.test.domain;
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
Test1.java
package cn.xiedacon.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.xiedacon.test.domain.Person;
public class Test1 {
@Test
public void test() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person1 = (Person) context.getBean("singletonPerson");
person1.setId(1);
person1.setName("aaa");
Person person2 = (Person) context.getBean("singletonPerson");
Person person3 = (Person) context.getBean("prototypePerson");
person3.setId(2);
person3.setName("bbb");
Person person4 = (Person) context.getBean("prototypePerson");
System.out.println("singletonPerson1:"+person1);
System.out.println("singletonPerson2:"+person2);
System.out.println(person1.equals(person2));
System.out.println("prototypePerson1:"+person3);
System.out.println("prototypePerson2:"+person4);
System.out.println(person3.equals(person4));
}
}
输出结果:
singletonPerson1:Person [id=1, name=aaa]
singletonPerson2:Person [id=1, name=aaa]
true
prototypePerson1:Person [id=2, name=bbb]
prototypePerson2:Person [id=0, name=null]
false
由上可知:
scope=”singleton”时,返回同一个对象
scope=”prototype”时,返回不同对象
scope=”request”和scope=”session”,需要到web容器中演示
init-method,destroy-method属性演示
bean.xml
<?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-3.0.xsd">
<bean id="person" class="cn.xiedacon.test.domain.Person" init-method="init" destroy-method="destroy"></bean>
</beans>
Person.java
package cn.xiedacon.test.domain;
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
public void init(){
System.out.println("对象被创建啦!!!");
}
public void destroy(){
System.out.println("对象被销毁啦!!!");
}
}
Test1.java
package cn.xiedacon.test;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.xiedacon.test.domain.Person;
public class Test1 {
@Test
public void test() {
System.out.println("创建对象");
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person = (Person) context.getBean("person");
person.setId(1);
person.setName("aaa");
System.out.println(person);
System.out.println("销毁对象");
context.close();
}
}
输出结果:
创建对象
对象被创建啦!!!
Person [id=1, name=aaa]
销毁对象
对象被销毁啦!!!
factory-bean,factory-method属性演示
bean.xml
<?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-3.0.xsd">
<bean id="person1" class="cn.xiedacon.test.domain.Person" factory-bean="factory" factory-method="getPerson"></bean>
<bean id="person2" class="cn.xiedacon.test.domain.Person" factory-bean="factory" factory-method="getPerson">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="aaa"></constructor-arg>
</bean>
<bean id="factory" class="cn.xiedacon.test.Factory"></bean>
</beans>
Person.java
package cn.xiedacon.test.domain;
public class Person {
private int id;
private String name;
public Person() {
super();
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
Factory.java
package cn.xiedacon.test;
import cn.xiedacon.test.domain.Person;
public class Factory {
public Person getPerson(){
return new Person();
}
public Person getPerson(int id,String name){
return new Person(id, name);
}
}
Test1.java
package cn.xiedacon.test;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.xiedacon.test.domain.Person;
public class Test1 {
@Test
public void test() {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person1 = (Person) context.getBean("person1");
Person person2 = (Person) context.getBean("person2");
System.out.println(person1);
System.out.println(person2);
}
}
输出结果:
Person [id=0, name=null]
Person [id=1, name=aaa]
merge属性演示
bean.xml
<?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-3.0.xsd">
<bean id="base" class="cn.xiedacon.test.domain.Person">
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
<bean id="person1" class="cn.xiedacon.test.domain.Person" parent="base">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="aaa"></constructor-arg>
<property name="list">
<list merge="default">
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
<bean id="person2" class="cn.xiedacon.test.domain.Person" parent="base">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="aaa"></constructor-arg>
<property name="list">
<list merge="false">
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
<bean id="person3" class="cn.xiedacon.test.domain.Person" parent="base">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="aaa"></constructor-arg>
<property name="list">
<list merge="true">
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
</beans>
Person.java
package cn.xiedacon.test.domain;
import java.util.List;
public class Person {
private int id;
private String name;
private List<Integer> list;
public Person() {
super();
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", list=" + list + "]";
}
}
Test1.java
package cn.xiedacon.test;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.xiedacon.test.domain.Person;
public class Test1 {
@Test
public void test() {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person1 = (Person) context.getBean("person1");
Person person2 = (Person) context.getBean("person2");
Person person3 = (Person) context.getBean("person3");
System.out.println("merge=default:" + person1);
System.out.println("merge=false:" + person2);
System.out.println("merge=true:" + person3);
}
}
输出结果:
merge=default:Person [id=1, name=aaa, list=[1, 2, 3]]
merge=false:Person [id=1, name=aaa, list=[1, 2, 3]]
merge=true:Person [id=1, name=aaa, list=[1, 2, 3, 1, 2, 3]]
时间: 2024-10-08 23:30:37