Spring高级应用之组合注解和元注解

1.核心概念:

元注解:可以注解在其他注解上的注解;被注解的注解成为组合注解;

2.组合注解的定义步骤


定义组合注解

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@Documented

@Configuration

@ComponentScan

public @interface TangoConfig {

String[] value() default {};

}

使用组合注解

@TangoConfig("com.tango.spring.MetaAnnotation")

public class ServiceConfig {

}

测试组合注解是否生效的Service类

@Service

public class PrintServices {

public void print() {

System.out.println("这是一场没有结束的表演!");

}

}

主类进行测试

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {

public static void main(String[] args) {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ServiceConfig.class);

    PrintServices printService = context.getBean(PrintServices.class);

    printService.print();

}

}

你可能感兴趣的:(Spring高级应用之组合注解和元注解)