zoukankan      html  css  js  c++  java
  • Spring @Configuration @Import

    Spring @Configuration

    @Configuration 用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被 @Bean 注解的方法,这些方法将会被 AnnotationConfigApplicationContext 或 AnnotationConfigWebApplicationContext 类进行扫描,并用于构建 bean 定义,初始化 Spring 容器。

    Spring 3.0开始,@Configuration 用于定义配置类,定义的配置类可以替换xml文件,一般和 @Bean 注解联合使用。

    @Configuration 注解主要标注在某个类上,相当于xml配置文件中的<beans>
    @Bean注解主要标注在某个方法上,相当于xml配置文件中的<bean>

    @Configuration
    public class DemoConfiguration {
    
        @Bean
        public DemoBean demoBean() {
            return new DemoBean();
        }
    
    }

    相当于

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false">
      <bean id="demoBean" class="com.aircity.demo.entity.demoBean"></bean>
    </beans>

    @Import 

    @Import用来导入 @Configuration注解的配置类、注入普通类、导入 ImportSelector 的实现类或导入 ImportBeanDefinitionRegistrar 的实现类。

    @Import 注解,顾名思义,导入,即把类加入Spring IOC容器。

    假设有两个要加入IOC容器的类TestA、TestB

    @Data
    public class TestA {
        private Integer id = 1;
        private String name = "TestA";
    
        public void print(){
            System.out.println(this.toString());
        }
    }
    @Data
    public class TestB {
        private Integer id = 2;
        private String name = "TestB";
    
        public void print(){
            System.out.println(this.toString());
        }
    }

    在任何一个配置类上加@Import({TestA.class, TestB.class})……

    @Import({TestA.class, TestB.class})
    @Configuration
    public class ImportConfig {}

    然后就行了……
    随便写点代码测试一下:

    @RestController
    @RequestMapping("/import")
    public class TestImportController {
    
        @Autowired
        private TestA testA;
    
        @Autowired
        private TestB testB;
    
        @RequestMapping("/test1")
        public void test1(){
            testA.print();
            testB.print();
        }
    }

    REF

    https://blog.csdn.net/weixin_44360895/article/details/112758122

     

     

  • 相关阅读:
    UVALive2678子序列
    UVA11549计算机谜题
    UVA11520填充正方形
    LA3635派
    UVALive3971组装电脑
    记录未完成题目
    SPOJ 6219 Edit distance字符串间编辑距离
    ACM组队安排-——杭电校赛(递推)
    逆袭指数-——杭电校赛(dfs)
    油菜花王国——杭电校赛(并查集)
  • 原文地址:https://www.cnblogs.com/emanlee/p/15754180.html
Copyright © 2011-2022 走看看