Spring hello world实例

本教程介绍如何在Spring4 中创建一个简单的 Hello World 例子。

在这篇文章中使用的技术或工具:

  1. Spring 4.1
  2. Eclipse 10
  3. JDK 1.8

项目文档结构:创建MAVEN 项目

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.javass.spring.chapter2</groupId>
  <artifactId>HelloSpring1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <dependencies>
 
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
             
    </dependencies>
</project>

Beans 文件

<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="helloBean" class="com.zyzx.core.HelloWorld">
        <property name="name" value="zyzx" />
    </bean>
  
</beans>

执行文件

package com.zyzx.core;

/**
 * Spring bean
 * 
 */
public class HelloWorld {
    private String name;

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

public void printHello() {
        System.out.println("Spring 4 : Hello ! " + name);
    }
}

package com.zyzx.core;

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

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "beans.xml"); 
        HelloWorld obj = (HelloWorld) context.getBean("helloBean");
        obj.printHello();
    }
}

输出结果:

Spring 4 : Hello ! zyzx

Spring(4)--- hello world实例相关推荐

  1. activiti自己定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义

    注:(1)环境搭建:activiti自己定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建         (2)创建流程模型:activiti自己定义流程之Spr ...

  2. activiti自定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建         (2)创建流程模型:activiti自定义流程之Sprin ...

  3. Spring 4 MVC入门实例

    Spring 4发布很长一段时间了,从 Spring 3 到 Spring 4 有巨大的改变.网上还有很多教程是基于 Spring 3.0 的,甚至 Spring 2.5,要想按照网上的教程" ...

  4. Struts2和Spring和Hibernate应用实例

    Struts2.Spring和Hibernate应用实例 Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到广泛的应用.作为最成功的Web框架,Struts自然拥有众多的优点 ...

  5. Spring Boot 整合 FreeMarker 实例

    前言 在之前的文章Spring Boot 整合 Thymeleaf中,我们学习了如何将模板 Thymeleaf 整合到 Spring Boot 中,那今天我们就来看看,另一个老牌的开源免费模板引擎 - ...

  6. 获取Spring容器中的实例

    1. 介绍 在Spring环境中想要获取一个类的实例,如果直接new出来,只是创建了一个新的对象并不是Spring容器中的实例,因此如果类中使用了@Value ,@Autowired等注解是不起作用的 ...

  7. spring starter原理和实例

    spring starter原理和实例 背景 spring starter实现原理 自定义starter 背景 springboot的一个优势就是starter,利用starter,可以避免一些的繁琐 ...

  8. Spring 如何初始化泛型类实例

    在 Java 中对于泛型类型,比如这样简单的类定义 class Processor<T> {} 如果直接初始化时要指定具体类型的话,我们可以这么写 Processor<String& ...

  9. Spring事务传播特性实例解析

    背景介绍 目前系统正在进行代码重构前期预研工作,目标采用spring控制事务以减少开发代码量,提高开发效率.同时避免开发人员编码控制事务所带来的链接没有释放,事务没有提交,出现异常事务没有回滚的Bug ...

  10. Redis整合Spring结合使用缓存实例(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...

最新文章

  1. 中文版开源!这或许是最经典的Python编程教材
  2. python3.8使用requests_python3.8.1 入门基础学习 之 【 requests 基础学习,python3爬虫必备基础】...
  3. 2.3.5 操作系统之信号量机制实现进程的互斥、同步与前驱关系
  4. gcc编译器的整个工作过程
  5. navicat 添加外键1215错误
  6. 127_Power PivotPower BI DAX计算订单商品在库时间(延伸订单仓储费用)
  7. mysql md5解密_Web渗透测试——Mysql数据库手工注入
  8. 如何使用 vSphere Certificate Manager 替换 SSL 证书 (2097936)
  9. Android CircleImageView圆形ImageView
  10. JSP使用教程(第四版)学习笔记
  11. php递归源码,PHP递归算法的实例程序
  12. 计算机病毒防治——计算机病毒种类
  13. java中类名指的是什么_JAVA 类名.class是什么意思?
  14. 9月20日云栖精选夜读:异构计算高性能计算分论坛——揭秘拿什么实现超算平民化、国际化?
  15. Java实现线性回归模型算法
  16. Android ToastUtil
  17. 宁夏-银川地区geojson数据
  18. gensim numpy spicy等词云图制作库的安装方法(着重后期)
  19. 预训练语言模型复现-2 whole word mask
  20. 4.13黄金多空博弈,白银TD最新操作

热门文章

  1. mysql嵌套loop循环_mysql游标嵌套循环
  2. 学计算机信息管理专业的感谢,计算机信息管理专业工作求职信
  3. ionic 配置java_Ionic 项目配置环境
  4. 【LeetCode】剑指 Offer 17. 打印从1到最大的n位数
  5. 力扣892.三维形体的表面积
  6. 使用Android手机进行开发的尝试
  7. 从零开始搭建vue移动端项目到上线的步骤
  8. 【Hadoop 分布式部署 十 一: NameNode HA 自动故障转移】
  9. python编写排列组合,密码生产功能
  10. LF 第一模块 考试总结