前言:在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. .NET应用架构设计—服务端开发多线程使用小结(多线程使用常识)

    有一段时间没有更新博客了,最近半年都在着写书<.NET框架设计—大型企业级框架设计艺术>,很高兴这本书将于今年的10月份由图灵出版社出版,有关本书的具体介绍等书要出版的时候我在另写一篇文行 ...

  2. Redis时延问题分析及应对

    Redis时延问题分析及应对 Redis的事件循环在一个线程中处理,作为一个单线程程序,重要的是要保证事件处理的时延短,这样,事件循环中的后续任务才不会阻塞: 当redis的数据量达到一定级别后(比如 ...

  3. 谈谈Java中的ThreadLocal

    什么是ThreadLocal ThreadLocal一般称为线程本地变量,它是一种特殊的线程绑定机制,将变量与线程绑定在一起,为每一个线程维护一个独立的变量副本.通过ThreadLocal可以将对象的 ...

  4. Linux 之 目录和文件

    1  初识 1.1  终端 打开终端: Ctrl + Alt + F1 ~ F6 图形界面: Ctrl + Alt + F7 1.2  命令 1)  一般 date; cal - calendar; ...

  5. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  6. Jsp 错题分析

    ArrayList删除元素通过RemoveAt(int index)来删除指定索引值的元素 运行时异常都是RuntimeException类及其子类异常,如NullPointerException.I ...

  7. java线程跟多线程

    java创建线程两种方式: 1.继承Thread创建线程 /** * Created by lsf on 16/4/18. */ class NewThread extends Thread { Ne ...

  8. java多线程系类:JUC原子类:03之AtomicLongArray原子类

    概要 AtomicIntegerArray, AtomicLongArray, AtomicReferenceArray这3个数组类型的原子类的原理和用法相似.本章以AtomicLongArray对数 ...

  9. [转]用Whois获得电信运营商的IP地址是如何分配的?

    [转]用Whois获得电信运营商的IP地址是如何分配的? Linux下获得一些中国电信运营商的IP地址分配情况: APNIC是管理亚太地区IP地址分配的机构,它有着丰富准确的IP地址分配库,同时这些信 ...

  10. DOCKER是什么,它解决了什么问题?(转)

    Docker 虚拟机 1. docker与虚拟机性能比较 2. 如日中天的Docker解决了什么问题?