Spring3系列1-HelloWord例子

一、      环境

spring-framework-3.2.4.RELEASE

jdk1.7.0_11

Maven3.0.5

eclipse-jee-juno-SR2-win32

二、      用Maven创建项目

mvn archetype:generate -DgroupId= com.lei.demo -DartifactId=Spring3-Example

       -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

然后转换成Eclipse项目:mvn eclipse:eclipse

或者直接在Eclipse中创建maven-archetype-quickstart项目。

三、      编辑pom.xml,加入spring3依赖

<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>com.lei.demo</groupId>
<artifactId>spring3-Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring3-Example</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring3配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
</dependencies>
</project>

四、      创建一个Spring Bean

一个简单的spring  bean如下

package com.lei.demo.helloworld;

public class HelloWorld {

    private String name;

    public void setName(String name) {
this.name = name;
} public void printHello() {
System.out.println("第一个Spring 3 : Hello ! " + name);
}
}

五、      创建Spring Bean的配置文件

创建文件SpringBeans.xml,配置bean如下。文件位于src/main/resources下

<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="helloBean" class="com.lei.demo.helloworld.HelloWorld">
<property name="name" value="leiOOlei" />
</bean> </beans>

六、      创建测试App


package com.lei.demo.helloworld;

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

七、      目录结构

八、      运行结果

Spring3系列1 -- HelloWord例子的更多相关文章

  1. Spring3系列11- Spring AOP——自动创建Proxy

    Spring3系列11- Spring AOP——自动创建Proxy 在<Spring3系列9- Spring AOP——Advice>和<Spring3系列10- Spring A ...

  2. Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法

    Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...

  3. Spring3系列9- Spring AOP——Advice

    Spring3系列9- Spring AOP——Advice Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统 ...

  4. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

  5. Spring3系列5-Bean的基本用法

    Spring3系列5-Bean的基本用法 本篇讲述了Bean的基本配置方法,以及Spring中怎样运用Bean. 主要内容如下: 一.      Spring中Bean的相互引用 二.      Sp ...

  6. Spring3系列3 -- JavaConfig

    Spring3系列3-JavaConfig-1 从Spring3开始,加入了JavaConfig特性,JavaConfig特性允许开发者不必在Spring的xml配置文件中定义bean,可以在Java ...

  7. Spring3系列13-Controller和@RequestMapping

    Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...

  8. Spring3系列12- Spring AOP AspectJ

    Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...

  9. Spring3系列8- Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

随机推荐

  1. js页面刷新、前进、后退

    1.使用按钮 <input type=button value=刷新 onclick="window.location.reload()"> <input typ ...

  2. JS模块间错误隔离

    问题背景: 页面中有多个功能模块,怎么在一个模块出了问题之后,保证其它模块的正常工作. 上面的差不多就是面试官的原话了,姑且称之为模块间错误隔离问题 第一反应是动态按需加载代码,用户操作发生后再加载对 ...

  3. Heroku空项目 Git本地Push代码错误 以及 Heroku Web启动错误

    在Eclipse下建了一个"Blank Heroku App", 然后将自己写好的JS Web练习代码直接复制放到了这个空项目下, 由于Eclipse下的git工具不太会用, 导致 ...

  4. Javascript 异步加载详解(转)

    本文总结一下浏览器在 javascript 的加载方式. 关键词:异步加载(async loading),延迟加载(lazy loading),延迟执行(lazy execution),async 属 ...

  5. 配置SharePoint 2013 Search 拓扑结构

    在单台服务器上安装了 SharePoint Server 2013,并且创建了具有默认搜索拓扑的 Search Service 应用程序.在默认搜索拓扑中,所有搜索组件都位于承载管理中心的服务器上.S ...

  6. [Python爬虫] 在Windows下安装PIP+Phantomjs+Selenium

    最近准备深入学习Python相关的爬虫知识了,如果说在使用Python爬取相对正规的网页使用"urllib2 + BeautifulSoup + 正则表达式"就能搞定的话:那么动态 ...

  7. paip.提升性能----jvm参数调整.txt

    paip.提升性能----jvm参数调整.txt 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...

  8. atitit.技术选型方法总结为什么java就是比.net有前途

    atitit.技术选型方法总结为什么java就是比.net有前途 #----按照不同的需要有不铜的法... 一般有开发效率,稳定性上的需要.. 作者 老哇的爪子 Attilax 艾龙,  EMAIL: ...

  9. javaweb回顾第三篇数据库访问

    前言:本篇主要针对数据库的操作,在这里不适用hibernate或者mybatis,用最原始的JDBC进行讲解,通过了解这些原理以后更容易理解和学习hibernate或mybatis. 1:jdbc的简 ...

  10. VC基于消息的异步套接字

    用WSAStartup,需要在StdAfx.h头文件中需要声明 #include    #pragma   comment(lib,"WS2_32.lib") 用AfxSocket ...