一、自己写一个注解,并导入容器进行扫描
注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Service
public @interface firstAnnotation {
String value() default "默认值";
}
引用:
@firstAnnotation
public class TestService {
public String say() {
return "这是第一个服务注解" + new java.util.Date();
}
}
测试:注意这里要去扫引用注解的包,这样容器加载的时候才能扫到调用的类。否则是扫不到的,因为不在容器里头
@ComponentScan("com.wgc.configuration.service")
public class TestMyService {
public static void main(String[] args) {
//类对象拿到类注解
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestMyService.class)
.web(WebApplicationType.NONE)
.run(args);
TestService testService = context.getBean(Tes