Spring配置文件篇
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 13 14 <!-- db --> 15 16 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 17 destroy-method="close"> 18 <property name="driverClassName"> 19 <value>com.mysql.jdbc.Driver</value> 20 </property> 21 <property name="url"> 22 <value>jdbc:mysql://192.1.128.162:3306/ics</value> 23 </property> 24 <property name="username"> 25 <value>root</value> 26 </property> 27 <property name="password"> 28 <value>root</value> 29 </property> 30 </bean> 31 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 32 33 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 34 <property name="dataSource"> 35 <ref local="dataSource" /> 36 </property> 37 </bean> 38 39 <bean id="userDao" class="com.test.info.ics.dao.impl.UserDaoImpl"> 40 <!-- <property name="jdbcTemplate"> 41 <ref local="icsdb" /> 42 </property> 43 --> 44 </bean> 45 46 <bean id="userResources" class="com.test.info.ics.resources.UserResources" /> 47 48 <bean id="userService" class="com.test.info.ics.service.impl.UserServiceImpl" /> 49 50 <bean id="log4jInitialization" 51 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 52 <property name="targetClass" value="org.springframework.util.Log4jConfigurer" /> 53 <property name="targetMethod" value="initLogging" /> 54 <property name="arguments"> 55 <list> 56 <value>classpath:log4j.properties</value> 57 </list> 58 </property> 59 60 </bean> 61 </beans>
加载Spring配置文件篇
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 8 <!-- Provide Spring context --> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:ics-context.xml</param-value> 12 </context-param> 13 14 <!-- --> 15 <context-param> 16 <param-name>resteasy.resources</param-name> 17 <param-value>com.test.info.ics.resources.UserResources</param-value> 18 </context-param> 19 20 <!-- --> 21 <listener> 22 <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> 23 </listener> 24 25 <!-- Load Spring context's listener --> 26 <listener> 27 <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class> 28 </listener> 29 30 31 32 <servlet> 33 <servlet-name>rest</servlet-name> 34 <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> 35 <!-- 36 <init-param> 37 <param-name>javax.ws.rs.Application</param-name> 38 <param-value>com.test.info.ics.ServiceApplication</param-value> 39 </init-param> 40 --> 41 </servlet> 42 43 <servlet-mapping> 44 <servlet-name>rest</servlet-name> 45 <url-pattern>/*</url-pattern> 46 </servlet-mapping> 47 </web-app>
解释:
L9~L12:实现自动加载Spring的配置文件
L15~L18:定义Resource资源类
此时,Source中不需要实现javax.ws.rs.core.Application方法
同时,L36~L39处的设置就不需要了
如果这段设置不做的话,则需要实现javax.ws.rs.core.Application方法,如下:
1 package com.test.info.ics; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.ws.rs.core.Application; 7 8 import com.test.info.ics.resources.UserResources; 9 10 11 public class ServiceApplication extends Application { 12 HashSet<Object> singletons = new HashSet<Object>(); 13 private Set<Class<?>> empty = new HashSet<Class<?>>(); 14 15 public ServiceApplication() { 16 empty.add(UserResources.class); 17 } 18 19 @Override 20 public Set<Class<?>> getClasses() { 21 return empty; 22 } 23 24 @Override 25 public Set<Object> getSingletons() { 26 return singletons; 27 } 28 }
同时,需要设置L36~L39。
L21~L28:两个Listener是为了启动Spring