`
fengjianchao_vip
  • 浏览: 42298 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

@Component @Service @Controller @Repository注解使用

阅读更多

@Component 相当于实例化类的对象。

 

通过在 classpath 中通过自动扫描方式把组建纳入 spring 容器管理。

要使用自动扫描机制我们需要打开一下配置信息:

 

Bean.xml代码   收藏代码
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.        xmlns:context="http://www.springframework.org/schema/context"   
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5 .xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5 .xsd">  
  9.            <context:component-scan base-package="com.zchen" />  
  10. </beans>  

 

 

注:前面讲要使用注解需要配置: <context:annotation-config />但如果使用了@Component就不需要加它了,因为:<context:component-scan base-package="com.zchen">里面默认了<context:annotation-config />。

Java代码   收藏代码
  1. @Component ( "userService" )  
  2. public   class  UserService {  

 @Service用于标注业务层组件、

 @Controller用于标注空竹曾组件(如Struts中的action)

 @Repository用于标注数据访问组件即DAO组件

 @Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注,(现在可以都用此注解)

分享到:
评论

相关推荐

    Spring注解@Component、@Repository、@Service、@Controller区别.doc

    Spring 注解@Component、@Repository、@Service、@Controller 区别 在 Spring 框架中,@Component、@Repository、@Service、@Controller 是四个常用的注解,它们都是继承自 @Component 注解,用于标注不同的组件或 ...

    Spring注解 @Component、@Repository、@Service、@Controller区别

    Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。

    Spring核心注解深入解析:提升开发效率

    在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...

    Spring注解 - 52注解 - 原稿笔记

    在火狐中显示可能会有问题,大家都是... @RequestMapping , @RequestParam , @Resource , @ResponseBody , @RestController , @Scope , @Service , @Validated , @Value , @WebFilter , @WebInitParam , @WebListener

    基于框架的Web开发-装配Bean自动装配.doc

    项目分层之后(引入dao,service,web层之后), @Component注解还有三个分身---@repository ,@Service,@Controller。这三个注解怎么用,以后再说,目前都使用@Component。 1.1 为Car类加@Component注解 注解也是要用...

    Java面试可能问的问题.docx

    面试遇到的问题 1.spring的AOP/IOC怎么用 Ioc: ...Aop: ... 2.设计模式 单例模式 ...4.SpringMVC注解 @Controller ...@Component 在类定义之前添加@Component注解,他会...@Service 用于对业务逻辑层进行注解, (特殊的@Compone

    SpringBoot常用注解详解含使用示例(值得珍藏)

    本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...

    Spring @讲义.txt

    Spring @讲义.txt Spring注解@Component、@Repository、@Service、@Controller区别

    Spring注释 注入方式源码示例,Annotation

    凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处需要注入对象,所以用@Resource就有了ref的功效。 要用注解注入方式...

    Spring Boot最常用的30个注解.docx

    2 @Service 3 @Repository 4 @Component 5 @Bean 6 @Configuration 三、 注入相关 1 @Resource 2 @Autowired 3 @Qualifier 4 @value 5 @ConfigurationProperties 四、 HTTP请求相关 1 @GetMapping 2 @PostMapping 3 ...

    Spring注解开发

    spring注解开发@PreDestroy除了@Component外,Spring提供了3个功能基本和@Component等效的注解 @Repository 用于对DAO实现类进行标注 @Service 用于对Service实现类进行标注 @Controller 用于对Controller实现类进行...

    springMVC详解以及注解说明

    • @Service • @Autowired • @RequestMapping • @RequestParam • @ModelAttribute • @Cacheable • @CacheFlush • @Resource • @PostConstruct • @PreDestroy • @Repository • @Component ...

    Spring常用注解(收藏大全).docx

    根据业务逻辑层、数据访问层和展现层的不同,Spring 提供了相应的注解,例如 `@Service`、`@Repository` 和 `@Controller`。 * `@Component`:声明当前类是一个组件。 * `@Service`:在业务逻辑层使用,表明该类是...

    spring mvc 注解

    2. Controller 注解 3. Service 注解 4. Component 注解 5. Repository 注解 6. CookieValue 注解 7. PathVariable 注解 8. RequestBody 注解 9. RequestHeader 注解 10. RequestMethod 类 11. RequestParam 注解 12...

    IOC 基于 注解方式 实现- 半自动化配置

    @Service: 一般用来修饰 业务service层 @Repository: 一般用来修饰 数据访问dao层 @Component: 当一个类 , 分不清是 哪个层 可以用这个注解来修饰 @Controller: 一般用来修饰 控制层 @Autowired @Qualifier ...

    Spring注解详解

    * `@Service`: 标记一个类作为服务。 * `@Autowired`: 自动装配依赖项。 * `@RequestMapping`: 映射请求和处理方法。 * `@RequestParam`: 绑定请求参数。 * `@ModelAttribute`: 绑定模型对象。 * `@Cacheable`: 缓存...

    Feign配置扫描包出现奇葩问题.docx

    在这里,@SpringBootApplication注解指定了要扫描的包,Spring Boot应用程序将会扫描这些包中的@Component、@Controller、@Service、@Repository等注解,并将其注册到Spring容器中。 因此,要解决Feign配置扫描包...

    Spring组件自动扫描详解及实例代码

    Spring组件自动扫描详解及实例代码 ...其他特定的注解有@Repository、@Service和@Controller,它们分别标识了持久层、服务处和表现层的组件。 实现方法 User.Java package com.zzj.bean; impor

    Angular 理解module和injector,即依赖注入

    依赖注入(DI)的好处不再赘言,使用过spring框架的都知道。...比如spring中,服务的注册是通过xml配置文件的标签或是注解@Repository、@Service、@Controller、@Component实现的;对象的获取可以Applicati

    个人整理的Spring、SpringMVC、MyBatis相关知识的思维导图

    * @Service 业务逻辑层,Service 层 * @Controller 控制层,Controller 层 * @Repository 持久层,dao 层 Spring 基于 XML 配置文件方式实现 * &lt;bean id="" class=""&gt;&lt;/bean&gt; + id 属性:唯一表示 + class 属性...

Global site tag (gtag.js) - Google Analytics