导入;spring-web-3.2.0.RELEASE.jar
在web.xml中配置:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
修改程序的代码:
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
1.程序中有Junit环境.
2.导入一个jar包.spring与junit整合jar包.
3.测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest {
@Autowired
private UserService userService;
@Test
public void demo1(){
userService.sayHello();
}
}
(1)到(6)总结
配置Bean其他的属性:
DI注入属性:
对象属性:
@Component 描述Spring框架中Bean
@Repository 用于对DAO实现类进行标注
@Service 用于对Service实现类进行标注
@Controller 用于对Controller实现类进行标注
DI属性注入
Spring整合Web项目:
Spring整合Junit测试:
原文地址:http://blog.51cto.com/4534309/2107879