spring中stereotype注解Component,Repository,Service,Controller

【README】

本文介绍了 spring4.0 下 org.springframework.stereotype 的注解类型,俗称刻板型注解(一成不变型);

包括 @Component, @Repository,@Service, @Controller ;

目录

【README】

【1】@Component注解(组件)

【2】@Repository注解(仓库)

【3】@Service注解(服务)

【4】@Controller注解(控制器)



【1】@Component注解(组件)

因为 Service, Repository , Controller 都使用到了 Component注解;被 @Controller修饰的注解被视为 spring自动扫描的候选对象;

表示带此注解的类是“组件”。 在使用基于注解的配置和类路径扫描时,此类被视为自动检测的候选对象
其他注解也可以被视为标识一个类为组件,典型的如 @Repository 或 AspectJ 的 @Aspect 注解。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any
	 */
	String value() default "";

}


【2】@Repository注解(仓库)

1.表示带该注解的类是“存储库”或仓库,最初由领域驱动设计 (Evans, 2003) 定义为“一种用于封装存储、检索和搜索行为的机制,它模拟了一个 对象集合”。

2.实现传统 Java EE 模式(例如“数据访问对象”)的团队也可以将此注解应用于 DAO 类,但在执行此操作之前应注意理解数据访问对象DAO和 DDD 样式仓库之间的区别

3.当与 PersistenceExceptionTranslationPostProcessor 结合使用时,该注解修饰的类可以进行 Spring DataAccessException 转换。

4.从 Spring 2.5 开始,该注解也作为 @Component 的特例,允许通过类路径扫描自动检测被注解的类。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any
	 */
	String value() default "";

}

【3】@Service注解(服务)

表示带该注解的类是“服务”,最初由领域驱动设计(Evans,2003)定义为“作为独立于模型中的接口提供的操作,没有封装状态”。
也可以表示被修饰的类是“业务服务外观”。 此注解是通用的版型对象(一成不变),个别团队可能会缩小其语义范围并酌情使用。

该注解作为@Component 的特例,允许使用方通过类路径扫描自动检测被注解的类。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any
	 */
	String value() default "";

}

【4】@Controller注解(控制器)

1.表示带该注释的类是“控制器”(例如 Web 控制器)
2.该注解作为@Component 的特例,允许通过类路径扫描自动检测实现类。

3.它通常与 RequestMapping注解修饰的处理器方法结合使用。

org.springframework.web.bind.annotation.RequestMapping

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any
	 */
	String value() default "";

}

猜你喜欢

转载自blog.csdn.net/PacosonSWJTU/article/details/120847403
今日推荐