前言:在web项目中引入spring框架中的配置文件,我们给每一个java bean进行相关配置可以非常安全,便捷的管理我们的bean。那么,问题来了,如果一个项目中所涉及到的java bean十分庞大,而且每一个bean中的配置都是大同小异的,那么这份applicationContext.xml文件恐怕是无能为力了。接下来,我们使用spring的注解便可以很好的解决这一问题。

首先:我们浏览一下我们原始的applicationContext.xml中的部分配置

   <bean id="myNewsAction" class="news.action.NewsAction" scope="prototype">
<property name="ns" ref="myNewsService"></property>
</bean> <bean id="myNewsService" class="news.service.NewsServiceImpl" scope="prototype">
<property name="nd" ref="myNewsDao"></property>
</bean> <bean id="myNewsDao" class="news.dao.NewsDaoImpl" scope="prototype">
<property name="sf" ref="mySessionFactory"></property>
</bean>

解析:在这个代码段中我们可以看出,我们的控制器也就是我们的action访问的是我们的service层,而service层则是访问的数据层dao。在这种传统的写法中每个类有什么属性要注入非常明显,而今天我们要做的就是要简化这份配置文件。

接下来:如果我们把这份配置文件简化成这样

     <bean id="myNewsAction" class="news.action.NewsAction" scope="prototype"></bean>

     <bean id="myNewsService" class="news.service.NewsServiceImpl" scope="prototype"></bean>

     <bean id="myNewsDao" class="news.dao.NewsDaoImpl" scope="prototype"></bean>    

解析:我们只是绑定了每个bean,但是并没有为其注入属性。其实我们是用到了spring的@Autowired,@Qualifier这两个注解

     @Autowired
@Qualifier("mySessionFactory")
private SessionFactory sf;

解析:在@Qualifier这个注解中我们申明其引用的是哪一个bean,spring便会自动为其注入这个实例,并且属性的set方法也可省略

但是:经过上面的一番操作仿佛没有给我省多少事,别急,认真看完本篇博客的人才知道有用的东西在最后。哈哈哈!

 <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 基于news这个包自动扫描其中的类 ,也会自动注入解析器-->
<context:component-scan base-package="news"></context:component-scan> <!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <bean id="mySessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
</bean> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> </bean> </beans>

解析:从这份applicationContext.xml文件中我们可以明显的看到我们压根没有给我们的java bean进行相关配置,只是配置了一些基本的数据源。唯一多了一行

<context:component-scan base-package="news"></context:component-scan>通过这个节点的base-package属性可以配置spring需要自动注入的哪个基包。
此时便是spring的@Controller @Service @Repository这三个注解起作用的时候了
 @Controller("myNewsAction")
@Scope("prototype")
public class NewsAction extends ActionSupport { @Autowired
@Qualifier("myNewsService")
private NewsService ns;
 @Service("myNewsService")
@Scope("prototype")
public class NewsServiceImpl implements NewsService { @Autowired
@Qualifier("myNewsDao")
private NewsDao nd;
 @Repository("myNewsDao")
@Scope("prototype")
public class NewsDaoImpl implements NewsDao { @Autowired
@Qualifier("mySessionFactory")
private SessionFactory sf;

解析:①,注解@Controller为我们的控制器action类的类注解相当于applicationContext.xml文件中的bean节点,而括号中的值相当于bean节点中的id属性的属性值。同理:@Service为我们业务层的类注解,@Repository为数据层dao的类注解。

②,注解 @Scope("prototype") 相当于applicationContext.xml文件中bean节点中scope属性,这个非单例模式注解十分重要,主要起到线程安全,防止并发操作时出现异常的作用

小结:使用spring的类注解和属性注解确实能给我们带来许多便利,关于类属性的注解其实jdk javax.annotation.Resource包中便有@Resource注解。所以,我们当然也可以选择使用jdk的注解,不过要注意的是,千万不要把jdk的注解和spring的注解混用。在软件系统中,由于原生的jdk难免存在一些缺陷,我们在开发过程中往往需要引入各种框架,因此我们的项目便不得不与这些框架耦合在一起。虽然我们一直不希望我们的代码出现耦合,毕竟这只是一种理想状态。总之,轻度耦合一直是我们追求的代码风格。

使用spring注解@Controller @Service @Repository简化配置的更多相关文章

  1. SpringMVC常用注解@Controller,@Service,@repository,@Component

    SpringMVC常用注解@Controller,@Service,@repository,@Component controller层使用@controller注解 @Controller 用于标记 ...

  2. SpringMVC常用注解@Controller,@Service,@repository,@Component,@Autowired,@Resource,@RequestMapping

    1.controller层使用@Controller注解-用于呈现层,(spring-mvc) @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controlle ...

  3. Spring 注解@Component,@Service,@Controller,@Repository

    Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...

  4. spring自动扫描的注解@Component @Controller @Service @Repository

    @Component @Controller @Service @Repository的作用 1.@controller 控制器(注入服务)2.@service 服务(注入dao)3.@reposit ...

  5. @Component @Controller @Service @Repository@Resourse

    @Component @Controller @Service @Repository@Resourse这些全部是Spring提供的注解. 其中@Component用来表示把一个类纳入spring容器 ...

  6. Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法

    一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...

  7. Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope

    以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...

  8. Spring注解详解@Repository、@Component、@Service 和 @Constroller

    概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...

  9. 注解@Component,@Controller,@Service,@Repository简单了解

    Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring B ...

随机推荐

  1. javascript设计模式-策略模式

    策略模式笔记   将定义的一组算法封装起来,使其相互之间可以替换.   封装的算法具有一定独立性,不会随客户端变化而变化.   与状态模式异同?     1. 结构上看,它与状态模式很像,也是在内部封 ...

  2. 机器学习实战笔记(Python实现)-00-readme

    近期学习机器学习,找到一本不错的教材<机器学习实战>.特此做这份学习笔记,以供日后翻阅. 机器学习算法分为有监督学习和无监督学习.这本书前两部分介绍的是有监督学习,第三部分介绍的是无监督学 ...

  3. javascrip中parentNode和offsetParent之间的区别

    首先是 parentNode 属性,这个属性好理解,就是在 DOM 层次结构定义的上下级关系,如果元素A包含元素B,那么元素B就可以通过 parentElement 属性来获取元素A. 要明白 off ...

  4. 【转】What is an SDET? Part 2 – Skill Matrix of SDET

    What is an SDET? Part 2 ---- Skill Matrix of SDET (Instead of naming it as part 2 of What is an SDET ...

  5. 【小白的CFD之旅】03 老蓝

    第一次见到老蓝,小白都不太敢相信,对面那不修边幅的糟老头子会是自己要找的导师.嘴里叼着烟,牙都掉了好几颗,穿着还算整齐,这是小白对老蓝的第一印象,这印象并不太好,尤其是在小白发誓认真度过研究生三年时光 ...

  6. springmvc 上传下载

    springmvc文件上传下载在网上搜索的代码 参考整理了一份需要使用的jar.commons-fileupload.jar与commons-io-1.4.jar 二个文件 1.表单属性为: enct ...

  7. java设计模式之原型模式

    原型模式概念 该模式的思想就是将一个对象作为原型,对其进行复制.克隆,产生一个和原对象类似的新对象.java中复制通过clone()实现的.clone中涉及深.浅复制.深.浅复制的概念如下: ⑴浅复制 ...

  8. faster_rcnn c++版本的 caffe 封装(1)

    转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ 由于需要把FasterRCNN做的工程化,因此这里需要对Caff ...

  9. 第23章 java线程通信——生产者/消费者模型案例

    第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...

  10. java之多线程 二

    线程的生命周期: 当线程被创建并被启动时,它既不是一启动就进入了执行状态,在线程的生命周期中,它要经过new(新建),就绪(Runnable),运行(Running),阻塞(Blocked),dead ...