首页 > 编程语言 > 详细

spring IOC 装配一个bean

时间:2019-06-22 15:22:15      阅读:124      评论:0      收藏:0      [点我收藏+]

1.0属性注入

新建一个people类

package com.java.test3;

/**
 * @author nidegui
 * @create 2019-06-22 14:45
 */
public class People {
    private Integer id;
    private String name;
    private String age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", age=‘" + age + ‘\‘‘ +
                ‘}‘;
    }
}

  

装配在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 id="helloWorld" class="com.java.test.Helloworld"></bean>

  	<bean id="people" class="com.java.test3.People"></bean>

	<!--属性注入-->
	<bean id="people2" class="com.java.test3.People">
		<property name="name" value="nidegui"></property>
		<property name="id" value="1"></property>
		<property name="age" value="12"></property>
	</bean>
</beans>

  

测试:

package com.java.test3;

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

/**
 * @author nidegui
 * @create 2019-06-22 14:47
 */
public class Test {
    public static void main(String[] args) {
        /*属性注入*/
       ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        People people =(People) ac.getBean("people2");
        System.out.println(people);
    }
}

  

技术分享图片

 

spring IOC 装配一个bean

原文:https://www.cnblogs.com/nidegui/p/11068898.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!