码迷,mamicode.com
首页 > 编程语言 > 详细

Spring学习-day02

时间:2018-03-09 21:24:20      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:Spring

<!-- 属性值也可以使用 value 子节点进行配置,如果含有特殊字符使用<![CDATA[<>]]>包裹起来 --> <bean id="car1" class="com.atguigu.spring.beans.Car"> <constructor-arg value="2000" type="double"></constructor-arg> <constructor-arg type="java.lang.String"> <value><![CDATA[<shanghai^>]]></value> </constructor-arg> <constructor-arg value="100" type="int"></constructor-arg> </bean> <bean id="person" class="com.atguigu.spring.beans.Person"> <property name="name" value="Tom"></property> <property name="age" value="20"></property> <!-- 可以使用 property 的 ref 属性建立 bean 之间的引用关系 --> <!-- 1. <property name="car" ref="car1"></property> 2. 使用 ref 子节点配置 <property name="car"> <ref bean="car"/> </property> --> <!-- 使用内部 bean, 不能在外部使用只能在内部使用 --> <property name="car"> <bean class="com.atguigu.spring.beans.Car"> <constructor-arg value="1000" type="double"></constructor-arg> <constructor-arg value="Ford"></constructor-arg> <constructor-arg value="changan"></constructor-arg> </bean> </property> </bean> <bean id="person2" class="com.atguigu.spring.beans.Person"> <constructor-arg value="xiaoming"></constructor-arg> <constructor-arg value="18"></constructor-arg> <!-- 测试 null <constructor-arg> <null/> </constructor-arg> --> <!-- 为级联属性赋值 . 注意:属性需要先初始化然后才可以为级联属性赋值,否则产生异常,与struts2不同--> <constructor-arg ref="car"></constructor-arg> <property name="car.maxSpeed" value="20"></property> </bean> <!-- 测试如何配置集合属性 --> <!-- list 集合 --> <bean id="person3" class="com.atguigu.spring.beans.collection.Person"> <property name="name" value="Mark" ></property> <property name="age" value="22"></property> <!-- <property name="cars"> <list> <ref bean="car"/> <ref bean="car1"/> </list> </property> --> </bean> <!-- map 集合 --> <bean id="mapPerson" class="com.atguigu.spring.beans.collection.Person"> <property name="name" value="TT"></property> <property name="age" value="23"></property> <!-- 使用 map 节点 及 map 的 entry 子节点配置 Map 类型的成员变量 --> <property name="cars1"> <map> <entry key="AAA" value-ref="car"></entry> <entry key="BBB" value-ref="car1"></entry> </map> </property> </bean> <!-- 配置 Properties 属性值 --> <bean id="dataSource" class="com.atguigu.spring.beans.collection.DataSource"> <property name="properties"> <!-- 使用 props 和 prop 子节点来为 Properties 属性赋值 --> <props> <prop key="user">root</prop> <prop key="password">1234</prop> <prop key="jdbcUrl">jdbc:mysql:///test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> <!-- 配置独立的集合 bean,以供多个 bean 进行引用,需要导入 util 命名空间 --> <util:list id="cars"> <ref bean="car1"/> <ref bean="car"/> </util:list> <bean id="person4" class="com.atguigu.spring.beans.collection.Person"> <property name="name" value="jack"></property> <property name="age" value="11"></property> <property name="cars1" ref="cars"></property> </bean>
package com.atguigu.spring.beans.collection;

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

public class Main {

    public static void main(String[] args) {
        //创建 spring 的 IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("one.xml");

        //从 IOC 容器对象中获取 Bean 的实例
//      Person person = (Person) ctx.getBean("person3");
//      Person person2 = (Person) ctx.getBean("mapPerson");
//      DataSource dataSource = ctx.getBean(DataSource.class);
        Person person4 = (Person) ctx.getBean("person4");

        //打印
        //System.out.println(person);
//      System.out.println(person2);
//      System.out.println(dataSource);
        System.out.println(person4);
    }

}
package com.atguigu.spring.beans.collection;

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

import com.atguigu.spring.beans.Car;

public class Person {
    private String name;
    private int age;
    //private List<Car> cars;
    private Map<String,Car> cars1;

    public Map<String, Car> getCars1() {
        return cars1;
    }
    public void setCars1(Map<String, Car> cars1) {
        this.cars1 = cars1;
    }

    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> getCars() {
//      return cars;
//  }
//  public void setCars(List<Car> cars) {
//      this.cars = cars;
//  }

//  @Override
//  public String toString() {
//      return "Person [name=" + name + ", age=" + age + ", cars=" + cars1 + "]";
//  }

//  public Person(String name, int age, List<Car> cars) {
//      super();
//      this.name = name;
//      this.age = age;
//      this.cars = cars;
//  }
    public Person() {
        super();
    }

    public Person(String name, int age, Map<String, Car> cars1) {
        super();
        this.name = name;
        this.age = age;
        this.cars1 = cars1;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", cars1=" + cars1 + "]";
    }

}

Spring学习-day02

标签:Spring

原文地址:http://blog.51cto.com/13416247/2084685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!