快速使用 maven 构建一个 spring web 项目
2015-02-06 22:52
1096 查看
原文:/article/2050441.html
本来上网想下载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
plaincopy
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qiantu</groupId>
<artifactId>testmaven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testmaven Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 添加这个 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 换成最新版4.11 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>testmaven</finalName>
</build>
</project>
保存之后就会发现多了很多包
4、然后写代码测试一下:
ApplicationConfig.java(这个类相当于 applicationContext.xml)
[java] view
plaincopy
package maven;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration//表明这是一个配置文件,相当于applicationContext.xml
@ComponentScan//自动扫描
public class ApplicationConfig {
//下面部分对于这个测试是不需要的。
//只是记录一下 @Configuration + @Bean 的使用
//相当于xml的<bean id=userService class=xxx />
/*
@Bean
public UserService userService() {
return new UserService();
}
*/
}
User.java
[java] view
plaincopy
package maven;
public class User {
private String name;
public User(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserDAO.java
[java] view
plaincopy
package maven;
import org.springframework.stereotype.Component;
@Component
public class UserDAO {
public User getUser() {
return new User("qiantujava");
}
}
UserService.java
[java] view
plaincopy
package maven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserService {
//用@Autowired注解的属性不需要写set/get方法
@Autowired
private UserDAO userDAO;
public void printUser() {
User user = userDAO.getUser();
System.out.println(user.getName());
}
}
UserServiceTest.java
[java] view
plaincopy
package maven;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import junit.framework.TestCase;
public class UserServiceTest extends TestCase {
@Test
public void testPrintUser() {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(ApplicationConfig.class);
UserService userService = ctx.getBean(UserService.class);
userService.printUser();
//用 @Configuration + @Bean 的时候用这个获取bean
// UserService userService = (UserService) ctx.getBean("userService");
}
}
参考连接:http://projects.spring.io/spring-framework/
本来上网想下载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
plaincopy
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qiantu</groupId>
<artifactId>testmaven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testmaven Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 添加这个 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 换成最新版4.11 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>testmaven</finalName>
</build>
</project>
保存之后就会发现多了很多包
4、然后写代码测试一下:
ApplicationConfig.java(这个类相当于 applicationContext.xml)
[java] view
plaincopy
package maven;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration//表明这是一个配置文件,相当于applicationContext.xml
@ComponentScan//自动扫描
public class ApplicationConfig {
//下面部分对于这个测试是不需要的。
//只是记录一下 @Configuration + @Bean 的使用
//相当于xml的<bean id=userService class=xxx />
/*
@Bean
public UserService userService() {
return new UserService();
}
*/
}
User.java
[java] view
plaincopy
package maven;
public class User {
private String name;
public User(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserDAO.java
[java] view
plaincopy
package maven;
import org.springframework.stereotype.Component;
@Component
public class UserDAO {
public User getUser() {
return new User("qiantujava");
}
}
UserService.java
[java] view
plaincopy
package maven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserService {
//用@Autowired注解的属性不需要写set/get方法
@Autowired
private UserDAO userDAO;
public void printUser() {
User user = userDAO.getUser();
System.out.println(user.getName());
}
}
UserServiceTest.java
[java] view
plaincopy
package maven;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import junit.framework.TestCase;
public class UserServiceTest extends TestCase {
@Test
public void testPrintUser() {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(ApplicationConfig.class);
UserService userService = ctx.getBean(UserService.class);
userService.printUser();
//用 @Configuration + @Bean 的时候用这个获取bean
// UserService userService = (UserService) ctx.getBean("userService");
}
}
参考连接:http://projects.spring.io/spring-framework/
相关文章推荐
- 快速使用 maven 构建一个 spring web 项目
- 使用Maven构建javaWeb项目时,启动tomcat出错:严重: Error configuring application listener of class org.springframewo
- eclipse 使用maven 构建springbooot+mysql +freemarker 简单web 项目
- 使用Spring Boot +JPA+mybatis快速启动一个web项目
- 使用Eclipse的maven构建一个web项目
- 使用Eclipse的maven构建一个web项目
- 快速创建一个基于Gradle构建的SpringBoot Web项目(SpringBoot-01)
- eclipse下使用Maven构建一个web项目
- 快速构建Spring Boot 1.5.8 maven Web 项目
- 使用Spring Web MVC和Maven构建web项目
- 使用maven命令终端构建一个web项目及发布该项目
- 使用sts构建Maven Spring webmvc项目
- 使用Maven构建Web项目+Spring+Mybatis配置
- Maven快速使用教程(二) spring boot 项目构建
- 使用Eclipse的maven构建一个web项目
- 使用Maven构建javaWeb项目时,启动tomcat出错:严重: Error configuring application listener of class org.springframewo
- 使用maven构建一个Spring Boot项目
- 【springBoot】之快速构建一个web项目
- 使用Maven构建一个JavaWeb项目
- 使用Maven构建javaWeb项目时,启动tomcat出错:严重: Error configuring application listener of class org.springframewo