Spring入门示例

开发环境

  Spring 4.3.0+Myeclipse2015+JDK1.8

 

准备阶段:

  1、新建一Spring01项目,然后新建一个lib文件。将下面的添加到lib文件中

  2、将lib文件所有的包导入项目

 

开发步骤:

  1、新建一个Hello.java的类

复制代码
 1 package com.proc.bean;
 2 
 3 public class Hello {
 4 
 5     private String msg;
 6 
 7     public void setMsg(String msg) {
 8         this.msg = msg;
 9     }
10     public void say(){
11         System.out.println("Hello "+msg);
12     }
13 }
复制代码

  2、在src文件夹下面新建一个applicationContext.xml文件

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6         <!-- id:该实例的名称,class:该实例的类型 -->
 7         <bean id="helloWorld" class="com.proc.bean.Hello">
 8             <!-- 设置属性msg的值为world -->
 9             <property name="msg" value="world"></property>
10         </bean>
11 </beans>
复制代码

 

  3、代码测试

复制代码
 1 package com.proc.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.proc.bean.Hello;
 8 
 9 public class TestSpring {
10     
11     @Test
12     public void test1(){
13         ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
14         Hello hello=(Hello)context.getBean("helloWorld");
15         hello.say();
16     }
17 }
复制代码

  

  结果输出:

复制代码
六月 23, 2016 3:23:13 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1637f22: startup date [Thu Jun 23 15:23:13 CST 2016]; root of context hierarchy
六月 23, 2016 3:23:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Hello world
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Terry_dong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值