SSH2搭建篇04——配置spring注解
来源:互联网 发布:linux启动过程详解 图 编辑:程序博客网 时间:2024/06/05 02:18
感觉使用spring注解相当方便,就对原来的项目做了下改变,主要是对spring配置文件的改变
1:applicationContext-hibernate.xml
<?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:aop="http://www.springframework.org/schema/aop" 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.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"> <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${db.driverClassName}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="defaultAutoCommit" value="false" /><property name="maxActive" value="150" /> <property name="maxIdle" value="100" /> <property name="maxWait" value="60000" /> <property name="minIdle" value="5" /> <property name="testOnBorrow" value="true"/> <property name="testWhileIdle" value="true"/> <property name="validationQuery" value="select 1 from dual" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> --> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> </props> </property> <property name="mappingResources"> <list> <value>com/yeshun/bean/User.hbm.xml</value> </list> </property> </bean> <!--jdbcTemplate--><bean id="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource" /></bean><!--transactionManager--><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean> <!-- 配置hibernateTemplate模板 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
2:applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop" 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.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"> <!-- Hibernate configuration --><import resource="classpath:applicationContext-hibernate.xml" /> <!-- 采用注释的方式配置bean --> <context:annotation-config /> <context:component-scan base-package="com.yeshun" /> <!-- 采用注释的方式配置 aop --> <aop:aspectj-autoproxy /> <!-- 采用annotation的方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!--采用配置文件的方式配置事务 --> <!-- 配置一个事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.yeshun.service.impl.*Impl.*(..))" id="txManager" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txManager" /> </aop:config> <!-- 配置事务的具体使用方式 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <!-- 其他方法使用默认的事务设置 --> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
还有就是BaseDaoImpl 有了些改变
package com.yeshun.dao.impl;import java.io.Serializable;import java.sql.SQLException;import java.util.List;import javax.annotation.Resource;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.yeshun.dao.BaseDao;/** * @author yeshun */public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {protected @Resource(name = "jdbcTemplate") JdbcTemplate jdbcTemplate;@Resource(name = "hibernateTemplate")public void setSupperHibernateTemplate(HibernateTemplate hibernateTemplate) {super.setHibernateTemplate(hibernateTemplate);}public Object getObject(Class clazz, Serializable id) {return getHibernateTemplate().load(clazz.getName(), id);}public Object saveObject(Object obj) throws RuntimeException, SQLException{return getHibernateTemplate().save(obj);}public void updateObject(Object obj) throws RuntimeException, SQLException{getHibernateTemplate().update(obj);} public void saveOrUpdate(Object obj) throws RuntimeException, SQLException{ getHibernateTemplate().saveOrUpdate(obj);}public void deleteObject(Object obj) throws RuntimeException, SQLException{this.getHibernateTemplate().delete(obj);}public Object removeObject(Class clazz, Serializable id) throws RuntimeException, SQLException{// TODO Auto-generated method stubObject obj = this.getObject(clazz, id);if(obj!=null)deleteObject(obj);return obj;}public java.util.List getAll(Class clazz) {List list=getHibernateTemplate().loadAll(clazz);return list;}}
别的,基本是在DaoImpl 的类上面加上@Repository("UserDaoImpl") (UserDaoImpl 相当于spring bean的名字)
package com.yeshun.service.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.yeshun.bean.User;import com.yeshun.dao.UserDao;import com.yeshun.service.UserService;@Transactional@Service("UserServiceImpl")public class UserServiceImpl implements UserService{private @Resource(name = "UserDaoImpl")UserDao userDao;public List<User> findAllUser() {// TODO Auto-generated method stubreturn userDao.findAllUser();}}
也附上源码的下载地址点击打开链接
- SSH2搭建篇04——配置spring注解
- SSH2搭建篇03——spring
- SSH2搭建篇01——strusts2
- SSH2搭建篇02——hibernate
- 最新SSH2环境搭建——基础配置(1)
- ssh2项目搭建注解形式
- SSH2整合之注解篇
- Spring注解配置事务管理——问题
- Spring 注解配置——@Autowired
- spring—使用注解配置Bean
- spring(七)ssh2框架搭建
- Spring ssh2框架简介和搭建步骤
- 搭建jetty spring 框架 配置spring 注解 <二>
- 1、Spring MVC框架搭建——基于注解方式
- 最新SSH2环境搭建——代码示例(2)
- 【ssh2】配置,新手入门级——Struts2配置(大牛勿喷)
- 基于注解的Spring MVC 框架搭建及配置详解
- SSM搭建-Spring mvc基于注解的配置(13)
- ORACLE GROUPING_ID函数
- 【Android】Android SDK下载和更新失败的解决方法!!!
- 常用语句
- iOS程序之事件处理流程
- 【Windows源码分析】(一)初始化内核与执行体子系统
- SSH2搭建篇04——配置spring注解
- Qt中的菜单类使用
- 各种事
- Java模拟发送键盘消息 shift组合键
- Oracle 11gR2数据库已不再包含css配置命令 - localconfig
- Android四大基本组件介绍与生命周期
- Hibernate关联映射之多对一单向关联映射
- 看我可以坚持多久..
- POJ 1328 Radar Installation