春の統合技術
役割:フレームワークの統合に関して、springはオブジェクトコンテナーとして機能し、コンテナー内のさまざまなBeanを管理します。JavaEEアプリケーションでは、主にDAO、サービス、アクション、およびハンドラーを管理します。SpringとHibernteが統合された後、SessionFactoryは永続化操作をさらに簡素化するように管理され、SpringとStruts2はホストActionに統合され、SpringとServiceレイヤーはトランザクション管理機能を提供するために統合されます。
統合技術:
- SpringフレームワークとMVCフレームワーク(Struts2とSpringmvc)の統合
- Springフレームワークと永続層フレームワークの統合(HibernateとMybatis)
SSH統合環境の構築
-
SSH統合ライブラリ
をインポートします。struts2フレームワークとSpring統合プラグインのインポートに特に注意してください。
-
Web.xmlを構成します
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<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>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 启动注解配置 -->
<context:annotation-config/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
</props>
</property>
</bean>
<!-- 扫描包: 把包之下的类,创建实例,容纳到ioc容器中 -->
<context:component-scan base-package="com.gec.login.dao.impl,com.gec.login.service.impl"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
一般的な注釈
- @Componentの一般的なアノテーション、主な機能はインスタンスを作成し、それをSpringコンテナに組み込むことです
- @Repositoryは
、データアクセス層でよく使用されます。@ Componentの基本的な機能に加えて、特定の機能もあります。たとえば、データアクセス層の場合、データアクセス例外をキャプチャします。 - @Serviceは、ビジネス層でよく使用されます。注:現在のクラスの関数を分類するのが難しい場合は、@ Componentアノテーションを使用できます。