本来上网想下载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)
4.0.0 com.qiantu testmaven war 0.0.1-SNAPSHOT testmaven Maven Webapp http://maven.apache.org org.springframework spring-context 4.0.0.RELEASE junit junit 4.11 test testmaven
保存之后就会发现多了很多包

< http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+NKGiyLu689C0tPrC67LiytTSu8/Co7o8L3A+CjxwPkFwcGxpY2F0aW9uQ29uZmlnLmphdmGjqNXiuPbA4M/gtbHT2iBhcHBsaWNhdGlvbkNvbnRleHQueG1so6k8YnI+CjwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">package maven; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration//表明这是一个配置文件,相当于applicationContext.xml @ComponentScan//自动扫描 public class ApplicationConfig { //下面部分对于这个测试是不需要的。 //只是记录一下 @Configuration + @Bean 的使用 //相当于xml的
User.java
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
package maven;
import org.springframework.stereotype.Component;
@Component
public class UserDAO {
public User getUser() {
return new User("qiantujava");
}
}
UserService.java
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
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/