Spring(4)--- hello world实例
Spring hello world实例
本教程介绍如何在Spring4 中创建一个简单的 Hello World 例子。
在这篇文章中使用的技术或工具:
- Spring 4.1
- Eclipse 10
- 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实例相关推荐
- activiti自己定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义
注:(1)环境搭建:activiti自己定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自己定义流程之Spr ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Sprin ...
- Spring 4 MVC入门实例
Spring 4发布很长一段时间了,从 Spring 3 到 Spring 4 有巨大的改变.网上还有很多教程是基于 Spring 3.0 的,甚至 Spring 2.5,要想按照网上的教程" ...
- Struts2和Spring和Hibernate应用实例
Struts2.Spring和Hibernate应用实例 Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到广泛的应用.作为最成功的Web框架,Struts自然拥有众多的优点 ...
- Spring Boot 整合 FreeMarker 实例
前言 在之前的文章Spring Boot 整合 Thymeleaf中,我们学习了如何将模板 Thymeleaf 整合到 Spring Boot 中,那今天我们就来看看,另一个老牌的开源免费模板引擎 - ...
- 获取Spring容器中的实例
1. 介绍 在Spring环境中想要获取一个类的实例,如果直接new出来,只是创建了一个新的对象并不是Spring容器中的实例,因此如果类中使用了@Value ,@Autowired等注解是不起作用的 ...
- spring starter原理和实例
spring starter原理和实例 背景 spring starter实现原理 自定义starter 背景 springboot的一个优势就是starter,利用starter,可以避免一些的繁琐 ...
- Spring 如何初始化泛型类实例
在 Java 中对于泛型类型,比如这样简单的类定义 class Processor<T> {} 如果直接初始化时要指定具体类型的话,我们可以这么写 Processor<String& ...
- Spring事务传播特性实例解析
背景介绍 目前系统正在进行代码重构前期预研工作,目标采用spring控制事务以减少开发代码量,提高开发效率.同时避免开发人员编码控制事务所带来的链接没有释放,事务没有提交,出现异常事务没有回滚的Bug ...
- Redis整合Spring结合使用缓存实例(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...
最新文章
- 中文版开源!这或许是最经典的Python编程教材
- python3.8使用requests_python3.8.1 入门基础学习 之 【 requests 基础学习,python3爬虫必备基础】...
- 2.3.5 操作系统之信号量机制实现进程的互斥、同步与前驱关系
- gcc编译器的整个工作过程
- navicat 添加外键1215错误
- 127_Power PivotPower BI DAX计算订单商品在库时间(延伸订单仓储费用)
- mysql md5解密_Web渗透测试——Mysql数据库手工注入
- 如何使用 vSphere Certificate Manager 替换 SSL 证书 (2097936)
- Android CircleImageView圆形ImageView
- JSP使用教程(第四版)学习笔记
- php递归源码,PHP递归算法的实例程序
- 计算机病毒防治——计算机病毒种类
- java中类名指的是什么_JAVA 类名.class是什么意思?
- 9月20日云栖精选夜读:异构计算高性能计算分论坛——揭秘拿什么实现超算平民化、国际化?
- Java实现线性回归模型算法
- Android ToastUtil
- 宁夏-银川地区geojson数据
- gensim numpy spicy等词云图制作库的安装方法(着重后期)
- 预训练语言模型复现-2 whole word mask
- 4.13黄金多空博弈,白银TD最新操作