springとstruts 2、hibernateの集積
一、スプリングとstruts 2の集積
springとstruts 2を統合するには様々な方法がありますが、ここでは簡単なプラグインを紹介します.
1、stust 2-spring-plugnパッケージを導入する
2、springを追加するプロファイル
springプロファイルAplicatonicontext.xmlデフォルトの添加位置はclassipathの下で、名称や経路を変更する必要があるなら、web.xmlで設定してください.
3、struts.xml配置
springとstrutsを統合したアクションもspring容器で管理され、struts.xmlでActionのclass属性はspringに配置されたBeanに変更されました.
4、spring配置ファイルの詳細
後ろを見てください
二、スプリングとヒベルナの集積
springとhibernateの統合後、hibernate.cfg.xmにおけるデータベースとの接続、OMPマッピング関係、及びsession factory管理は、すべてspringによって制御されます.
spring配置ファイルの詳細
springとstruts 2を統合するには様々な方法がありますが、ここでは簡単なプラグインを紹介します.
1、stust 2-spring-plugnパッケージを導入する
2、springを追加するプロファイル
springプロファイルAplicatonicontext.xmlデフォルトの添加位置はclassipathの下で、名称や経路を変更する必要があるなら、web.xmlで設定してください.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、struts.xml配置
springとstrutsを統合したアクションもspring容器で管理され、struts.xmlでActionのclass属性はspringに配置されたBeanに変更されました.
<package name="user" namespace="/user" extends="default">
<action name="*" class="user.userAction">
<result name="edit" type="freemarker">
/WEB-INF/user/user_edit.ftl
</result>
</action>
</package>
4、spring配置ファイルの詳細
後ろを見てください
二、スプリングとヒベルナの集積
springとhibernateの統合後、hibernate.cfg.xmにおけるデータベースとの接続、OMPマッピング関係、及びsession factory管理は、すべてspringによって制御されます.
spring配置ファイルの詳細
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="org.springframework.samples.petclinic.web"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/forum"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.*</value>
</list>
</property>
<!--
<property name="mappingResources">
<list>
<value>com/forum/user/entity/User.hbm.xml</value>
</list>
</property>
-->
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!--
<bean id="transationService" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
-->
<!-- ,
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>user.userManager</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transationService</value>
</list>
</property>
</bean>
-->
<!-- , -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txManager" expression="execution(* com.forum.*.*manager.*.*(..))" />
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="txManager"/>
</aop:config>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="user.userDao" class="com.forum.user.dao.impl.UserDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean id="user.userManager" class="com.forum.user.manager.impl.UserManagerImpl">
<property name="userDao">
<ref bean="user.userDao"/>
</property>
</bean>
<bean id="user.userAction" class="com.forum.user.action.UserAction">
<property name="userManager">
<ref bean="user.userManager"/>
</property>
</bean>
</beans>