Spring配置与第一Spring HelloWorld
林炳文Evankaka原创作品。
转载请注明出处http://blog.csdn.net/evankaka
本文将主讲了Spring在Eclipse下的配置,并用Spring执行了第一个HelloWorld.
一、下载须要的文件
这里我们已经配置好Java的执行环境和装好Eclipse了。
下载Spring
下载地址:http://maven.springframework.org/release/org/springframework/spring/
下载commons-logging
下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi
将它们下载后解压到自己想放的位置,下载之前记得要看清楚是32位还是64位
二、配置Spring
1、新建一个project,就叫SpringHelloworld。
2、加入Spring3.x的包。网上有非常多不同的方法。这里我仅仅讲一种。
在Window->Preferences->Java->Build Path->User Libraries->New加入一个用户包的库,这里这么做的原因是Spring包比較多,我们这样做,配置一次后,以后每一个project要用直接加入该库即可了
命名为Spring3.2,点击OK
加入成功后
加入到project中来:
选择新建的project-》Properties->Java Build Path->Add library
在跳出的窗体中选择User Library
然后又会跳出一个窗体,这时就能够选择我们之前配置的用户库的包Spring3.2了,把沟打上。
加入成功
然后project中就能够看到加入进来的Spring3.2了
三、加入commons-logging
选择project-》Properties->Java Build Path->Add library
然后选择commons-logging所在的包就能够了
加入成功了
四、開始Spring编程
好了,上面的配置都弄好后,我们就能够開始第一个HelloWorld了
1.首先在当前包下新建一个HelloWorld.java
package com.test;
/**
* Spring第一个HelloWorld
* @author 林炳文(邮箱ling20081005@126.com 博客:http://blog.csdn.net/evankaka)
* @time 2015.4.1
*/
public class HelloWorld {
private String info; public String getInfo() {
return info;
} public void setInfo(String info) {
this.info = info;
} }
2、编写配置文件applicationContext.xml
在当前project下
这就是加入成功后的
然后把applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置须要被Spring管理的Bean(创建,创建后放在了Spring IOC容器里面)-->
<bean id="hello" class="com.test.HelloWorld">
<!-- 配置该Bean须要注入的属性(是通过属性set方法来注入的) -->
<property name="info" value="Happy New Year!"/>
</bean>
</beans>
3、反转控制開始
在Main.java中加入例如以下:
/**
* Spring第一个HelloWorld
* @author 林炳文(邮箱ling20081005@126.com 博客:http://blog.csdn.net/evankaka)
* @time 2015.4.1
*/
package com.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main { private String who = null; public static void main(String[] args) {
//获取Spring的ApplicationContext配置文件,注入IOC容器中
//(Map: key:String, bean标签的id属性值 ==>value:Object, bean标签class属性所指类的实例)
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hw1 = (HelloWorld)factory.getBean("hello");//map.get("hello")
System.out.println(hw1.getInfo());
System.out.println(hw1); }
}
然后选择project右键:
接下来就是输出结果啦:
林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
版权声明:这篇文章的博客林炳文Evankaka原创文章,博客,未经同意,不得转载。
Spring配置与第一Spring HelloWorld的更多相关文章
- spring配置数据源(交给spring容器完成)
##将DataSource的创建权交给spring容器去完成 1.导入spring依赖 <dependency> <groupId>org.springframework< ...
- Spring配置方式
Spring配置方式 第一阶段:xml配置 在spring 1.x时代,使用spring开发满眼都是xml配置的bean,随着项目的扩大, 我们需要把xml配置文件分放到不同的配置文件中,那时 ...
- Druid + spring 配置数据库连接池
1. Druid的简介 Druid是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBo ...
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...
- [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- spring boot实战(第一篇)第一个案例
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...
- 学习spring2--跟我一起学Spring 3(3)–使用Spring开发第一个HelloWorld应用
http://www.importnew.com/13246.html 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所有文章 资讯 ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
随机推荐
- Android在发送带有附件的邮件
准备好工作了-下载最新的版本号JMail https://java.net/projects/javamail/pages/Home#Download_JavaMail_1.5.2_Release h ...
- Mono+CentOS+Jexus
在.NET Core之前,实现.Net跨平台之Mono+CentOS+Jexus初体验准备工作 本篇文章采用Mono+CentOS+Jexus的方式实现部署.Net的Web应用程序(实战,上线项目). ...
- [Sqlite]-->Java采用jdbc联系Sqlite各种特定的工艺数据库的数据操作
引: 1, Sqlite在Windows.Linux 和 Mac OS X 上的安装过程 2.嵌入式数据库的安装.建库.建表.更新表结构以及数据导入导出等等具体过程记录 3,嵌 ...
- 第2周 页_SQL Server 中数据存储的基本单位
原文:第2周 页_SQL Server 中数据存储的基本单位 上周通过探讨SQL Server如何执行一个查询奠定了基础.我也在那里提到页是8kb的缓存.今天我们对页进行进一步集中探讨,从性能调优角度 ...
- PocketSphinx语音识别系统语言模型的训练和声学模型的改进
PocketSphinx语音识别系统语言模型的训练和声学模型的改进 zouxy09@qq.com http://blog.csdn.net/zouxy09 关于语音识别的基础知识和sphinx的知识, ...
- android 网络运营商的名字显示规则(锁定屏幕,下拉列表)
一:Background & 有关flow MTK Operator name display分为两种类型的手机: 1. Sim卡名称: 从基于引导SIM卡读取IMSI到Spn-conf.xm ...
- poj2386Lake Counting
题意是这种.给你一个N*M的矩形图.这个图由两个东西组成.'.'和'W', W代表这个地方有水. .代表这个地方没水. 假设一个有水的地方它的上下左右,左上,坐下.右上.右下也有水,那么 就看成它们连 ...
- android最新的工具DateHelper
最新的工具DateHelper 实用程序类,.的天数来获得一个给定的月份.过了几天去习惯或.周.一个月.日期等.. 代码例如以下: import android.annotation.Suppress ...
- JDK源代码学习系列07----Stack
JDK源代码学习系列07----Stack 1.Stack源代码很easy ...
- C#(SuperWebSocket)与websocket通信
原文:C#(SuperWebSocket)与websocket通信 客户端代码 点击可以查看一些关于websocket的介绍 <!DOCTYPE html> <html> &l ...