您的位置:首页 > 编程语言 > Java开发

spring hibernate cxf/xfire/rest 框架搭建

2013-03-26 22:48 148 查看
一、建立工程所需JAR包
1.spring+hibernate所需jar包








2.cxf的所需jar包:

a)必须jar包
cxf.jar  commons-logging.jar geronimo-activation.jar (Or the Sun equivalent)
geronimo-annotation.jar (Or the Sun equivalent) geronimo-javamail.jar (Or the Sun equivalent) neethi.jar jaxb-api.jar jaxb-impl.jar stax-api.jar XmlSchema.jar
wstx-asl.jar xml-resolver.jar


对于 Java2WSDL 和 WSDL2Java,需要再增加如下 jar 包:

jaxb-xjc.jar veliocity.jar velocity-dep.jar

为了支持 JAX-WS ,需要再增加如下 jar 包:

jaxws-api.jar saaj-api.jar saaj-impl.jar asm.jar (可选的,但是可以提升包装类型的性能)

为了支持 XML 配置,需要再增加如下 jar 包:

aopalliance.jar spring-beans.jar spring-context.jar spring-core.jar spring.web.jar

为了独立的 HTTP 服务支持,需要再增加如下 jar 包:

geronimo-servlet.jar jetty.jar jetty-sslengine.jar jetty-util.jar sl4j.jar & sl4j-jdk14.jar (可选的,但是可以提升日志 logging)

为了支持 Aegis ,需要再增加如下 jar 包:

jaxen.jar jdom.jar stax-utils.jar

为了支持 WS-Security ,需要再增加如下 jar 包:

bcprov-jdk14.jar wss4j.jar xalan.jar xmlsec.jar

为了支持 HTTP Binding ,需要再增加如下 jar 包:

jra.jar jettison.jar (仅为 JSON 服务所需的)licenses(目录)

3.xfire所需jar包



4.rest所需jar包




二、xml配置信息

1.web.xml内容
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"

	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
	version="2.4">

	<display-name>inforcnCircleService</display-name>

	<distributable />

	<context-param>

		<param-name>contextConfigLocation</param-name>

		<param-value>

            classpath:/spring/applicationContext*.xml

        </param-value>

	</context-param>

	<filter>

		<filter-name>encodingFilter</filter-name>

		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

		<init-param>

			<param-name>encoding</param-name>

			<param-value>UTF-8</param-value>

		</init-param>

		<init-param>

			<param-name>forceEncoding</param-name>

			<param-value>true</param-value>

		</init-param>

	</filter>

	<filter-mapping>

		<filter-name>encodingFilter</filter-name>

		<url-pattern>/*</url-pattern>

	</filter-mapping>

<!-- 	<filter> -->

<!-- 		<filter-name>securityFilter</filter-name> -->

<!-- 		<filter-class>org.springframework.security.util.FilterToBeanProxy</filter-class> -->

<!-- 		<init-param> -->

<!-- 			<param-name>targetClass</param-name> -->

<!-- 			<param-value>org.springframework.security.util.FilterChainProxy</param-value> -->

<!-- 		</init-param> -->

<!-- 	</filter> -->

<!-- 	<filter-mapping> -->

<!-- 		<filter-name>securityFilter</filter-name> -->

<!-- 		<url-pattern>/*</url-pattern> -->

<!-- 	</filter-mapping> -->

	<listener>

               <!--  自动装配ApplicationContext的配置信息 -->

		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

	</listener>

	<listener>

                <!-- org.springframework.web.util.IntrospectorCleanupListener监听器主要负责处理由JavaBean Introspector使用而引起的缓冲泄露-->

		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

	</listener>

	<listener>

<!-- ContextLoaderListener实现ServletContextListener监听器接口,而ServletContextListener只负责监听Web容器的启动和关闭的事件。RequestContextFilter实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,Web服务器接收的每次请求都会通知该监听器。通过配置RequestContextFilter,Spring容器与Web容器结合的更加密切。 -->

		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

	</listener>

<!-- 	<listener> -->

<!-- 		<listener-class>org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class> -->

<!-- 	</listener> -->

<!-- 	<listener> -->

<!-- 		<listener-class>cn.walle.core.support.spring.CustomRequestContextListener</listener-class> -->

<!-- 	</listener> -->

<!-- xfire监听器配置 -->

	<servlet>

		<servlet-name>xfire</servlet-name>

		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>

	</servlet>

	<servlet-mapping>

		<servlet-name>xfire</servlet-name>

		<url-pattern>/xfireservices/*</url-pattern>

	</servlet-mapping>

<!-- cxf监听器配置 -->

        <servlet>

		<servlet-name>CXFServlet</servlet-name>

		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

	</servlet>

	<servlet-mapping>

		<servlet-name>CXFServlet</servlet-name>

		<url-pattern>/cxfservices/*</url-pattern>

	</servlet-mapping>

<!-- rest监听器配置 -->

        <servlet>

		<display-name>JAX-RS REST Servlet</display-name>

		<servlet-name>JAX-RS REST Servlet</servlet-name>

		<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>

		<init-param>

			<param-name>com.sun.jersey.config.property.packages</param-name>

			<param-value>cn.inforcn</param-value>

		</init-param>

		<load-on-startup>1</load-on-startup>

	</servlet>

	<servlet-mapping>

		<servlet-name>JAX-RS REST Servlet</servlet-name>

		<url-pattern>/ws/*</url-pattern>

	</servlet-mapping>

	<welcome-file-list>

		<welcome-file>index.jsp</welcome-file>

	</welcome-file-list>

</web-app>


2.spring xml配置内容

a) application-xfire.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xsi:schemaLocation="http://www.springframework.org/schema/beans

					 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

    <!--<bean id="jaxbTypeMappingRegistry" class="org.codehaus.xfire.jaxb2.JaxbTypeRegistry"

          init-method="createDefaultMappings"/>-->

    <bean id="xmlbeansTypeRegistry" class="org.codehaus.xfire.xmlbeans.XmlBeansTypeRegistry"/>

    

    <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/>

    <bean id="handlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">

        <!--<property name="typeMappingRegistry" ref="jaxbTypeMappingRegistry"/>-->

        <property name="typeMappingRegistry" ref="xmlbeansTypeRegistry"/>

        <property name="xfire" ref="xfire"/>

        <property name="webAnnotations" ref="webAnnotations"/>

    </bean>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property name="urlMap">

            <map>

                <entry key="/">

                    <ref bean="handlerMapping"/>

                </entry>

            </map>

        </property>

    </bean>

</beans>

b) application-cxf.xml

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:jaxws="http://cxf.apache.org/jaxws"

       xmlns:jaxrs="http://cxf.apache.org/jaxrs"

       xmlns:cxf="http://cxf.apache.org/core"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd

            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd

            http://www.springframework.org/schema/context   

		 http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>

    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>

    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>

    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    

	<context:component-scan base-package="cn"

							name-generator="cn.walle.core.support.spring.CustomBeanNameGenerator" />

<!--     <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/> -->

<!--    <jaxws:endpoint id="userService" implementor="#userManager" address="/UserService"/>-->

         <!-- webservice服务器端的配置   

                implementor后如果是类名的话 cn.walle.circleManageService,则java注解 @Autowire @Resource等都不会将将类注入

                只能使用#circleManageService,同时需要在定义的类上加上 @Component(value="circleManageService")才能用注解方式将类注入

         -->

        <jaxws:endpoint id="CircleManageService" implementor="#circleManageService" address="/CircleManageService" />

<!--  客户端bean配置(如果只做服务的话,不需配置客户端)

<jaxrs:server address="/rest">

        <jaxrs:features>

            <cxf:logging/>

        </jaxrs:features>

        <jaxrs:serviceBeans>

            <ref bean="userManager"/>

        </jaxrs:serviceBeans>

        <jaxrs:providers>

            <ref bean="jsonProvider"/>

        </jaxrs:providers>

        <jaxrs:extensionMappings>

            <entry key="json" value="application/json"/>

            <entry key="xml" value="application/xml"/>

            <entry key="feed" value="application/atom+xml"/>

        </jaxrs:extensionMappings>

    </jaxrs:server>

-->

</beans>


c)application-rest.xml

<?xml version="1.0"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName" default-lazy-init="true">

	<bean id="circleManager" class="cn.inforcn.circle.web.CircleManagerRest"></bean>

</beans>


3.webservice类的编写
a) xfire
接口类
@WebService

public interface CircleManageService {

	public String circleManage(String param);

}

实现类

@Service    @WebService(serviceName="CircleManageService",endpointInterface="cn.walle.circle.webservice.services.CircleManageService")

public class CircleManageServiceImpl implements CircleManageService {

	@Autowired

	private SystemManage systemManage;

	

	public String circleManage(String param) {

//xfire中得到访问的ip地址

		HttpServletRequest request = XFireServletController.getRequest(); 

		String ip = request.getRemoteAddr();

                ....................

       }

}

b)cxf

接口类

@WebService

public interface CircleManageService {

	public String circleManage(String param);

}


实现类

@Component(value="circleManageService")

@Service

@WebService(serviceName="CircleManageService",endpointInterface="cn.walle.circle.webservice.services.CircleManageService")

public class CircleManageServiceImpl implements CircleManageService {

	

	
private SystemManage systemManage;

	
//@Context

	
//private WebServiceContext context;

	
public String circleManage(String param) {

                //cxf中获取ip地址

		
Message message = PhaseInterceptorChain.getCurrentMessage();

		
HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);

		
String ip = request.getRemoteAddr();

       }

}




c)rest

@Path("CircleManager")

    public class CircleManagerRest {

        @POST

	@Path("upload")

	public String upload(@Context HttpServletRequest request){

        ..............

        }

    }




4.spring hibernate配置
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xmlns:aop="http://www.springframework.org/schema/aop"

	xmlns:context="http://www.springframework.org/schema/context"

	xmlns:jee="http://www.springframework.org/schema/jee"

	xmlns:tx="http://www.springframework.org/schema/tx"

	xsi:schemaLocation="http://www.springframework.org/schema/beans

					 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
					 http://www.springframework.org/schema/aop 
					 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
					 http://www.springframework.org/schema/context 
					 http://www.springframework.org/schema/context/spring-context-2.5.xsd 
					 http://www.springframework.org/schema/jee 
					 http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
					 http://www.springframework.org/schema/tx 
					 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- JNDI DataSource for J2EE environments -->

	<!--

	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/walle" />

	-->

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

		<property name="driverClassName" value="${jdbc.driverClassName}" />

		<property name="url" value="${jdbc.url}" />

		<property name="username" value="${jdbc.username}" />

		<property name="password" value="${jdbc.password}" />

		<property name="maxActive" value="2000" />

		<property name="maxIdle" value="1000"></property>

		<property name="maxWait" value="25000" />

		<property name="poolPreparedStatements" value="false" />

		<property name="defaultAutoCommit" value="true" />

	</bean>

	<!-- 

	<bean id="multiDataSource" class="cn.walle.core.support.spring.MultiDataSource">

		<property name="targetDataSources">

			<map>

				<entry key="dataSource1" value-ref="dataSource1" />

				<entry key="dataSource2" value-ref="dataSource2" />

			</map>

		</property>

		<property name="defaultTargetDataSource" ref="dataSource" />

	</bean>

	 -->

	<!-- Hibernate SessionFactory -->

	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

		<property name="dataSource" ref="dataSource" />

		<property name="annotatedClasses">

			<bean class="cn.walle.core.support.hibernate.HibernateEntityClassesFactoryBean" />

		</property>

		<property name="mappingLocations">

			<bean class="cn.walle.core.support.hibernate.HibernateMappingLocationsFactoryBean" />

		</property>

<!-- 		<property name="configLocations"> -->

<!-- 			<list> -->

<!-- 				<value>classpath:jbpm4/jbpm.hibernate.cfg.xml</value> -->

<!-- 			</list> -->

<!-- 		</property> -->

		<property name="hibernateProperties">

			<value>

				hibernate.dialect=${hibernate.dialect}

				hibernate.query.substitutions=true 'Y', false 'N'

				hibernate.cache.use_second_level_cache=true

				hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

				hibernate.jdbc.fetch_size=50

				hibernate.jdbc.batch_size=25

				hibernate.show_sql=true

				hibernate.format_sql=false

				hibernate.use_sql_comments=true

			</value>

		</property>

		<property name="lobHandler" ref="lobHandler" />

	</bean>

	<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">

		<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" />

	</bean>

	<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" />

	<!-- Transaction manager for a single Hibernate SessionFactory -->

	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

		<property name="sessionFactory" ref="sessionFactory" />

	</bean>

	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">

		<property name="customEditors">

			<map>

				<entry key="java.util.Date">

					<bean class="cn.walle.circle.core.CircleDateEditor" />

				</entry>

				<entry key="java.lang.String[]">

					<bean class="cn.walle.circle.core.CircleStringArrayEditor" />

				</entry>

			</map>

		</property>

	</bean>

	<bean class="cn.walle.core.support.spring.ApplicationContextRegister" />

	<!-- Enable annotation-based configuration -->

	<context:annotation-config />

	<!-- Enable classpath scanning for managed components -->

	<context:component-scan base-package="cn"

							name-generator="cn.walle.core.support.spring.CustomBeanNameGenerator" />

	<!-- Enable @AspectJ support -->

	<aop:aspectj-autoproxy />

	<!-- Enable @Transactional support -->

	<tx:annotation-driven />

        <aop:config>

		<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..service.*Manager.*(..))" order="100" />

	</aop:config>

	<tx:advice id="txAdvice">

		<tx:attributes>

			<tx:method name="*" rollback-for="Throwable" />

		</tx:attributes>

	</tx:advice>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: