美文网首页程序员Spring
Spring中常用的@Configuration和其他配置类注解

Spring中常用的@Configuration和其他配置类注解

作者: emi1997 | 来源:发表于2019-10-16 10:15 被阅读0次
    Spring.jpg

    前言

    从Spring3.0开始,@Configuration用于定义配置类,可以替换Spring的xml配置文件,常常配合@Bean使用,一个@Configuration注解的类中可以有一个或多个@Bean注解的方法


    详细使用方法

    1. 单独使用@Configuration注解在类上
    @Configuration
    public class TestConfiguration {
        public TestConfiguration() {
            System.out.println("TestConfiguration容器启动初始化。。。");
        }
    }
    

    这种相当于加载了一个spring配置文件,<beans></beans>标签你没有配置任何一个<bean></bean>相当于下面这样的Spring的xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"
    
        xmlns:context="[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)" xmlns:jdbc="[http://www.springframework.org/schema/jdbc](http://www.springframework.org/schema/jdbc)" 
    
        xmlns:jee="[http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee)" xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)"
    
        xmlns:util="[http://www.springframework.org/schema/util](http://www.springframework.org/schema/util)" xmlns:task="[http://www.springframework.org/schema/task](http://www.springframework.org/schema/task)" xsi:schemaLocation="
    
            [http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-4.0.xsd](http://www.springframework.org/schema/beans/spring-beans-4.0.xsd)
    
            [http://www.springframework.org/schema/context](http://www.springframework.org/schema/context) [http://www.springframework.org/schema/context/spring-context-4.0.xsd](http://www.springframework.org/schema/context/spring-context-4.0.xsd)
    
            [http://www.springframework.org/schema/jdbc](http://www.springframework.org/schema/jdbc) [http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd](http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd)
    
            [http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee) [http://www.springframework.org/schema/jee/spring-jee-4.0.xsd](http://www.springframework.org/schema/jee/spring-jee-4.0.xsd)
    
            [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-4.0.xsd](http://www.springframework.org/schema/tx/spring-tx-4.0.xsd)
    
            [http://www.springframework.org/schema/util](http://www.springframework.org/schema/util) [http://www.springframework.org/schema/util/spring-util-4.0.xsd](http://www.springframework.org/schema/util/spring-util-4.0.xsd)
    
            [http://www.springframework.org/schema/task](http://www.springframework.org/schema/task) [http://www.springframework.org/schema/task/spring-task-4.0.xsd](http://www.springframework.org/schema/task/spring-task-4.0.xsd)" default-lazy-init="false">
    
    </beans>
    
    1. @Configuration注解类+@Bean注解方法
    @Configuration
    public class TestConfiguration {
        public TestConfiguration() {
            System.out.println("TestConfiguration容器启动初始化。。。");
        }
        // @Bean注解注册bean,同时可以指定初始化和销毁方法
        // @Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
        @Bean
        @Scope("prototype")
        public TestBean testBean() {
            return new TestBean();
        }
    }
    

    这等同于在<beans></beans>标签中配置一个<bean id="testBean" class="TestBean"></bean>,并且可以在@Bean中指定实例的初始化方法和销毁方法,等效于下面的Spring的xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"
    
        xmlns:context="[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)" xmlns:jdbc="[http://www.springframework.org/schema/jdbc](http://www.springframework.org/schema/jdbc)" 
    
        xmlns:jee="[http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee)" xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)"
    
        xmlns:util="[http://www.springframework.org/schema/util](http://www.springframework.org/schema/util)" xmlns:task="[http://www.springframework.org/schema/task](http://www.springframework.org/schema/task)" xsi:schemaLocation="
    
            [http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-4.0.xsd](http://www.springframework.org/schema/beans/spring-beans-4.0.xsd)
    
            [http://www.springframework.org/schema/context](http://www.springframework.org/schema/context) [http://www.springframework.org/schema/context/spring-context-4.0.xsd](http://www.springframework.org/schema/context/spring-context-4.0.xsd)
    
            [http://www.springframework.org/schema/jdbc](http://www.springframework.org/schema/jdbc) [http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd](http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd)
    
            [http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee) [http://www.springframework.org/schema/jee/spring-jee-4.0.xsd](http://www.springframework.org/schema/jee/spring-jee-4.0.xsd)
    
            [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-4.0.xsd](http://www.springframework.org/schema/tx/spring-tx-4.0.xsd)
    
            [http://www.springframework.org/schema/util](http://www.springframework.org/schema/util) [http://www.springframework.org/schema/util/spring-util-4.0.xsd](http://www.springframework.org/schema/util/spring-util-4.0.xsd)
    
            [http://www.springframework.org/schema/task](http://www.springframework.org/schema/task) [http://www.springframework.org/schema/task/spring-task-4.0.xsd](http://www.springframework.org/schema/task/spring-task-4.0.xsd)" default-lazy-init="false”>
    
    <bean id="testBean" class="TestBean"></bean>
    
    </beans>
    
    1. @Configuration注解类+ @ComponentScan注解类
    @Configuration
    //添加自动扫描注解,basePackages为TestBean包路径
    @ComponentScan(basePackages = "com.xxx.demo.configuration")
    public class TestConfiguration {
        public TestConfiguration() {
            System.out.println("TestConfiguration容器启动初始化。。。");
        }
    }
    

    @ComponentScan用于配置自动扫描包,等效于下面的Spring的xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"
    
        xmlns:context="[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)" xmlns:jdbc="[http://www.springframework.org/schema/jdbc](http://www.springframework.org/schema/jdbc)" 
    
        xmlns:jee="[http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee)" xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)"
    
        xmlns:util="[http://www.springframework.org/schema/util](http://www.springframework.org/schema/util)" xmlns:task="[http://www.springframework.org/schema/task](http://www.springframework.org/schema/task)" xsi:schemaLocation="
    
            [http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-4.0.xsd](http://www.springframework.org/schema/beans/spring-beans-4.0.xsd)
    
            [http://www.springframework.org/schema/context](http://www.springframework.org/schema/context) [http://www.springframework.org/schema/context/spring-context-4.0.xsd](http://www.springframework.org/schema/context/spring-context-4.0.xsd)
    
            [http://www.springframework.org/schema/jdbc](http://www.springframework.org/schema/jdbc) [http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd](http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd)
    
            [http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee) [http://www.springframework.org/schema/jee/spring-jee-4.0.xsd](http://www.springframework.org/schema/jee/spring-jee-4.0.xsd)
    
            [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-4.0.xsd](http://www.springframework.org/schema/tx/spring-tx-4.0.xsd)
    
            [http://www.springframework.org/schema/util](http://www.springframework.org/schema/util) [http://www.springframework.org/schema/util/spring-util-4.0.xsd](http://www.springframework.org/schema/util/spring-util-4.0.xsd)
    
            [http://www.springframework.org/schema/task](http://www.springframework.org/schema/task) [http://www.springframework.org/schema/task/spring-task-4.0.xsd](http://www.springframework.org/schema/task/spring-task-4.0.xsd)" default-lazy-init="false”>
    
    <context:component-scan base-package="com.xxx.demo"/>
    
    </beans>
    
    1. 使用@Import引入其他配置类
    @Configuration
    @ImportResource("classpath:applicationContext-configuration.xml")
    @Import(TestConfiguration.class)
    public class WebConfig {
    }
    

    通过上面这种方式可以在一个配置类中引入其他配置类或者配置文件


    总结

    简单介绍了几个配置类中常用注解的使用方法

    • 单独使用@Configuration注解在类上
    • @Configuration注解类+@Bean注解方法
    • @Configuration注解类+ @ComponentScan注解类
    • 使用@Import引入其他配置类

    相关文章

      网友评论

        本文标题:Spring中常用的@Configuration和其他配置类注解

        本文链接:https://www.haomeiwen.com/subject/kcdymctx.html