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

spring setter注入

时间:2017-08-17 20:03:19      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:setname   alt   www.   实体   bsp   rgs   ret   ext   override   

第一步: 编写实体类

Dog.java

package com.xuzhiwen.spring7;

public class Dog {
    private String name;
    private String hobby;
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

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

第二步:编写配置文件

applicationContext6.xml

<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-3.0.xsd">
    
    <bean id="dog" class="com.xuzhiwen.spring7.Dog">
        <property name="name" value="dahuang" />
        <property name="hobby" value="eat food" />
    </bean>
</beans>

第三步:编写测试类

package com.xuzhiwen.spring7;

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

public class Test {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext6.xml");
        Dog dog = (Dog) app.getBean("dog");
        System.out.println(dog);
    }
}

第四步:运行结果如下

技术分享

 

spring setter注入

标签:setname   alt   www.   实体   bsp   rgs   ret   ext   override   

原文地址:http://www.cnblogs.com/beibidewomen/p/7383503.html

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