使用spring注解@Controller @Service @Repository简化配置
前言:在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简化配置的更多相关文章
- SpringMVC常用注解@Controller,@Service,@repository,@Component
SpringMVC常用注解@Controller,@Service,@repository,@Component controller层使用@controller注解 @Controller 用于标记 ...
- SpringMVC常用注解@Controller,@Service,@repository,@Component,@Autowired,@Resource,@RequestMapping
1.controller层使用@Controller注解-用于呈现层,(spring-mvc) @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controlle ...
- Spring 注解@Component,@Service,@Controller,@Repository
Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...
- spring自动扫描的注解@Component @Controller @Service @Repository
@Component @Controller @Service @Repository的作用 1.@controller 控制器(注入服务)2.@service 服务(注入dao)3.@reposit ...
- @Component @Controller @Service @Repository@Resourse
@Component @Controller @Service @Repository@Resourse这些全部是Spring提供的注解. 其中@Component用来表示把一个类纳入spring容器 ...
- Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法
一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...
- Spring注解详解@Repository、@Component、@Service 和 @Constroller
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- 注解@Component,@Controller,@Service,@Repository简单了解
Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring B ...
随机推荐
- 0029 Java学习笔记-面向对象-枚举类
可以创建几个对象? n多个:大部分的类,都可以随意创建对象,只要内存不爆掉 1个:比如单例类 有限的几个:采用单例类的设计思路,可以只允许创建少数的几个特定的对象:还有就是枚举类. 创建少数几个对象, ...
- AsyncTask官方学习
异步任务学习 这两天使用到特别多的AsyncTask类,一块来学习一下吧 AsyncTask允许更方便和简单使用UI线程,这个类允许你在UI线程中进行后台操作和展示结果,而无需操作Thread或者ha ...
- Autofac在MVC4中牛刀小试
Autofac是传说中速度最快的一套.NET高效的依赖注入框架.Autofac的介绍与使用请去参考Autofac全面解析系列(版本:3.5). 这里介绍的已经挺详细的啦. 下面我就先来说说MVC4 ...
- Consul Windows 安装
下载文件https://www.consul.io/downloads.html, 解压完毕后只有一个consul文件 consul 启动一个 Agent consul agent -server - ...
- Linux svn的搭建与使用
Linunx svn的搭建与使........纯手打的.. 一.安装前的准备 1.1 配置yum 库 1)加载光盘 2)进入/etc/yum.repo.d目录 3)复制"rhel-debug ...
- linux tomcat 的安装
1.tomcat6 下载地址 http://tomcat.apache.org/download-60.cgi 下载的话,下载那个.tar.gz后缀名的即可. 好像在 Linux.Unix上tomca ...
- Release Management 安装 之 集成TFS
集成TFS时需要在TFS服务器执行 tfssecurity /g+ "Team Foundation Service Accounts" n:ALM\rmtfsint ALLOW ...
- Java基础知识笔记(四:多线程基础及生命周期)
一.多线程基础 编写线程程序主要是构造线程类.构造线程类的方式主要有两种,一种是通过构造类java.lang.Thread的子类,另一种是通过构造方法实现接口java.lang.Runnable的类. ...
- EF Load之详讲
参考 Loading Related Entities with Entity Framework - A Beginner's Primer
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...