【Spring】配置Bean

版权声明:本文内容来源于网络,如有侵权请联系删除 https://blog.csdn.net/ZyhMemory/article/details/88556517

HelloWorld.java

package com.test.sping.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's Constructor...");
    }
}

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" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!--
    	配置 bean
    	class: bean 的全类名,通过反射的方式在 IOC 容器中创建 Bean , 所以要求 Bean 中必须有无参数的构造器
    	id: 标识容器中的 bean. id 唯一。
    -->
    <bean id="helloWorld" class="com.test.sping.beans.HelloWorld">
        <property name="name" value="Sping"></property>
    </bean>
</beans>

猜你喜欢

转载自blog.csdn.net/ZyhMemory/article/details/88556517