转自:http://www.cnblogs.com/cypergreen/archive/2012/08/11/2633497.html
m2eclipse+spring3 hello world
如果你仅仅想使用maven管理你的jar包依赖,而又不习惯他那深深的目录结构,请往下看。
(1)安装m2eclipse
打开你的Eclipse JEE(3.7+),
在浏览器中打开http://eclipse.org/m2e/download/
拖动这个图标到你的Eclipse中(不要拖到编辑器中就行),插件就装好了!
我们新建一个 dynamic web工程
工程名test,直接点Finish
现在的目录结构是这样的
在工程名上右键 》Configure>Convert to maven project
弹出POM对话框,直接点Finish
打开生成的pom.xml文件,切换到pom.xml标签卡。
Add Spring 3.0 dependency
打开spring网站,当前的最新版为3.1.0.RELEASE
修改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>test</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> <properties> <spring.version>3.1.0.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>
我们做的只是加了一个properties,一个dependencies,保存。这时m2eclipse插件会自动将依赖的所有jar包下载到本地,并添加到工程的classpath
如果保存pom.xml后没有出现上图中红框部分的内容,可以尝试在工程上右键> Maven > Update Project...
根据这里的例子:http://www.mkyong.com/spring3/spring-3-hello-world-example/
我们添加一个spring bean: HelloWorld.java,一个spring配置文件SpringBeans.xml,一个测试类App.java
HelloWorld.java
package com.mkyong.core; /** * Spring bean * */ public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void printHello() { System.out.println("Spring 3 : Hello ! " + name); } }
SpringBeans.xml
<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.mkyong.core.HelloWorld"> <property name="name" value="Mkyong" /> </bean> </beans>
App.java
package com.mkyong.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( "SpringBeans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloBean"); obj.printHello(); } }
看下图,在App.java中右键Run As > Java Application
打印出Spring 3: Hello!,我们完成了第一步。
接下来在WebContent上右键new > Other> JSP file,文件名为index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> Hello, 松柏长青! </body> </html>
在文件中右键Run As > Run on Server
点下一步,保持默认设置直到Finish
BINGO!
全文完。
下载本示例中的代码:https://bitbucket.org/cyper/test/get/bcb5d2cbb8f5.zip