组合注解与其注解的注解具有相同的作用
1. 组合注解
package com.annotation;
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;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@ComponentScan
public @interface CompositeConfiguration {
String[] values() default {};
}
2. Service类
package com.annotation;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
public void outputResult() {
System.out.println("We can also get bean from composite annotation.");
}
}
3. Configuration annotated by composite annotation
package com.annotation;
@CompositeConfiguration
public class DemoConfig {
}
4. Test
package com.annotation;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DemoConfig.class})
public class Bootrap {
@Autowired
DemoService service;
@org.junit.Test
public void test() {
service.outputResult();
}
}