spring基础(2)-注入方式和配置集合属性

1.注入方式包括三种:属性注入,构造器注入,工厂方法。通常情况下常用的是前两种。
(1)属性注入:通过setter方法

<!--通过构造方法来配置bean的属性 -->
    <bean id="car" class="com.atguigu.spring.beans.Car">
    <constructor-arg value="baoma" index="0"></constructor-arg>
    <constructor-arg value="qingdao" index="1"></constructor-arg>
    <constructor-arg value="30000" type="double"></constructor-arg>
    </bean>

(2)构造器注入

<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型! 以区分重载的构造器 -->
    <bean id="car2" class="com.atguigu.spring.beans.Car">
    <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
    <!-- 如果字面值包含特殊字符可以使用 <![CDATA[]] 包裹起来-->
    <!-- 属性值也可以用value子节点进行直接配置 -->
    <constructor-arg  type="java.lang.String">
        <value><![CDATA[<qingdao~>]]></value>
    </constructor-arg>
    <constructor-arg  type="int">
        <value>250</value>
    </constructor-arg>
    </bean>


    <bean id="person" class="com.atguigu.spring.beans.Person">
        <property name="name" value="Jim" ></property>
        <property name="age" value="24"></property>
        <!-- 使用property的ref属性建立bean之间的引用关系 -->
        <!-- 
        <property name="car" ref="car2"></property>
        <property name="car" >
            <ref bean="car2"/>
        </property>
        -->
         <!-- 内部bean  不能被外部引用 只能在内部使用 -->
         <property name="car">
            <bean class="com.atguigu.spring.beans.Car">
                <constructor-arg value="ford"></constructor-arg>
                <constructor-arg value="changan"></constructor-arg>
                <constructor-arg value="20000" type="double"></constructor-arg>
            </bean>
         </property>
    </bean>

相关的测试类

package com.atguigu.spring.beans;

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 + "]";
    }



}
package com.atguigu.spring.beans;

public class Car {

    private String brand;
    private String corp;
    private double price;
    private int 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 + "]";
    }



}
package com.atguigu.spring.beans;

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

public class Main {

    public static void main(String[] args){

        /*//创建HelloWorld 的一个对象,此处的调用等同于下面的Spring调用
        HelloWorld helloworld = new HelloWorld();
        //为name属性赋值
        helloworld.setName("atguiug");*/

        //1.创建Spring 的IOC容器对象
        //ApplicationContext代表IOC容器
        //ClassPathXmlApplicationContext :是ApplicationContext接口的实现类,该类从类路径下加载配置文件
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.从IOC容器中获取bean实例
        //利用id定位到IOC容器中的bean
        HelloWorld helloworld = (HelloWorld) ctx.getBean("helloworld2");
        //利用类型返回IOC容器中的bean,但要求IOC容器中必须只能有一个该类型的bean
        //HelloWorld helloworld = ctx.getBean(HelloWorld.class);
        System.out.println(helloworld);

        //调用hello方法
        helloworld.hello();

        //通过构造方法来配置bean的属性
        Car car = (Car) ctx.getBean("car");
        System.out.println(car);

        car = (Car) ctx.getBean("car2");
        System.out.println(car);

        Person person=(Person) ctx.getBean("person");
        System.out.println(person);


    }
}

2.配置集合属性
list,Map,Properties等。
(1)spring代码

<!-- 测试如何配置集合属性 -->
    <bean id="person3" class="com.atguigu.spring.beans.collection.Person">
        <property name="name" value="Mike"></property>
        <property name="age" value="24"></property>
        <property name="cars">
        <!-- 使用list节点为list类型的属性赋值 -->
            <list>
                <ref bean="car"></ref>
                <ref bean="car2"/>
            </list>
        </property>
    </bean>

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

    <bean id="newPerson" class="com.atguigu.spring.beans.collection.NewPerson">
        <property name="name" value="rose"></property>
        <property name="age" value="28"></property>
        <property name="cars">
        <!-- 使用map节点及map-的entry的子节点配置map类型的成员变量 -->
            <map>
                <entry key="AA" value-ref="car"></entry>
                <entry key="BB" value-ref="car2"></entry>
            </map>
        </property>
    </bean>


    <!-- 配置Properties属性值 -->
    <bean id="dataSource" class="com.atguigu.spring.beans.collection.DataSource">
        <property name="properties">
        <!-- 使用props和 prop子节点为属性赋值-->
            <props>
                <prop key="user">root</prop>
                <prop key="password">321456</prop>
                <prop key="jdbcURL">jdbc://mysql</prop>
                <prop key="driverClass">com.mysql.jdbc.Driver</prop>

            </props>
        </property>
    </bean>

    <!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间 -->
    <util:list id="cars">
        <ref bean="car"/>
        <ref bean="car2"/>
    </util:list>

    <bean id="person4" class="com.atguigu.spring.beans.collection.Person">
        <property name="name" value="wangwag"></property>
        <property name="age" value="29"></property>
        <property name="cars" ref="cars"></property>
    </bean>

    <!-- 通过p命名空间为bean的属性赋值,需要先导入p命名空间 -->
    <bean id="person5" class="com.atguigu.spring.beans.collection.Person" p:age="30"
        p:name="rose" p:cars-ref="cars" ></bean>

(2)相关测试代码

package com.atguigu.spring.beans.collection;

import java.util.List;

import com.atguigu.spring.beans.Car;



public class Person {

    private String name;
    private int age;
    private List<Car> cars;
    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=" + cars + "]";
    }
}
package com.atguigu.spring.beans.collection;

import java.util.Properties;

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 + "]";
    }


}
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 act = new ClassPathXmlApplicationContext("applicationContext.xml");
    //从IOC容器中获取bean实例
    Person person = (Person) act.getBean("person5");
    System.out.println(person);

    NewPerson  newPerson = (NewPerson) act.getBean("newPerson");
    System.out.println(newPerson);

    DataSource dataSource = act.getBean(DataSource.class);
    System.out.println(dataSource.getProperties());
    }


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

import java.util.Map;

import com.atguigu.spring.beans.Car;

public class NewPerson {


    private String name;
    private int age;
    private Map<String,Car> cars;

    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> getCars() {
        return cars;
    }
    public void setCars(Map<String, Car> cars) {
        this.cars = cars;
    }
    @Override
    public String toString() {
        return "NewPerson [name=" + name + ", age=" + age + ", cars=" + cars + "]";
    }


}

以上仅作为学习参考使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值