spring整合 xml配置

来源:互联网 发布:云计算数据中心是什么 编辑:程序博客网 时间:2024/06/05 08:55
Spring整合hibernate
基本原理:就是由spring来管理hibernate的SessionFactory

方式一:零障碍整合(了解)

spring中提供的一个LocalSessionFacotry加载Hibernate的配置文件  (需要自己定义一个hibernate.cfg.xml)

<beanid="sessionFactoy"class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
</bean>
我们必须配置spring的ContextLoaderListener

  <context-param>
  <param-name>contextConfigLocation</param-name>
 
  <param-value>classpath:applicationContext.xml</param-value> 
  </context-param>
 
  <!-- spring 整合web -->
  <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 

方式二  spring管理hibernate配置
不在需要hibernate.cfg.xml文件,所有关于hibernate.cfg.xml文件中的配置都在spring的配置文件中来配置。

需要一个db.properties 文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///sshtest
jdbc.username=root
jdbc.password=123

spring 中的配置

<!-- 引入properties 文件 -->
<context:property-placeholderlocation="classpath:db.properties"/>

<!-- 不需要hibernate.cfg.xml 的hibernate的配置 -->
<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <propertyname="driverClass"value="${jdbc.driverClass}"></property>
     <propertyname="jdbcurl"value="${jdbc.url}"></property>
     <propertyname="user"value="${jdbc.username}"></property>
     <propertyname="password"value="${jdbc.password}"></property>
</bean>

然后创建LocalSessionFacotry 加载连接池

     <!-- 加载hibernate配置文件 -->
     <beanid="sessionFactoy"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
          
           <propertyname="dataSource"ref="dataSource">
                <props>
                <propkey="hiernate.show_sql">true</prop>
                
                <propkey="hiernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <propkey="hiernate.hbm2ddl.auto">update</prop>
                <propkey="hiernate.format_sql">true</prop>
                </props>
           </property>
     </bean>


spring 整合hibernate 后的dao

要找Dao 上继承HibernateDaoSupport  就可以获得jdbcTemplate

public class UserDaoImpl extends HibernateDaoSupportimplements IUserDao{
     @Override
     public void add(User user) {
           // TODO Auto-generated method stub
           this.getHibernateTemplate().save(user);
     }
}

xml中声明bean 引入sessionFactoy
     <!-- 声明dao-->
     <beanid="userDao"class="com.ithiema.dao.UserDaoImpl">
     <!-- 只需要注入sessionFactoyBean 就可以获取hibernate  sessionFactoyBean是对hibernate的图个封装 -->
           <propertyname="sessionFactoyBean"ref="sessionFactoyBean"></property>
     
     </bean>


spring 中配置事物管理器
     <!-- 事物管理 -->
     
     <!-- 配置事物管理器 -->
     <beanname="transactionManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager">
     
     <!-- 引入dataSource  这里我们用的是dataSource 的封装 sessionFactory  -->
     <propertyname="sessionFactoryBean"ref="sessionFactoyBean"></property>
     </bean>
     <!-- 通知 -->
     <tx:adviceid="txAdvice"transaction-manager="transactionManager">
     <tx:attributes>
     
     <tx:methodname="add"/>
     <tx:methodname="update"/>
     <tx:methodname="del"/>
     </tx:attributes>
     
     </tx:advice>
     
     <!-- 切面 -->
     
     <aop:config>
<!-- 这里只写了add()方法  如果是想匹配所有的  * *..*(..)-->
     <aop:pointcutexpression="execution(* *.add(..))"id="mypointcut"/>
     <aop:advisoradvice-ref="txAdvice"pointcut-ref="mypointcut"/>
     </aop:config>

Spring整合struts2框架

(1)spring 管理action.
1.写一个addUser.jsp
<formaction="${pageContext.request.contextPath}/user_add"method="post">
     name<inputtype="text"name="username">
     age<inputtype="text"name="age">
<inputtype="submit"value="addUser">
</form>


2.在applicationContext.xml 中配置  action

<!-- spring 中整合struts -->
     <beanid="userAction"class="com.ithiema.action.UserAction">
     <propertyname="userService"ref="userService"></property>
     </bean>

3.action 类中要提供 目标对象的set方法

public class UserAction extends ActionSupportimplements  ModelDriven<User>{
     private UserService userService;
     public void setUserService(UserService userService) {
           this.userService =userService;
     }

3.在struts 中配置
    <struts>
     <packagename="default"namespace="/"extends="struts-default">
     <!--  class 就是spring 配置的stutsbean的id -->
     <actionname="user_*"class="userAction"method="{1}">
     <resultname="success">/success.jsp</result>
     </action>
     </package>
     
     </struts>

必须在web.xml 中配置struts2 框架的filter
 <!-- spring 整合strtus-->
  <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>/*</url-pattern>
 
  </filter-mapping>

spring整合struts2框架方式二(action中自动注入service)
1.Struts.xml 中的Class需要全类名
 <struts>
     <packagename="default"namespace="/"extends="struts-default">
     <!--  class 就是spring 配置的stutsbean的id -->
     <actionname="user_*"class="com.itheima.action.UserAction"method="{1}">
     <resultname="success">/success.jsp</result>
     </action>
     </package>
     
     </struts>
这时 就会将action 中需要的注入到service中

public class UserAction extends ActionSupportimplements  ModelDriven<User>{
     private UserService userService;
     public void setUserService(UserService userService) {
           this.userService =userService;
     }




在配置文件中配置  自动注入

struts.objectFactory.spring.autoWire=name  //表示根据name 自动注入


也可以在struts.xml 中修改注入的方式

<constantname="struts.objectFactory.spring.autoWire"value="type"></constant>  <!-- 表示根据type 自动注入-->


总结:
如果struts中的class写的是全类名  则是根据自动注入 自动注入的话就要在 db.properties配置文件中 配置注入的方式
如果不是全类名而是  applicationContext.xml 中配置的action 的bean的 id那么就要在applicationContext.xml中配置action 的bean 

记住区别 非自动注入就是在applicationContext.xml 中配置了bean 而且class非全类名
          自动注入就是在   db.properties配置文件  中配置了autoWire class 为全类名
*action 中的目标对象要有set方法






原创粉丝点击