首页 > 代码库 > spring 配置bean

spring 配置bean

Main(测试方法)

public class Main {

 

public static void main(String[] args) { 

//1.创建Spring 的IOC容器对象;

//spring提供了两种IOC容器的实现

//1.BeanFactory:面向spring本身,,(就是底层的东西)

//2.ApplicationContext: 面向使用spring框架的开发者,她提供了更多高级特性,是BeanFactory的子接口

// ----ConfigurableApplicationContext:ApplicationContext的子接口;增加了两个方法,refresh()和close()

// -------主要实现类ClassPathXmlApplicationContext:从类路径下加载配置文件

//--------主要实现类FileSystemXmlApplicationContext:从文件系统中加载配置文件

//applicationContext 代表IOC容器,

ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");

//2.从IOC容器中获取beans实例

HelloWorld helloWorld =(HelloWorld) cxt.getBean("helloWorld");

   //3.调用hello方法

helloWorld.hello();

Car car = (Car) cxt.getBean("car");

System.out.println(car);

 

Car car2 = (Car)cxt.getBean("car2");

System.out.println(car2);

 

 

Person person = (Person) cxt.getBean("person");

System.out.println(person);

 

Person1 person3 = (Person1)cxt.getBean("person3");

System.out.println("集合属性:::"+person3);

 

Person2 person2 = (Person2)cxt.getBean("person2");

System.out.println("Map属性:::"+person2);

 

 

DataSource dataSource = (DataSource) cxt.getBean("dataSource");

System.out.println(dataSource);

 

 

Person1 person4 = (Person1)cxt.getBean("person4");

System.out.println("集合属性:::"+person4);

 

Person2 person5 = (Person2)cxt.getBean("person5");

System.out.println("Map属性:::"+person5);

 

Person1 person6 = (Person1)cxt.getBean("person6");

System.out.println("p属性:::"+person6);

 

}

/**************************************************************************************/

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:util="http://www.springframework.org/schema/util"

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

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-4.0.xsd

">

 

<!-- 配置bean -->

<!-- class的路径一般都是包名+类名,中间用“.”分开

  class:bean的全类目名,通过反射的方式在IOC容器中穿件Bean,所以要求bean中有无参数的构造器

  id:标识容器中的bean,id值唯一

-->

<!-- property 是属性配置,这里是属性注入  -->

<bean id="helloWorld" class ="lixiuming.spring.beans.HelloWorld">

<property name="name" value=http://www.mamicode.com/"Spring"></property>

</bean>

 

 

 

<!-- 通过构造方法来配置bean属性 ,可以指定参数的位置和参数的类型,来区分重载的构造器  value值可以拿到外面,遇到特殊字符用CDATA-->

<bean id="car" class="lixiuming.spring.beans.Car">

<constructor-arg  index="0">

<value><![CDATA[<Shanghai>]]></value>

</constructor-arg>

<constructor-arg  index="1">

<value>shanghai</value>

</constructor-arg>

<constructor-arg value=http://www.mamicode.com/"30000000" index="2"></constructor-arg>

</bean>

 

<bean id="car2" class="lixiuming.spring.beans.Car">

<constructor-arg value=http://www.mamicode.com/"Audi" index="0"></constructor-arg>

<constructor-arg value=http://www.mamicode.com/"Shanghai" index="1"></constructor-arg>

<constructor-arg value=http://www.mamicode.com/"240" index="2" type="int"></constructor-arg>

</bean>

 

 

 

 

<!-- 配置person  

——引用其他外部Bean    ref="car2" 

——内部Bean创建   内部的bean 不能被外部引用

——级联属性

-->

<bean id="person" class="lixiuming.spring.beans.Person">

<property name="name" value=http://www.mamicode.com/"LiXiuming"></property>

<property name="age" value=http://www.mamicode.com/"18" ></property>

<!-- <property name="car" ref="car2"></property>

 --> <!-- 内部Bean引用 -->

<property name= "car">

<bean class="lixiuming.spring.beans.Car">

<constructor-arg value=http://www.mamicode.com/"Fode"></constructor-arg>

    <constructor-arg value=http://www.mamicode.com/"Changan"></constructor-arg>

    <constructor-arg value=http://www.mamicode.com/"200"  type="int"></constructor-arg>

</bean>

</property>

<property name="car.price" value=http://www.mamicode.com/"300000"></property>

</bean>

 

<!--  配置集合属性 -->

<bean id="person3"  class="lixiuming.spring.beans.Person1">

<property name="name" value=http://www.mamicode.com/"Mike"></property>

<property name="age" value=http://www.mamicode.com/"20"></property>

<property name="car" >

<list>

<ref bean="car"></ref>

<ref bean="car2"></ref>

<bean class="lixiuming.spring.beans.Car">

<constructor-arg value=http://www.mamicode.com/"Fode"></constructor-arg>

    <constructor-arg value=http://www.mamicode.com/"Changan"></constructor-arg>

    <constructor-arg value=http://www.mamicode.com/"200"  type="int"></constructor-arg>

</bean>

</list>

</property>

</bean>

 

<!-- 配置Map属性值 -->

<bean id="person2" class="lixiuming.spring.beans.Person2">

<property name="name" value=http://www.mamicode.com/"tom"></property>

<property name="age" value=http://www.mamicode.com/"25"></property>

<property name="car">

<map>

<entry key="aa" value-ref="car"></entry>

<entry key="bb" value-ref="car2"></entry>

</map>

</property>

</bean>

 

 

<!-- 配置Property属性值 需要导入util 命名空间-->

<bean id="dataSource" class="lixiuming.spring.beans.DataSource"  >

<property  name="properties">

<props>

<prop key="user"> root</prop>

<prop key="password"></prop>

<prop key="jdbcUrl">jdbc:mysql:///test</prop>

<prop key="driverClass">com.mysql.jdbc.Driver</prop>

</props>

</property>

</bean>

 

 

<!-- 配置单例的集合bean以供多个bean调用 -->

<util:list id="cars">

<ref bean="car"/>

<ref bean="car2"/>

</util:list>

 

<bean id="person4" class="lixiuming.spring.beans.Person1">

<property name="name" value=http://www.mamicode.com/"Jack"></property>

<property name="age" value=http://www.mamicode.com/"18"></property>

<property name="car" ref="cars"> </property>

</bean>

 

 

<!-- 配置单例的map  bean以供多个bean调用 -->

<util:map id="carsmap">

<entry key="aa" value-ref="car"></entry>

<entry key="bb" value-ref="car2"></entry>

</util:map>

 

<bean id="person5" class="lixiuming.spring.beans.Person2">

<property name="name" value=http://www.mamicode.com/"Jack"></property>

<property name="age" value=http://www.mamicode.com/"18"></property>

<property name="car" ref="carsmap"> </property>

</bean>

 

<!-- 配置P命名空间为bean的属性赋值,需要先导入P命名空间 -->

<bean id="person6" class="lixiuming.spring.beans.Person1" p:age="30" p:name="aaa" p:car-ref="cars"></bean>

 

 

 

 

<!-- 对比配置servlet

 <servlet>

<servlet-name>HelloServlet</servlet-name>

<servlet-class>com.bro.HelloServlet3</servlet-class>

</servlet>

 

<servlet-mapping>

<servlet-name>HelloServlet</servlet-name>

<url-pattern>/HelloServlet</url-pattern>

</servlet-mapping> 

-->

 

</beans>

/**************************************************************************************/

附录

 

public class HelloWorld {

 

 

 

private String name;

 

 

 

public void setName(String name){

 

this.name = name;

 

System.out.println("setName:"+name);

 

}

 

 

 

public void hello(){

 

System.out.println("hello:"+name);

 

}

 

}

/**************************************************************************************/

 

public class Car {

 

private String brand;

private String corp;

private double price;

private int maxSpeed;

 

public String getBrand() {

return brand;

}

 

public void setBrand(String brand) {

this.brand = brand;

}

 

public String getCorp() {

return corp;

}

 

public void setCorp(String corp) {

this.corp = corp;

}

 

public double getPrice() {

return price;

}

 

public void setPrice(double price) {

this.price = price;

}

 

public int getMaxSpeed() {

return maxSpeed;

}

 

public void setMaxSpeed(int maxSpeed) {

this.maxSpeed = maxSpeed;

}

 

public Car(String brand, String corp, double price) {

super();

this.brand = brand;

this.corp = corp;

this.price = price;

}

 

public Car(String brand, String corp, int maxSpeed) {

super();

this.brand = brand;

this.corp = corp;

this.maxSpeed = maxSpeed;

}

 

@Override

public String toString() {

return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";

}

}

/**************************************************************************************/

public class DataSource {

private Properties properties;

public Properties getProperties() {

return properties;

}

 

public void setProperties(Properties properties) {

this.properties = properties;

}

 

@Override

public String toString() {

return "DataSource [properties=" + properties + "]";

}

/**************************************************************************************/

 

public class Person {

 

private String name;

private int age;

private Car car;

 

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Car getCar() {

return car;

}

public void setCar(Car car) {

this.car = car;

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";

}

 

}

 

/**************************************************************************************/

 

public class Person1 {

 

private String name;

private int age;

private List<Car> car;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public List<Car> getCar() {

return car;

}

public void setCar(List<Car> car) {

this.car = car;

}

@Override

public String toString() {

return "Person1 [name=" + name + ", age=" + age + ", car=" + car + "]";

}

 

}

 

/**************************************************************************************/

public class Person2 {

 

private String name;

private int age;

private Map<String,Car>car;

 

 

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Map<String, Car> getCar() {

return car;

}

public void setCar(Map<String, Car> car) {

this.car = car;

}

 

@Override

public String toString() {

return "Person2 [name=" + name + ", age=" + age + ", car=" + car + "]";

}

 

}

 

 

/**************************************************************************************/

 

spring 配置bean