Spring代码实例系列-01:hellospring(通过xml文件以默认的方式进行setter注入)

超级通道:Spring代码实例系列-绪论
这是Spring框架中的hello world实例,简单的展示了以下技术:
1. 注入bean
2. spring的xml配置文件
3. setter注入

1.目录

project
    \---src
        \---main
            \---java
            |   \---pers
            |       \---hanchao
            |           \---helloworld
            |               \---App.java
            |               \---Hellospring.java
            \---resources
                \---spring-config
                    \---spring-helloworld.xml

2.App.java

package pers.hanchao.helloworld;

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

/**
 * <p>hellospring</p>
 * @author hanchao 2018/1/7 12:15
 **/
public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config/spring-helloworld.xml");
        HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
        helloSpring.printHello();
    }
}

3.spring-helloworld.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="helloSpring" class="pers.hanchao.helloworld.HelloSpring">
        <property name="name" value="hanchao"/>
    </bean>
</beans>

4.HelloSpring.java

package pers.hanchao.helloworld;

/**
 * <p>springhello:通过xml配置bean,注入值到bean</p>
 * @author hanchao 2018/1/7 12:18
 **/
public class HelloSpring {

    private String name;

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

    public void printHello(){
        System.out.println("hello spring:" + this.name);
    }
}

5.result

hello spring:hanchao
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值