• 使用@Configuration编写自定义配置类


    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>www.tszr</groupId>
        <artifactId>chapter021</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    package com.itheima.dao;
    
    public class MyService {
    
    }
    package com.itheima.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.itheima.dao.MyService;
    
    @Configuration
    public class MyConfig {
        
        @Bean
        public MyService myService() {
            return new MyService();
        }
    }
    package com.itheima;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    //import org.springframework.context.annotation.ImportResource;
    
    @SpringBootApplication
    //@ImportResource("classpath:beans.xml")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    package com.itheima;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.ApplicationContext;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import com.itheima.domain.Person;
    import com.itheima.config.MyPropertiesConfig;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class ApplicationTest {
        
        @Autowired
        private ApplicationContext applicationContext;
        
        @Test
        public void ioTest() {
            System.out.println("spring容器是否包含了我用Configuration和Bean设置的配置对象呢?");
            System.out.println(applicationContext.containsBean("myService"));
        }
    }

  • 相关阅读:
    2021.07.13 学习总结
    2021.07.12 学习总结
    2021.07.11 学习总结
    form表单提交失败
    url传参和解决中文乱码
    layui日期选择无效的问题
    Eclipse中快速生成Javabean的方法
    一种控制台输出效果的方法
    暑期周总结
    环境配置2
  • 原文地址:https://www.cnblogs.com/tszr/p/15907312.html
Copyright © 2020-2023  润新知