package ch2.annotation;
//target/elementType用來設定注解的使用范圍
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
//表明這個注解documented會被javac工具記錄
import java.lang.annotation.Documented;
//retention/retentionPolicy(保留)注解,在編譯的時候會被保留在某個(那個)特定的編譯階段
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//target/elementType用來設定注解的使用范圍:type用於類或接口上
@Target(ElementType.TYPE)
//retention/retentionPolicy(保留)注解,在編譯的時候會被保留在某個(那個)特定的編譯階段
//這種類型的Annotations將被JVM保留
@Retention(RetentionPolicy.RUNTIME)
//表明這個注解@documented會被javac工具記錄
@Documented
//組合注解
//組合configuration元注解
@Configuration
//組合ComponentScan元注解
@ComponentScan
//組合注解
public @interface WiselyConfiguration {
//覆蓋value參數
String[] value() default{};
}
package ch2.annotation;
import org.springframework.stereotype.Service;
//聲明為spring的組件
@Service
//演示服務Bean
public class DemoService {
public void outputResult()
{
System.out.println("從組合注解配置里,照樣獲得Bean");
}
}
package ch2.annotation;
//引入ch2.annotation下的包
//使用wiselyConfiguration組合注解代替@ComponentScan,@Configuration
@WiselyConfiguration("ch2.annotation")
//新的配置類
public class DemoConfig {
}
package ch2.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args)
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
DemoService demoService = context.getBean(DemoService.class);
demoService.outputResult();
context.close();
}
}