首页 > 解决方案 > 如何在spring中创建注释

问题描述

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Import({
        testMethod.class
})
public @interface test{
    public String value() default "";
}

@Component
public class testMethod{
...
}

在我的控制器中,我想使用我创建的注释

@test
@RequestMapping(...)
public response getAll(){
...}

我在testMethod中放了断点,它无法到达断点。似乎找不到 testMethod 组件。

标签: spring-boot

解决方案


您需要导入一个配置类。如文档所述,@Import 注释用于导入配置,如下所示。

指示要导入的一个或多个 {@link Configuration @Configuration} 类。

public class TestBean {
    public TestBean() {
    }
}

@Configuration
public class TestMethod {
    @Bean
    public TestBean testBean() {
        return new TestBean(); //put break-point here
    }
}



推荐阅读