前言:在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. flume:spooldir采集日志,kafka输出的配置问题

    flume配置: #DBFile DBFile.sources = sources1 DBFile.sinks = sinks1 DBFile.channels = channels1 # DBFil ...

  2. Hadoop2.5.0 搭建实录

    目录: 第一步:准备相关材料 第二步:虚拟机环境搭建 第三步:用户信息 第四步 安装.配置Java环境 第五步 Zookeeper安装配置 第六步 Hadoop安装.配置 第七步:HBase安装部署 ...

  3. ORACLE 解锁、找回表和找回程序语句

    最近在工作中同事们经常遇到锁表.误删表和程序覆盖的情况,现总结下遇到这三种情况的解决方案: 1.暴力删除锁表 当表被某些语句占用无法停止,或者出现事物阻塞的情况下,需要手动删除锁(万不得已的情况下用) ...

  4. Redis学习笔记4-Redis配置详解

    在Redis中直接启动redis-server服务时, 采用的是默认的配置文件.采用redis-server   xxx.conf 这样的方式可以按照指定的配置文件来运行Redis服务.按照本Redi ...

  5. nginx $document_uri 防止死循环

    $document_uri 表示访问的url 现在我的需求是,访问 www.xxx.com 请求到 www.xxx.com/bbs/ 在nginx配置文件中加入 if ($document_uri ! ...

  6. 架构实例之Demo_JSP

    架构实例之Demo_JSP 1.开发工具和开发环境       开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13 开发环境:W ...

  7. 数据结构(Java描述)之线性表

    基础概念 数据结构:是相互之间存在一种或多种关系的数据元素的集合. 逻辑结构和物理结构 关于数据结构,我们可以从逻辑结构和物理结构这两个维度去描述 逻辑结构是数据对象中数据元素之间的关系,是从逻辑意义 ...

  8. JSON 字符串中的中括号和大括号区别详解

    json 变量有两种可能, 可能是一个对象, (类似 类的实例), 也可能是一个数组!! 主要是要 从 ""语义" 上来分析, 到底该用 大括号还是用中括号: 如果从语义 ...

  9. Java并发编程:深入剖析ThreadLocal

    原文出处: 海 子 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理.首先,本文先谈一下对ThreadLocal的理解,然后根据Threa ...

  10. Reverse Nodes in k-Group

    Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...