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();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42036647/article/details/84069689