`
erichua
  • 浏览: 510315 次
  • 性别: Icon_minigender_2
  • 来自: 远方
社区版块
存档分类
最新评论

Spring总结-----@Component,@Service,@Controller,@Repository

阅读更多

在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。Spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息: 

<?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: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/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>

<context:component-scan base-package=”com.eric.spring”> 
</beans> 
其中base-package为需要扫描的包(含所有子包) @Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 
@Service public class VentorServiceImpl implements iVentorService { 
} @Repository public class VentorDaoImpl implements iVentorDao {
} getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() {
}
@PreDestroy public void destory() {
}
 
10
0
分享到:
评论
3 楼 wenjinglian 2010-10-10  
2 楼 erichua 2009-10-23  
原来是用code包含起来的,不知道为什么上传后就变了。
而且回车也被去掉了。
myali88 写道
文章代码用标注一下,方便看了。
<?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: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/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>

1 楼 myali88 2009-10-20  
文章代码用标注一下,方便看了。
<?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: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/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>

相关推荐

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

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

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

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

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

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

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

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

    springboot学习思维笔记.xmind

    @Service在业务逻辑层(service层) @Repository在数据访问层(dao层) @Controller在展现层(MVC→SpringMVC) 注入Bean的注解 @Autowired:Spring提供的注解 @Inject:JSR-330提供的注解 ...

    Spring @讲义.txt

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

    SpringMVC常见知识点.md

    - @Component @Controller @Service @Repository 区别? &lt;!-- /TOC --&gt; Spring MVC常见知识点及源码解析 MVC 是什么 / 有什么优点? MVC是一种设计模式,遵循 模型(Model),视图(View) 和 控制器(Controller)的...

    spring-boot-reference.pdf

    Controller endpoints 50.9. Health Information 50.9.1. Auto-configured HealthIndicators 50.9.2. Writing Custom HealthIndicators 50.9.3. Reactive Health Indicators 50.9.4. Auto-configured ...

    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的核心注解,包括但不限于@Component、@Repository、@Service、@Controller和@Autowired。这些注解简化了配置过程,减少了样板代码,并使得组件之间的耦合度降低,更有利于单元...

    spring02-5

    类的注解步骤:开启类扫描、添加注解(Component、Repository 、Service 、Controller )

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

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

    spring-boot-annotation-list:Spring Boot应用程序中常用注解的精选列表

    Spring Boot应用程序中常用注释的列表 本文档包含Spring Boot应用程序中常用注释的不完整列表。 它应该用作快速查找列表,有关详细和全面的信息,请阅读... @Repository-将带注释的类标记为Bean(通常提供数据访问的

    Java面试可能问的问题.docx

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

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

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

    SSH笔记-annotation配置注入关系1

    SSH笔记-annotation配置注入关系(使用@Component、@Service、@Repository、@Controller)

    Spring注解开发

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

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

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

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

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

Global site tag (gtag.js) - Google Analytics