zoukankan      html  css  js  c++  java
  • Eclipse IDE下的Spring框架使用简单实例

    Eclipse IDE下的Spring框架使用简单实例


    1 准备
    Java jdk安装。

    Eclipse软件安装。
    根据系统安装32/64版本,选择Eclipse IDE for Java Developers 进行在线安装。

    Spring框架下载。 

    Commons_Logging包下载

    2 配置工作
    在项目栏右键,Build Path->Config Build Path 选择Librarys -> Add External Jars导入Spring 与Commons_Logging的jar包

    3 示例程序
    在src文件夹下创建com.manongjc包,在包中分别创建HelloWorld类与MainApp类,代码如下:

    //HelloWorld类
    package com.manongjc;

    public class HelloWorld {
       private String message;

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

       public void getMessage(){
          System.out.println("Your Message : " + message);
       }
    }

    //MainApp类
    package com.manongjc;

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

    public class MainApp {
       public static void main(String[] args) {
          ApplicationContext context =
                 new ClassPathXmlApplicationContext("Beans.xml");

          HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

          obj.getMessage();
       }
    }

    在src文件夹下创建beans.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-3.0.xsd">

    <bean id="helloWorld" class="com.manongjc.HelloWorld">
    <property name="message" value="Hello World!"/>
    </bean>

    </beans>

    选中MainApp,点击运行,可以在Console里看到输出的日志。

  • 相关阅读:
    spring MVC框架(入门篇)-1
    spring的三种注入方式
    mybitis实现增,删,改,查,模糊查询的两种方式:(2)
    [JavaEE] 20141228_Java类文章搜集
    [JavaEE] Apache Maven 入门篇(下)
    [JavaEE] Apache Maven 入门篇(上)
    [ASP.Net] MVC2,3,4,5的不同
    [JavaEE] Mybatis与Ibatis比较
    [ASP.Net] 转 > ASP.NET MVC 大牛之路
    [ASP.Net] 转 > ASP.NET MVC 小牛之路
  • 原文地址:https://www.cnblogs.com/mingzhang/p/7615593.html
Copyright © 2011-2022 走看看