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

第一个Spring 程序

时间:2017-04-23 15:49:47      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:logging   oca   translate   location   搭建   apache   print   out   pat   

一 搭建好开发环境 JDK Eclipse 等

二 下载jar包 

https://commons.apache.org/logging/

https://repo.spring.io/release/org/springframework/spring

 

三 添加依赖jar包

把下载下来的jar包 添加到 我们新建的java项目 属性 JavaBuildPath的Libraries中

四 新建bean类 MyClass  并为属性 添加set 方法(以便容器 注入 也可以用 构造器 方式)

package com.exayong;

public class MyClass {
  String message;

  public void getMessage() {
    System.out.println("the message is :" + message);

  }

    public void setMessage(String str) {
  this.message = str;
  }

}

五 spring 配置文件 Beans.xml 放在src 目录下

<?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-3.0.xsd">

   <bean id = "MyClass" class = "com.exayong.MyClass">
    <!-- 下面为set方法注入-->
<property name = "message" value = "Hello World!"/>
  </bean>
</beans>

六 测试

main方法中 用ClassPathXmlApplicationContext 获取bean(项目的classpath 设置包含了src文件夹 所以配置文件 放在src下 才能找到)

public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  MyClass mc = (MyClass) context.getBean("myClass");
  mc.getMessage();
}

 

第一个Spring 程序

标签:logging   oca   translate   location   搭建   apache   print   out   pat   

原文地址:http://www.cnblogs.com/exayong/p/6752509.html

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