@Resourse(name="  xxx") 意味从上下文找xxx名字一样的然后引入

@Repository("personDao") 意味生成一个 bean 以便于让其他高业务层的去找这个  的bean

spring.xml新加入

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd

spring.xml中

 <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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean> <context:component-scan base-package="cn.itcast.s2sh"></context:component-scan> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

impl注解  (因为声明了注解,所以不能继承HibernateDaoSupport,利用组合的方式)

 package cn.itcast.s2sh.sh.dao.impl;

 import java.io.Serializable;

 import javax.annotation.Resource;

 import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository; import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.dao.PersonDao; @Repository("personDao")   //生成beanname
public class PersonDaoImpl implements PersonDao{
@Resource(name="hibernateTemplate") //从spring.xml中引入
private HibernateTemplate hibernateTemplate; //set get方法也就不用了,注解的形式注入了
@Override
public void savePerson(Person person) {
// TODO Auto-generated method stub
this.hibernateTemplate.save(person);
} @Override
public Person getPesonById(Serializable id) {
// TODO Auto-generated method stub
return (Person) this.hibernateTemplate.load(Person.class, id);
} }
public class PersonDaoImpl extends HibernateSupport implements PersonDao
{//上面代码一样 没有成员变量hibernateTemplate
}
因为HibernateTemple 是spring提供的类,它里面有 private HibernateTemplate hibernateTemplate;的成员变量,这个成员变量也需要 HibernateSupport提供注解,但是源代码里上面没有注解,但是又需要注入这个hibernateTemplate变量,因为继承关系继承父类的,所以 改写成组合方式,不用继承。

serviceImpl

 package cn.itcast.s2sh.sh.service.impl;

 import java.io.Serializable;

 import javax.annotation.Resource;

 import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.dao.PersonDao;
import cn.itcast.s2sh.sh.service.PersonService; @Service("personService")
public class PersonServiceImpl implements PersonService{
@Resource(name="personDao")
private PersonDao personDao; @Override
@Transactional(readOnly=false) //事务
public void savePerson(Person person) {
// TODO Auto-generated method stub
this.personDao.savePerson(person);
} @Override
public Person getPersonByID(Serializable id) {
// TODO Auto-generated method stub
Person person = this.personDao.getPesonById(id);
return person;
}
}
05-11 22:33