spring mvc常用注解@Component @Controller @Service @Repository
- 博客分类:
- ssih...
注解用了之后,会在*.xml文件中大大减少配置量。以前我们每个Bean都得到配置文件中配置关联下。spring2.5后,引入了完整的annotation配置注解,使得我们的程序配置更简单更容易维护。
@Component;@Controller;@Service;@Repository
在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到spring的管理中了。而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。
@Repository标签是用来给持久层的类定义一个名字,让Spring根据这个名字关联到这个类。
例如:
@Repository("userDao")
public class UserDaoImpl implements UserDao{
........................................
}
声明了UserDaoImpl 在Spring容器中叫userDao这个名字。
@Service是用于服务层的IServiceImpl类文件,功能与@Repository类似。
另外标签:@Autowired 用来注入。
例如:
@Autowired
private UserDao userDao;
这样就注入进去了,相当于我们new了个实现类,我们就无需写setter方法了。
我们还得有配置文件进行配置:
这样就把com.zxr.manager包下的所有.*DaoImpl,.*ServiceImpl都注册关联到Spring容器中去了。
-------------------------------------------------------------
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
注入方式:
把DAO实现类注入到action的service接口(注意不要是service的实现类)中,注入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,
然后属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。
在接口前面标上@Autowired注释使得接口可以被容器注入,如:
[java] view plaincopy
@Autowired
@Qualifier("chinese")
private Man man;
当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略,只写@Autowired。
@Component;@Controller;@Service;@Repository
在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到spring的管理中了。而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。
@Repository标签是用来给持久层的类定义一个名字,让Spring根据这个名字关联到这个类。
例如:
@Repository("userDao")
public class UserDaoImpl implements UserDao{
........................................
}
声明了UserDaoImpl 在Spring容器中叫userDao这个名字。
@Service是用于服务层的IServiceImpl类文件,功能与@Repository类似。
另外标签:@Autowired 用来注入。
例如:
@Autowired
private UserDao userDao;
这样就注入进去了,相当于我们new了个实现类,我们就无需写setter方法了。
我们还得有配置文件进行配置:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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"> <context:annotation-config/> <context:component-scan base-package="com.zxr.manager"> <context:include-filter type="regex" expression=".*DaoImpl"/> <context:include-filter type="regex" expression=".*ServiceImpl"/> </context:component-scan> </beans>
这样就把com.zxr.manager包下的所有.*DaoImpl,.*ServiceImpl都注册关联到Spring容器中去了。
-------------------------------------------------------------
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
注入方式:
把DAO实现类注入到action的service接口(注意不要是service的实现类)中,注入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,
然后属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。
在接口前面标上@Autowired注释使得接口可以被容器注入,如:
[java] view plaincopy
@Autowired
@Qualifier("chinese")
private Man man;
当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略,只写@Autowired。
发表评论
-
Spring MVC Controller单例陷阱
2014-09-12 10:01 922Spring MVC Controller默认是单例的: 单 ... -
Spring MVC模型(Model)层和视图(View)层
2014-04-20 18:57 0Spring MCV中Model层是View层的数据容器,Js ... -
springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
2014-04-20 18:52 3063spring mvc处理方法支持如下的返回方式:ModelAn ... -
applicationContext.xml 配置文件在web.xml中的写法
2014-03-11 16:28 2788applicationContext.xml 配置文件的一些认 ... -
SpringMVC和Struts2的比较
2014-02-27 19:12 743通俗说Spring的作用 Spring ... -
Struts-config.xml配置文件《action-mappings》元素的详解
2014-02-26 13:12 2445action-mappings 该元素用于将Action元素定 ... -
使用Hibernate向mysql数据库中插入中文,数据库中显示??乱码
2014-02-24 23:02 1011[size=medium]通过网上学习,要求修改Hiberna ... -
在web.xml中通过contextConfigLocation配置spring
2014-02-24 21:19 2312在web.xml中通过contextConfigLocatio ... -
Struts-config.xml配置action-mappings元素略计
2014-02-24 16:58 632action-mappings 该元素用于将Action元素 ... -
Spring的DataAccessException略记
2013-07-08 19:23 2450Spring的DAO框架没有抛出 ... -
Result Type(Struts.xml配置)
2013-06-25 08:35 620一个提交到服务器的处理通常可以分为两个阶段: 第一个阶段查询服 ... -
ibatis常用标记略记
2013-06-23 21:12 968isNull判断property字段是否是null,用isEm ... -
ibatis的iterate使用
2013-06-23 20:38 1082ibatis的iterate使用 Iterate:这属性遍历 ... -
Struts2 两个Action之间动态传参
2013-06-21 10:00 1043两个Action 动态传参数 研究了近两天的时间 ... -
spring入门之—第一步
2013-06-13 15:26 721注: 本文用的是spring-framework-3.1.1. ... -
Spring的ApplicationContext加载多个配置文件的三种方式
2013-06-13 15:09 18271.第一种,使用数组方式 ApplicationConte ... -
详解iBaits中SqlMapClientTemplate的使用
2013-06-05 16:07 0pache iBatis(现已迁至Google Code下发展 ... -
Struts2.0默认支持多种格式的result type
2013-06-05 16:00 853<action name="attachmen ... -
maven+ssi对oracle实现增删改查(二)
2013-06-05 12:06 11961.index.jsp访问项目默认页面 <%@ page ... -
maven+ssi对oracle实现增删改查(一)
2013-06-05 11:40 1228-. 1.Oracle10g 的表结构语句,需要内容的自己添加 ...
相关推荐
Spring注解@Component、@Repository、@Service、@Controller区别.doc
Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。
NULL 博文链接:https://bijian1013.iteye.com/blog/2064702
基于注解的spring mvc,dao 层注解:@Repository("userDao"), entity层注解:@Entity,service层注解:@Service("userService"), action层注解:@Controller("userController") @RequestMapping("/user.do")
详细介绍了Spring Boot最常用的30个注解,包含概念、原理、示例 Spring Boot最常用的30个注解 一、 @SpringBootApplication 二、 Spring Bean 相关 1 @Controller 2 @Service 3 @Repository 4 @Component 5 @Bean 6 ...
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...
Spring @讲义.txt Spring注解@Component、@Repository、@Service、@Controller区别
在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...
凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处需要注入对象,所以用@Resource就有了ref的功效。 要用注解注入方式...
关于Spring注解的配置,使用都有详细的注释说明 spring的MVC架构这里不是很明显,但LZ稍作添加即可!
spring注解开发@PreDestroy除了@Component外,Spring提供了3个功能基本和@Component等效的注解 @Repository 用于对DAO实现类进行标注 @Service 用于对Service实现类进行标注 @Controller 用于对Controller实现类进行...
2. Controller 注解 3. Service 注解 4. Component 注解 5. Repository 注解 6. CookieValue 注解 7. PathVariable 注解 8. RequestBody 注解 9. RequestHeader 注解 10. RequestMethod 类 11. RequestParam 注解 12...
面试遇到的问题 1.spring的AOP/IOC怎么用 Ioc: ...Aop: ... 2.设计模式 单例模式 ...3.Spring的优越性 ...@Repository 对Dao实现类进行注解 (特殊的@Component) @Service 用于对业务逻辑层进行注解, (特殊的@Compone
- @Component @Controller @Service @Repository 区别? <!-- /TOC --> Spring MVC常见知识点及源码解析 MVC 是什么 / 有什么优点? MVC是一种设计模式,遵循 模型(Model),视图(View) 和 控制器(Controller)的...
Spring MVC的常用注解 @Controller @RequestMapping @ResponseBody @RequestBody @PathVariable @RestController Spring MVC的基本配置 静态资源映射 拦截器配置 @ControllerAdivce ...
项目分层之后(引入dao,service,web层之后), @Component注解还有三个分身---@repository ,@Service,@Controller。这三个注解怎么用,以后再说,目前都使用@Component。 1.1 为Car类加@Component注解 注解也是要用...
还包括maven,很实用,很详细,配置很清晰。
Spring Boot应用程序中常用注释的列表 本文档包含Spring Boot应用程序中常用注释的不完整列表。 它应该用作快速查找列表,有关详细和全面的信息,请阅读官方的javadocs和文档。 内容 核心弹簧 注释的方法产生一个由...
Spring2.5注解(标注)学习笔记: 一句话:对开发人员来说:最常用的Spring2.5最常用的注解就4个: @Resource @Qualifier @Service @Repository