@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. linux基础命令学习 (十一)系统管理命令

    stat              显示指定文件的详细信息,比ls更详细 who               显示在线登陆用户 whoami          显示当前操作用户 hostname   ...

  2. iPhone X 适配手机端 H5 页面通用解决方案

    一:本文提供两种解决方案 1.终端解决方案(最优,建议选择) 2.web解决方案 导语: iPhone X的出现,一方面对于整个手机行业的发展极具创新领头羊的作用,另一方面也对现有业务的页面适配带来了 ...

  3. JAVA-数据类型、变量、常量

    http://blog.csdn.net/yuhailong626/article/details/7245571 http://www.cnblogs.com/JackieADBM/p/534222 ...

  4. JAVA EE 博客实例

    http://www.cnblogs.com/hoojo/category/276244.html

  5. JavaScript学习10:动态载入脚本和样式

    我们在写Web页面的时候,须要引入非常多的JavaScript脚本文件和CSS样式文件,尤其是在站点需求量非常大的时候,脚本的需求量也随之变大,这样一来,站点的性能就会大打折扣.因此就出现了动态载入的 ...

  6. C语言之数组中你所不在意的重要知识

    #include<stdio.h> void simpleArray(); void main() { simpleArray(); } //数组的简单操作 void simpleArra ...

  7. How to create a Maven web app and deploy to Tomcat - fast

    原文地址: http://www.blogjava.net/sealyu/archive/2010/01/08/308706.html Procedure Prerequisites and Assu ...

  8. andriod 下一个页面

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s ...

  9. OpenCV学习(2) OpenCV的配置

          下面我们在VS2010中编写一个简单的OpenCV程序,来看看如何在程序中使用OpenCV. 创建一个新的Win32 控制台程序,附加选项为空工程(empty project),并添加一个 ...

  10. C++对象赋值的四种方式

    1.  引用作为参数的方式传递. GetObject(Object& obj) { obj.value = value1; } 特点: 在外部构造一个对象. 把该对象以引用的方式传递到函数中. ...