Spring_(3)通过xml配置bean+创建对象

本文详细介绍如何在Spring框架中使用XML配置文件定义Bean,包括通过setter注入和构造方法注入属性,以及如何从Spring的IOC容器中获取这些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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值