SpringAnnotation注解之@Component,@Repository,@Service,@Controller

@Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中。这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了。初始化bean的名字为类名首字母小写

与@Component注解功能相同的注解有:@Repository,@Service,@Controller,@Component ,默认情况下Spring认为这4个注解会被认为是一个组件。

@Repository:数据层,一般放在Dao接口的实现类上

@Service:服务层,一般放在service接口的实现类上

@Controller:控制层,一般放在action上

例如:

1、配置包扫描器:

<context:component-scan base-package="com.fz.annotation"></context:component-scan>

2、Controller层

@Controller

public class UserController

3、Service层

@Service("userService")

public class UserService

4、Dao层

@Repository

public class UserDaoImpl implements UserDao

其中@Controller,@Component,@Service这些注解,如果默认括号里直接写("userService")的时候表示是value=

例如:@Service("userService") 和 @Service(value="userService") 是相等的

来自为知笔记(Wiz)

时间: 2024-07-31 14:33:21

SpringAnnotation注解之@Component,@Repository,@Service,@Controller的相关文章

Spring注解 Component Repository Service Controller区别

Spring 4.1 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能.所以,如果 Web 应

@Component @Repository @Service @Controller

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能.所以,如果 Web 应用

[转]what’s the difference between @Component ,@Repository &amp; @Service annotations in Spring

原文地址:https://www.cnblogs.com/softidea/p/6070314.html @Component is equivalent to <bean> @Service, @Controller , @Repository = {@Component + some more special functionality} That mean Service,Controller and Repository are functionally the same. The t

What&#39;s the difference between @Component, @Repository &amp; @Service annotations in Spring?

@Component is equivalent to <bean> @Service, @Controller , @Repository = {@Component + some more special functionality} That mean Service,Controller and Repository are functionally the same. The three annotations are used to separate "Layers&qu

@Component, @Repository, @Service的区别

注解 含义 @Component 最普通的组件,可以被注入到spring容器进行管理 @Repository 作用于持久层 @Service 作用于业务逻辑层 @Controller 作用于表现层(spring-mvc的注解) 原文地址:https://www.cnblogs.com/shihuibei/p/9639633.html

使用Spring的@Autowired 实现DAO, Service, Controller三层的注入(转)

简述: 结合Spring和Hibernate进行开发 使用@Autowired实现依赖注入, 实现一个学生注册的功能,做一个技术原型 从DAO(Repository) -> Service -> Controller 目录结构: 使用Maven做本地包管理, pom.xml [java] view plaincopy <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww

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

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能.所以,如果 Web 应

(转载)Spring 注解@Component,@Service,@Controller,@Repository

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能.所以,如果 Web 应

Java程序员常用的@Component、@Repository、@Controller、@Serv

Java程序员常用的@Component.@Repository.@Controller.@Service系列[案例demo3] 很多程序员通过在类上使用@Repository.@Component.@Service 和 @Constroller 注解,Spring会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中.这些类就成了 Spring受管组件.这三个注解除了作用于不同软件层次的类,其使用方式与@Repository 是完全相同的. 处