spring中的@import注解

  • 当前使用版本

jdk:1.8
springboot:2.0.2
  • 操做部分

  • 方法1

@Import 该注解的方式为 引入一个类或者多个类到spring容器内;
//在spring扫描不到的包里面定义一个类
public class Broker {
    public void testBroker() {
        System.out.println("Broker.testBroker");
    }
}
//在spring扫描到的包里定义一个类,例如controller中:
@RestController
@Import(Broker.class)
public class UserController {
    @Autowired
    Broker broker;

    @GetMapping("/get")
    public String get() {
        broker.testBroker();
        return "success";
    }
}
http://localhost:8080/get 访问
结果为:Broker.testBroker

当前表示的是:Broker被加载到spring中能够 被定义调用
//若是定义的为接口的话,能够@Import({实现类1.class, 实现类2.class}) 这种方式会报错 ield demoService in com.apple.controller.UserController required a single bean, but 2 were found:

public interface DemoService {
    public void test();
}

public class DemoServiceImpl implements DemoService {
    @Override
    public void test() {
        System.out.println("DemoServiceImpl.test");
    }
}

public class DemoService2Impl implements DemoService {
    @Override
    public void test() {
        System.out.println("DemoService2Impl.test");
    }
}


@RestController
@Import({DemoServiceImpl.class, DemoService2Impl.class})
public class UserController {
    @Autowired
    DemoService demoService;

    @GetMapping("/get")
    public String get() {
        demoService.test();
        return "success";
    }
}
//对于上面的报错信息能够更改成:
@Service("demo")
public class DemoServiceImpl implements DemoService {
    @Override
    public void test() {
        System.out.println("DemoServiceImpl.test");
    }
}

@RestController
@Import({DemoServiceImpl.class, DemoService2Impl.class})
public class UserController {
    @Resource(name="demo")
    DemoService demoService;

    @GetMapping("/get")
    public String get() {
        demoService.test();
        return "success";
    }
}
  • 方法2

//上面的方式使用起来挺方便的,可是若是是一个接口有两种实现,上面的方法,要在实现类中定义类的别名,也能够在一个类中定义两种实现的别名

public class MyImportDefinitionRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        BeanDefinition definition1 = BeanDefinitionBuilder.rootBeanDefinition(DemoServiceImpl.class).getBeanDefinition();
        beanDefinitionRegistry.registerBeanDefinition("aa", definition1);

        BeanDefinition definition2 = BeanDefinitionBuilder.rootBeanDefinition(DemoService2Impl.class).getBeanDefinition();
        beanDefinitionRegistry.registerBeanDefinition("bb", definition2);
    }
}

@RestController
@Import(MyImportDefinitionRegister.class)
public class UserController {
    @Resource(name = "aa")
    DemoService demoService;

    @GetMapping("/get")
    public String get() {
        demoService.test();
        return "success";
    }
}
  • 另外一种定义

若是自定义一个注解,注解中定义@Import,则对应的该类也会到入 对应的类信息
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ImportDirect.class)
public @interface MyAnnocation {
    public String value() default "default";
}

@RestController
@MyAnnocation
public class UserController {

    @Autowired
    ImportDirect importDirect;

    @GetMapping("/get")
    public String get() {
        importDirect.test();
        return "success";
    }
}
相关文章
相关标签/搜索