Spring整合技术
作用:在框架整合方面,spring充当一个对象容器作用,在容器中管理各种bean。在JavaEE应用中,主要管理DAO、Service、Action、Handler。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注解。