spring5 (一) 属性配置-XML方式

创建一个maven工程

在pom.xml 添加spring的依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.softtool</groupId>
    <artifactId>spring001</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

    </dependencies>
</project>

创建实体类

package com.softtool;

public class HelloWorld {

    private  String name;

    public HelloWorld(){

    }

    public HelloWorld(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public void hello() {
        System.out.println(name);
    }
}


创建一个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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="helloWorld" class="com.softtool.HelloWorld">
        <property name="name" value="hello world"></property>
    </bean>
</beans>

创建测试类


public class Main {
    public static void main(String[] args) {

        ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld) ac.getBean("helloWorld");
        helloWorld.hello();;
    }
}

#运行程序


十一月 02, 2018 9:17:32 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46ee7fe8: startup date [Fri Nov 02 21:17:32 CST 2018]; root of context hierarchy
十一月 02, 2018 9:17:32 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
hello world

Process finished with exit code 0

运行成功了,控制台中打印出来了hello world

类图

我们看到, ApplicationContext 是一个接口,代表容器,他有两个实现类,一个是ClassPathXmlApplicationContext 一个是FileSystemXmlApplicationContext 我们使用的是ClassPathXmlApplicationContext 从类路径中加载配置文件。

读取类的方法有很多,如下

我们使用

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

getBean里的 helloWorld 就是对应applicationContext.xml里配置的helloWorld

这些getBean方法是ApplicationContext继承自BeanFactory

通过构造方法获取

创建两个类

Car

package com.softtool;

public class Car {
    private String name;
    private String color;

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}

Person

package com.softtool;

public class Person {

    private String name;
    private Car car;
    private Integer age;
    private Integer value;

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    private String remark
            ;

    public Person() {
    }

    public Integer getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", car=" + car +
                ", age=" + age +
                ", value=" + value +
                ", remark='" + remark + '\'' +
                '}';
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public Person(String name, Car car) {
        this.name = name;
        this.car = car;
    }

    public String getName() {
        return name;
    }

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

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

}


在配置文件中配置

 <bean id="car" class="com.softtool.Car">
        <property name="name" value="BMW"></property>
        <property name="color" value="Red"></property>
    </bean>

    <bean id="person" class="com.softtool.Person">
        <constructor-arg value="wang"></constructor-arg>
        <constructor-arg  ref="car"></constructor-arg>
    </bean>

运行

package com.softtool;

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

public class Main {


    public static void main(String[] args) {

        ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) ac.getBean("person");
        System.out.println(person.toString());

    }
}

运行结果

十一月 08, 2018 10:39:33 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46ee7fe8: startup date [Thu Nov 08 10:39:33 CST 2018]; root of context hierarchy
十一月 08, 2018 10:39:33 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Person{name='wang', car=Car{name='BMW', color='Red'}, age=null, value=null, remark='null'}

使用内部Bean

在配置文件中配置

 <bean id="person2" class="com.softtool.Person">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="20"></property>
        <property name="car">
            <bean class="com.softtool.Car">
                <property name="name" value="Ben"></property>
                <property name="color" value="Blue"></property>
            </bean>
        </property>
    </bean>

测试及程序

        Person person2 = (Person) ac.getBean("person2");
        System.out.println(person2.toString());
		//输出//shPerson{name='zhangsan', car=Car{name='Ben', color='Blue'}, age=20, value=null, remark='null'}

集合属性

测试类

package com.softtool;

import java.util.List;

public class SiCong {

    private String name;
    private List<Car> cars;

    public String getName() {
        return name;
    }

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

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }


    @Override
    public String toString() {
        return "SiCong{" +
                "name='" + name + '\'' +
                ", cars=" + cars +
                '}';
    }
}

配置xml

   <bean  class="com.softtool.SiCong">

        <property name="name" value="王思聪"/>
        <property name="cars">
            <list>
               <ref bean="car"></ref>
                <bean class="com.softtool.Car">
                    <property name="name" value="福特"></property>
                    <property name="color" value="Blue"></property>
                </bean>
            </list>
        </property>
    </bean>

测试及程序

        SiCong siCong = (SiCong) ac.getBean(SiCong.class);
        System.out.println(siCong.toString());
//SiCong{name='王思聪', cars=[Car{name='BMW', color='Red'}, Car{name='福特', color='Blue'}]}

MAP

测试类

package com.softtool;

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

public class MapPerson {

    private String name;
     private Map<String,Car> map;

    public String getName() {
        return name;
    }

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

    public Map<String, Car> getMap() {
        return map;
    }

    public void setMap(Map<String, Car> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "MapPerson{" +
                "name='" + name + '\'' +
                ", map=" + map +
                '}';
    }
}

配置xml

 <bean  class="com.softtool.MapPerson">

        <property name="name" value=""/>
        <property name="map">
            <map>
                <entry key="周润发" value-ref="car"/>
                <entry  key="刘德华">
                    <bean   class="com.softtool.Car">
                        <property name="name" value="福特"></property>
                        <property name="color" value="Blue"></property>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>

运行

        MapPerson mapPerson = (MapPerson) ac.getBean(MapPerson.class);
        System.out.println(mapPerson.toString());

//MapPerson{name='', map={周润发=Car{name='BMW', color='Red'}, 刘德华=Car{name='福特', color='Blue'}}}

Properties

package com.softtool;

import java.util.Properties;

public class ConfigProperties {
    private Properties properties;

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "ConfigProperties{" +
                "properties=" + properties +
                '}';
    }
}

配置


    <bean class="com.softtool.ConfigProperties">
        <property name="properties">
            <props>
                <prop key="user">root</prop>
                <prop key="password">21345</prop>
            </props>
        </property>
    </bean>

测试

        ConfigProperties properties = ac.getBean(ConfigProperties.class);
        System.out.println(properties.toString());
//ConfigProperties{properties={user=root, password=21345}}

##使用P标签 配置

需要引入p命名空间xmlns:p="http://www.springframework.org/schema/p"

    <bean id="pCar" class="com.softtool.Car" p:name="宝马" p:color="银色">
    </bean>

测试

        Object pCar = ac.getBean("pCar");
        System.out.println(pCar.toString());
        //Car{name='宝马', color='银色'}

自动装配

配置

    <bean id="car" class="com.softtool.Car">
        <property name="name" value="BMW"></property>
        <property name="color" value="Red"></property>
    </bean>

    
    <bean id="autoPerson" class="com.softtool.Person" autowire="byName">
        <property name="name" value="自动装配"></property>
    </bean>

测试

        Person person3 = (Person) ac.getBean("autoPerson");
        System.out.println(person3.toString());
//Person{name='自动装配', car=Car{name='BMW', color='Red'}, age=null, value=null, remark='null'}

转载于:https://my.oschina.net/u/4006362/blog/2414012