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方法
阅读全文
0 0
- spring整合 xml配置
- spring整合hibernate xml配置
- Spring+Mybatis整合配置xml
- 【Spring】XML配置整合Mybatis
- spring-mybatis整合xml配置
- Spring整合Hibernate基于xml配置
- maven+Spring+SpringMVC+Mybatis 整合 xml配置
- maven+Spring+Struts2+Hibernate 整合 xml配置
- Spring与Hibernate 整合 xml配置
- spring整合struts2之web.xml配置
- SpringMVC与Spring整合,配置web.xml
- spring+hibernate整合web.xml、applicationContext.xml配置
- spring整合hibernate(注解、xml)applicationContext.xml配置
- spring整合hibernate(注解、xml)applicationContext.xml配置
- spring+hibernate整合web.xml、applicationContext.xml配置
- spring整合hibernate(注解、xml)applicationContext.xml配置
- spring整合hibernate(注解、xml)applicationContext.xml配置
- spring整合hibernate(注解、xml)applicationContext.xml配置
- 分析 | 无人驾驶汽车将如何彻底地改变城市面貌?
- 讨论 | 我们距离电影里的人工智能机器人到底有多远?
- IDEA 错误:找不到或无法加载主类
- angularJS四大特性
- Oracle 全局临时表
- spring整合 xml配置
- Matlab GUI界面,按钮
- linux下性能监控命令
- C#中使用SQL语句生成DataTable,对不同的数据库,处理方式不同,返回的结果属性不同
- 【Leetcode-Easy-169】 Majority Element
- 信息系统架构中间件
- 今晚直播 | 泰坦尼克号经典案例分析
- ssh整合struts
- 文档总结9-linux中的openssh-server