码迷,mamicode.com
首页 > 编程语言 > 详细

spring之在配置Bean时如何关联不同的Bean

时间:2020-01-04 18:43:54      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:配置   创建   XML   gets   vat   main   encoding   sys   cat   

Car.java

package com.gong.spring.beans;

public class Car {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Car [name=" + name + "]";
    }
    
}

Student.java

package com.gong.spring.beans;

public class Student {
    private String name;
    private int age;
    private double score;
    private Car car;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", score=" + score + ", car=" + car + "]";
    }
    
    
}

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="car" class="com.gong.spring.beans.Car">
        <property name="name" value="baoma"></property>
    </bean>
    
    <bean id="student" class="com.gong.spring.beans.Student">
        <property name="name" value="tom"></property>
        <property name="age" value="12"></property>
        <property name="score" value="98.00"></property>            
        <property name="car" ref="car"></property>
    </bean>
    
</beans>

Main.java

package com.gong.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 ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.从容器中获取Bean实例
        Student student = (Student) ctx.getBean("student"); 
        System.out.println(student.toString());
    }
}

输出:

技术图片

当然,也可以这么写:

        <property name="car"> 
            <ref bean="car"/>
        </property>

我们也可以在bean的内部配置相应的Bean,这个Bean就是一个内部bean,不能被外部使用。

        <property name="car">
            <bean class="com.gong.spring.beans.Car">
                <property name="name">
                    <value>baoma</value>
                </property>
            </bean>
        </property>

spring之在配置Bean时如何关联不同的Bean

标签:配置   创建   XML   gets   vat   main   encoding   sys   cat   

原文地址:https://www.cnblogs.com/xiximayou/p/12149867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!