spring bean 基于xml的4中初始化方法

时间:2022-11-15 21:20:27

本例主要介绍基于xml文件配置的4中初始化方法
1、使用property配置Bean
2、使用P标签简化配置Bean
3、使用构造函数初始化Bean
4、使用基础来初始化Bean

applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"
>


<!-- 1. 使用property初始化Bean属性 -->
<bean id="user1" class="cn.com.infcn.bean.User">
<property name="userName" value="lisi" />
<property name="age">
<value>20</value>
</property>

<property name="cars">
<!-- 使用 list 元素来装配集合属性 -->
<list>
<ref bean="audiCar" />
<ref bean="bmwCar" />
</list>
</property>
</bean>

<!-- 使用util标签:声明集合类型的 bean -->
<util:list id="cars">
<ref bean="audiCar" />
<ref bean="bmwCar" />
</util:list>

<!-- 2. 使用P标签简化bean配置 -->
<bean id="user2" class="cn.com.infcn.bean.User" p:userName="zhangsan"
p:age="1" p:cars-ref="cars">
</bean>


<!-- 3. 使用构造函数进行初始化bean -->
<bean id="user3" class="cn.com.infcn.bean.User">
<constructor-arg>
<value>wangwu</value>
</constructor-arg>
<constructor-arg value="5"></constructor-arg>
</bean>

<!-- 4. bean 的配置能够继承吗 ? 使用 parent 来完成继承 -->
<bean id="user4" parent="user3" p:cars-ref="cars"></bean>


<bean id="audiCar" class="cn.com.infcn.bean.Car" p:brand="audi"></bean>
<bean id="bmwCar" class="cn.com.infcn.bean.Car" p:brand="bmw"></bean>
</beans>

Car.java

package cn.com.infcn.bean;

/**
* @author jijs
*/

public class Car {
private String brand;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@Override
public String toString() {
return brand;
}
}

User.java

package cn.com.infcn.bean;
import java.util.List;

/**
* @author jijs
*/

public class User {
private String userName;
private int age;
private List<Car> cars;

public User() {
}
public User(String userName, int age) {
this.userName = userName;
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "User [userName=" + userName + ", age=" + age + ", cars=" + cars
+ "]";
}
}

BeanInstantiation.java

package cn.com.infcn.springdemo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.infcn.bean.User;

/**
* @author jijs
*/

public class BeanInstantiation {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 1、使用property配置Bean
printUser(ac, "user1");
// 2、使用P标签简化配置Bean
printUser(ac, "user2");
// 3、使用使用构造初始化Bean
printUser(ac, "user3");
// 4、使用基础来初始化Bean
printUser(ac, "user4");
}

private static void printUser(ApplicationContext ac, String beanName) {
User user = User.class.cast(ac.getBean(beanName));
System.out.println(user);
}
}

输出结果

User [userName=lisi, age=20, cars=[audi, bmw]]
User [userName=zhangsan, age=1, cars=[audi, bmw]]
User [userName=wangwu, age=5, cars=null]
User [userName=wangwu, age=5, cars=[audi, bmw]]

本人简书blog地址:http://www.jianshu.com/u/1f0067e24ff8    
点击这里快速进入简书

GIT地址:http://git.oschina.net/brucekankan/
点击这里快速进入GIT