快速使用 maven 构建一个 spring web 项目

原文:http://blog.csdn.net/qiantujava/article/details/18708121

本来上网想下载spring的,可以不提供下载。。只能用maven管理下载。。。

现在很多很多项目都是用maven来管理了,我们有必要学习一下maven,不深究,先入门。

Eclipse是自带有maven的,如果不熟的话,可以直接使用。


1、new 一个maven项目。(这里是web项目)


2、右击项目更目录,build path,configure build path... ,把打叉的删掉,也就是项目里不存在的删掉。然后返回,再自己添加进去。(根据约定俗成的目录来开发)



3、在pom.xml里面添加spring-context-xxx.jar。

(并把junit换成官网的最新版https://github.com/junit-team/junit/wiki/Download-and-Install

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.qiantu</groupId>  
  5.   <artifactId>testmaven</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>testmaven Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <dependencies>  
  11.       
  12.     <!-- 添加这个 -->  
  13.     <dependency>  
  14.         <groupId>org.springframework</groupId>  
  15.         <artifactId>spring-context</artifactId>  
  16.         <version>4.0.0.RELEASE</version>  
  17.     </dependency>  
  18.     
  19.     <!-- 换成最新版4.11 -->  
  20.     <dependency>  
  21.       <groupId>junit</groupId>  
  22.       <artifactId>junit</artifactId>  
  23.       <version>4.11</version>  
  24.       <scope>test</scope>  
  25.     </dependency>  
  26.       
  27.   </dependencies>  
  28.   <build>  
  29.     <finalName>testmaven</finalName>  
  30.   </build>  
  31. </project>  


保存之后就会发现多了很多包



4、然后写代码测试一下:

ApplicationConfig.java(这个类相当于 applicationContext.xml)

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package maven;  
  2.   
  3. import org.springframework.context.annotation.ComponentScan;  
  4. import org.springframework.context.annotation.Configuration;  
  5.   
  6. @Configuration//表明这是一个配置文件,相当于applicationContext.xml  
  7. @ComponentScan//自动扫描  
  8. public class ApplicationConfig {  
  9.   
  10.       
  11.     //下面部分对于这个测试是不需要的。  
  12.     //只是记录一下 @Configuration + @Bean 的使用  
  13.     //相当于xml的<bean id=userService class=xxx />  
  14.     /* 
  15.     @Bean 
  16.     public UserService userService() { 
  17.         return new UserService(); 
  18.     } 
  19.     */  
  20.       
  21. }  

User.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package maven;  
  2.   
  3. public class User {  
  4.     private String name;  
  5.   
  6.     public User(String name) {  
  7.         super();  
  8.         this.name = name;  
  9.     }  
  10.   
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.   
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18. }  

UserDAO.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package maven;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. @Component  
  6. public class UserDAO {  
  7.       
  8.     public User getUser() {  
  9.         return new User("qiantujava");  
  10.     }  
  11.       
  12. }  

UserService.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package maven;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component  
  7. public class UserService {  
  8.       
  9.     //用@Autowired注解的属性不需要写set/get方法  
  10.     @Autowired  
  11.     private UserDAO userDAO;  
  12.       
  13.     public void printUser() {  
  14.         User user = userDAO.getUser();  
  15.         System.out.println(user.getName());  
  16.     }  
  17.       
  18. }  

UserServiceTest.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package maven;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  6.   
  7. import junit.framework.TestCase;  
  8.   
  9. public class UserServiceTest extends TestCase {  
  10.     @Test  
  11.     public void testPrintUser() {  
  12.         ApplicationContext ctx =   
  13.                 new AnnotationConfigApplicationContext(ApplicationConfig.class);  
  14.           
  15.         UserService userService = ctx.getBean(UserService.class);  
  16.         userService.printUser();  
  17.           
  18.         //用  @Configuration + @Bean 的时候用这个获取bean  
  19. //      UserService userService = (UserService) ctx.getBean("userService");  
  20.           
  21.     }  
  22. }  



参考连接:http://projects.spring.io/spring-framework/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值