springbean的详细配置,装配对象,集合,map

xml测试文档


<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 id="helloWorld" class="com.dasenlin.test.springmodel.HelloWorld">
        <property name="name" value="Spring">property>
    bean>

    <bean id="car" class="com.dasenlin.test.springmodel.Car">
        <constructor-arg value="Audi" index="0">constructor-arg>
        <constructor-arg value="Shanghai" index="1">constructor-arg>
        <constructor-arg type="int">
            <value>240value>
        constructor-arg>
    bean>
    <bean id="car2" class="com.dasenlin.test.springmodel.Car">
        <constructor-arg value="BMW" index="0">constructor-arg>
        <constructor-arg index="1">
                <value>]]>value>
        constructor-arg>
        <constructor-arg value="210000.15" type="double">constructor-arg>
    bean>

    <bean id="person" class="com.dasenlin.test.springmodel.Person">
            <property name="name" value="Tom">property>
            <property name="age" value="20">property>

            

            
            
            
    bean>
    
    <bean id="person3" class="com.dasenlin.test.springmodel.collection.Person">
        <property name="name" value="XiaoMa">property>
        <property name="age" value="30">property>
        <property name="carli">
            <list>
                <ref bean="car4"/>
                <ref bean="car5"/>
                <ref bean="car6"/>
                <bean class="com.dasenlin.test.springmodel.collection.Car">
                    <constructor-arg value="QQ">constructor-arg>
                    <constructor-arg value="qirui">constructor-arg>
                    <constructor-arg value="50000">constructor-arg>
                    <constructor-arg value="156.12">constructor-arg>
                bean>
            list>
        property>
    bean>
    <bean id="car4" class="com.dasenlin.test.springmodel.collection.Car">
        <constructor-arg value="BMW">constructor-arg>
        <constructor-arg value="shanghai">constructor-arg>
        <constructor-arg value="210000">constructor-arg>
        <constructor-arg value="456.12">constructor-arg>
    bean>
    <bean id="car5" class="com.dasenlin.test.springmodel.collection.Car">
        <constructor-arg value="Benci">constructor-arg>
        <constructor-arg value="sichuan">constructor-arg>
        <constructor-arg value="410000">constructor-arg>
        <constructor-arg value="456.12">constructor-arg>
    bean>
    <bean id="car6" class="com.dasenlin.test.springmodel.collection.Car">
        <constructor-arg value="dazhong">constructor-arg>
        <constructor-arg value="shanxi">constructor-arg>
        <constructor-arg value="10000">constructor-arg>
        <constructor-arg value="256.12">constructor-arg>
    bean>
    
    <bean id="newPerson" class="com.dasenlin.test.springmodel.collection.Person">
        <property name="name" value="xiaoDong">property>
        <property name="age" value="25">property>
        <property name="carli"><null/>property>
        <property name="carMap">
            <map>
                <entry key="AA" value-ref="car4">entry>
                <entry key="BB" value-ref="car5">entry>
                <entry key="CC" value-ref="car6">entry>
            map>
        property>
    bean>
    
    <util:list id="carss">
        <ref bean="car4"/>
        <ref bean="car5"/>
    util:list>
    <bean id="person4" class="com.dasenlin.test.springmodel.collection.Person">
        <property name="name" value="Jaxk">property>
        <property name="age" value="22">property>
        <property name="carli">
            <ref bean="carss"/>
        property>
    bean>
    
    <bean id="person5" class="com.dasenlin.test.springmodel.collection.Person" p:name="Queen" p:age="20" p:carli-ref="carss">bean>
beans>

实体类

package com.dasenlin.test.springmodel;

public class HelloWorld {
    private String name;

    public String getName2() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void hello(){
        System.out.println("hello:"+name);
    } 
}
package com.dasenlin.test.springmodel;
/**
 *构造方法注入 
 */
public class Car {
    private String brand;
    private String company;
    private int price;
    private double maxSpeed;

    public Car(String brand, String company, int price) {
        super();
        this.brand = brand;
        this.company = company;
        this.price = price;
    }
    public Car(String brand, String company, double maxSpeed) {
        super();
        this.brand = brand;
        this.company = company;
        this.maxSpeed = maxSpeed;
    }

    public double getMaxSpeed() {
        return maxSpeed;
    }
    public void setMaxSpeed(double maxSpeed) {
        this.maxSpeed = maxSpeed;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", company=" + company + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
    }

}
package com.dasenlin.test.springmodel;

public class Person {
    private String name;
    private Integer age;
    private Car car;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer 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 + "]";
    }

}
package com.dasenlin.test.springmodel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        //HelloWorld hellworld = new HelloWorld();
        //hellworld.setName("xiaomei");
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloworld = ctx.getBean(HelloWorld.class);
        helloworld.hello();
        Car car=(Car) ctx.getBean("car");
        Car car2=(Car) ctx.getBean("car2");
        Person person = (Person) ctx.getBean("person");
        System.out.println(car.toString());
        System.out.println(car2.toString());
        System.out.println(person.toString());
    }

}

第二部分,集合实体

package com.dasenlin.test.springmodel.collection;

public class Car {
    private String brand;
    private String company;
    private Double price;
    private Double speedMax;

    public Car(String brand, String company, Double price, Double speedMax) {
        super();
        this.brand = brand;
        this.company = company;
        this.price = price;
        this.speedMax = speedMax;
    }

    @Override
    public String toString() {
        return "Car [brand=" + brand + ", company=" + company + ", price=" + price + ", speedMax=" + speedMax + "]";
    }

}
package com.dasenlin.test.springmodel.collection;

import java.util.List;
import java.util.Map;

public class Person {
    private String name;
    private Integer age;
    private List carli;
    private MapcarMap;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public List getCarli() {
        return carli;
    }
    public void setCarli(List carli) {
        this.carli = carli;
    }

    public Map getCarMap() {
        return carMap;
    }

    public void setCarMap(Map carMap) {
        this.carMap = carMap;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", carli=" + carli + ", carMap=" + carMap + "]";
    }

}
package com.dasenlin.test.springmodel.collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person p =(Person) ctx.getBean("person3");
        System.out.println(p);
        Person newp = (Person) ctx.getBean("newPerson");
        System.out.println(newp);
        Person p4 = (Person) ctx.getBean("person4");
        System.out.println(p4);
        Person p5 = (Person)ctx.getBean("person5");
        System.out.println(p5);
    }

}

你可能感兴趣的:(spring)