1. 程式人生 > >Spring_(3)通過xml配置bean+建立物件

Spring_(3)通過xml配置bean+建立物件

先來看整體專案的結構

在這裡插入圖片描述

HelloWorld.java

package com.spring.beans;

public class HelloWorld {
    private String name;

    public void setName(String name){
        System.out.println("setName"+name);
        this.name = name;
    }

    public void hello(){
        System.out.println("hello:"+ name)
; } public HelloWorld(){ System.out.println("HelloWorld Constractor..."); } }

Car.java

package com.spring.beans;

public class Car {

    private String brand;
    private String corp;
    private double price;
    private int maxSpead;

    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 maxSpead){ super(); this.brand = brand; this.corp = corp; this.maxSpead = maxSpead; } @Override
public String toString() { return "Car{" + "brand='" + brand + '\'' + ", corp='" + corp + '\'' + ", price=" + price + ", maxSpead=" + maxSpead + '}'; } }

通過xml配置bean檔案

在src下建立 applicationContext.xml (“很通常的命名方式”) 作用: 在這裡可以配置bean

<?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
        class:bean 的全類名, 通過反射的方式 在IOC容器中建立Bean, 所以要求Bean中必須有無引數的構造器
        id: 標識容器中的 bean, id 唯一,
    -->
    <bean id ="helloworld" class="com.spring.beans.HelloWorld">
        <property name="name" value="Spring"></property><!--這個是通過setter方法進行最常用的屬性注入-->
    </bean>

    <!--通過構造方法來配置bean的屬性-->
    <bean id="car" class="com.spring.beans.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="ShangHai" index="1"></constructor-arg>
        <constructor-arg value="300000" index="2"></constructor-arg>
    </bean>

    <!--使用構造器注入屬性值可以指定引數的位置和型別!以區分過載的構造器!-->
    <bean id="car2" class="com.spring.beans.Car">
        <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
        <constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
        <constructor-arg value="240" type="int"></constructor-arg>
    </bean>

    <!--<bean id ="helloworld2" class="com.spring.beans.HelloWorld">
        <property name="name" value="Spring"></property>
    </bean>-->
</beans>

通過XML配置檔案建立物件

Main.java

package com.spring.beans;

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

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

       /* //建立HelloWorld的一個物件
        HelloWorld helloWorld = new HelloWorld();
        //為name 屬性賦值
        helloWorld.setName("atguigu");*/


        //1.建立Spring 的IOC容器物件
        //ApplicationContext 代表IOC容器,實際上是一個藉口
        //ClassPathXmlApplicationContext: 是ApplicationContext 介面的實現類,該實現類從類路勁下來載入配置檔案
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.從IOC容器中獲取Bean 例項
        //利用id定位到IOC容器中的bean
        HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld");
        //利用型別返回IOC容器中的Bean,但要求IOC容器中必須只能由一個該型別的Bean
        //HelloWorld helloWorld = ctx.getBean(HelloWorld.class);//要求這個類在配置檔案中是唯一的
        System.out.println(helloWorld);

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

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


        //3.呼叫hello方法
        //helloWorld.hello();
    }
}