1· web项目必须的web.xml 文件
首先我们在web.xml文件配置spring的配置文件的位置
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
</context-param>
以及配置spring的监听器:
<!-- spring监听器:启动web.xml时启动spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2·然后spring.xml文件也是不可少的
基于注解行的配置所以spring.xml的配置也比较简单
<!-- 引入属性文件 -->
<!--因为我是和hiberbate整合的所以在config.properties配置的数据库的username,password等等-->
<context:property-placeholder location="classpath:config.properties" />
<!-- 自动扫描dao和service包(自动注入)自动扫描dao,service以及其下面的子包这样就可以使用@service,@comment,@Resp..... -->
<context:component-scan base-package="sy.dao,sy.service" />
3·然后再进行测试下:
// ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] { "classpath:spring.xml" });
// UserServiceI service = (UserServiceI)ac.getBean("userService");
// service.test();