Spring常用注解总结(1)

前言:项目中常用的注解常看常记,总会对自己有些好处,所以在这里分享一下。
使用spring时,可以使用xml配置文件配置相关信息。但是我还是喜欢用注解的方式,因为可以充分利用反射机制获取类结构信息,而这些信息可以有效地减轻配置的工作。

@Component

表示该类是“组件”。
当使用基于注释的配置和类路径扫描时,该类被认为是自动检测的候选对象。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
    String value() default "";
}

@Controller

表示该类是“控制器”。
这个注解作为@Component的一个特定方式存在,准许通过类路径扫描来自动检测实现类。
通常情况下会结合@RequestMapping注解使用。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    @AliasFor(annotation = Component.class)
    String value() default "";
}

@Service

表示该类是“服务”。
这个注解作为@Component的一个特定方式存在,准许通过类路径扫描来自动检测实现类。
通常情况下用于service服务器接口的实现类上。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(annotation = Component.class)
    String value() default "";
}

@Repository

这个注解作为@Component的一个特定方式存在,准许通过类路径扫描来自动检测实现类。
通常情况用来标识该类为DAO。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    @AliasFor(annotation = Component.class)
    String value() default "";
}

未完待续,积累点点滴滴,一步一脚印,加油

猜你喜欢

转载自www.cnblogs.com/HYMY-L/p/10731854.html