• Spring_配置 Bean(2)


    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
    class: bean 全类名,通过反射的方式在IOC 容器中创建Bean。所以要求Bean 中必须有无参数的构造器
    id: 标识容器中的bean. id 唯一
    -->
    <bean id="helloWord" class="com.hy.spring.beans.HelloWord">
    <property name="name" value="spring"></property>
    </bean>

    <!-- 通过构造方法来配置Bean的属性-->
    <bean id="car" class="com.hy.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="car1" class="com.hy.spring.beans.Car">
    <constructor-arg value="BaoMa" type="String"></constructor-arg>
    <constructor-arg value="China" type="String"></constructor-arg>
    <constructor-arg value="240" type="int"></constructor-arg>
    </bean>

    </beans>

    Car.java

    package com.hy.spring.beans;

    public class Car {
    private String company;
    private String brand;
    private double price;
    private int maxSpeed;

    public Car(String company, String brand, double price) {
    super();
    this.company = company;
    this.brand = brand;
    this.price = price;
    }

    public Car(String company, String brand, int maxSpeed) {
    super();
    this.company = company;
    this.brand = brand;
    this.maxSpeed = maxSpeed;
    }

    @Override
    public String toString() {
    return "Car [company=" + company + ", brand=" + brand + ", maxSpeed="
    + maxSpeed + ", price=" + price + "]";
    }
    }


    HelloWord.java

    package com.hy.spring.beans;

    public class HelloWord {
    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 HelloWord() {
    System.out.println("HelloWorld's Constructor");
    }
    }


    Main.java

    package com.hy.spring.beans;

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

    public class Main {

    public static void main(String[] args) {
    //1.创建 Spring 的 IOC容器的对象
    // ApplicationContext 代表IOC容器
    // ClassPathXmlApplicationContext : 是 ApplicationContext接口的实现类。从类路径下加载配置文件
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.从IOC容器中获取Bean实例
    //利用ID 定位到IOC 容器中的bean
    HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
    //利用类型返回IOC 容器中的bean,但要求IOC容器中必须只能有一个该类型的bean
    //HelloWord helloWord = (HelloWord) ctx.getBean(HelloWord.class);
    //3.调用hello方法
    helloWord.hello();

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

    }

    }

  • 相关阅读:
    第五天站立会议记录
    第四天站立会议
    第三天站立会议
    第二天站立会议
    冲刺会议第三天
    冲刺会议第二天
    团队建议总结
    第二次冲刺第一天
    项目总结(09)
    第七天站立会议
  • 原文地址:https://www.cnblogs.com/yang-hao/p/5792881.html
Copyright © 2020-2023  润新知