@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;
}
}

spring注解 annotation的更多相关文章

  1. spring beans源码解读之--Bean的注解(annotation)

    随着spring注解的引入,越来越多的开发者开始使用注解,这篇文章将对注解的机制进行串联式的讲解,不求深入透彻,但求串起spring beans注解的珍珠,展示给大家. 1. spring beans ...

  2. Spring boot注解(annotation)含义详解

    Spring boot注解(annotation)含义详解 @Service用于标注业务层组件@Controller用于标注控制层组件(如struts中的action)@Repository用于标注数 ...

  3. (转)Spring对注解(Annotation)处理源码分析1——扫描和读取Bean定义

    1.从Spring2.0以后的版本中,Spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...

  4. Spring对注解(Annotation)处理【转】

    1.从Spring2.0以后的版本中,spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...

  5. spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on

    spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on

  6. Annotation(三)——Spring注解开发

    Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...

  7. spring注解源码分析--how does autowired works?

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...

  8. Spring注解

    AccountController .java Java代码   1.        /** 2.         * 2010-1-23 3.         */ 4.        packag ...

  9. spring注解说明之Spring2.5 注解介绍(3.0通用)

    spring注解说明之Spring2.5 注解介绍(3.0通用) 注册注解处理器 方式一:bean <bean class="org.springframework.beans.fac ...

随机推荐

  1. activemq 消息传送测试

    activemq 5.10.0,topic   messagelength字符 20000011     发送时间 接收时间 传送时间 毫秒 1 1443067284128 1443067288325 ...

  2. iOS开发中虚拟键盘相关的坑

    初学者在学习iOS开发时,遇到在一个textField中输入完内容后却发现虚拟键盘无法隐藏起来而不知所措的情况的人一定不占少数吧.这篇文章就说说我遇到的和虚拟键盘有关的三个问题及解决对策. 在模拟器测 ...

  3. Spring @PostConstruct和@PreDestroy实例

    在Spring中,既可以实现InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 在初始化和销毁回调函 ...

  4. [mark]如何删除地址栏的记录?

    比如,我输入字母a ..因为经常访问.它首先会自动帮我补填 amazon.cn 第二行出现 ali213...第三行是 alipay... 这个很简单,没必要搞得很复杂很Geek.比如楼主的情况,首先 ...

  5. MySQL -- 调优

    多数时候数据库会成为整个系统的瓶颈,比如大的数据量的插入与修改,频繁的亦或是高流量的访问,都会对数据库系统带来很大的压力.我在平时工作的时候,总是会遇到大数据量的插入.修改或是查询的操作,所以在工作的 ...

  6. 第十章 OPENWRT安装nohup,因为不明原因nohup没有安装

    Available pacaagess里找了coreutils-nohup包,安装好就行了 运行nohup自己的程序的时候可能会出现一下: root@OpenWrt:~# nohup: ignorin ...

  7. Android onConfigurationChanged 不执行

    自从Android 3.2(API 13),screen size也开始跟着设备的横竖切换而改变. 所以,在AndroidManifest.xml里设置的MiniSdkVersion和 TargetS ...

  8. java获取系统进程号

    public static final int jvmPid() { try { RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean( ...

  9. iOS:切换视图的第三种方式:UITabBarController标签栏控制器

    UITabBarController:标签栏控制器 •通过设置viewControllers属性或者addChildViewController方法可以添加子控制器 –NSArray *viewCon ...

  10. C#好书盘点

    C#是学习asp.net和.NET Winform程序的基础,所以一定要学好.从00年C#出道到现在,7年过去了,C#的书出了许多,有不少好书. 1.<21天学通C# > 作者:作者:(美 ...