• SpringBoot系列之Spring容器添加组件方式


    SpringBoot系列之Spring容器添加组件方式

    本博客介绍SpringBoot项目中将组件添加到Spring容器中的方法,SpringBoot项目有一个很明显的优点,就是不需要再编写xml配置文件,只需要用SpringBoot的注解就可以实现类似功能,不过其实SpringBoot项目还是支持引入xml配置文件的,所以本博客介绍一下两种使用方式

    ok,介绍一下SpringBoot项目的@ImportResource注解,这个注解的作用就是引入一些xml资源,加载到Spring容器里

    建个TestBean类

    public class TestService {
    }
    

    新建一个beans.xml,写一个service的bean配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="testService" class="com.example.springboot.properties.service.TestService"></bean>
    </beans>
    

    然后可以Application类里直接引用,也可以加载Configuration配置类上面

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ImportResource;
    
    @SpringBootApplication
    @ImportResource(locations = {"classpath:beans.xml"})
    public class SpringbootPropertiesConfigApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SpringbootPropertiesConfigApplication.class, args);
    	}
    
    }
    
    

    Junit测试类:

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.ApplicationContext;
    
    @SpringBootTest
    class SpringbootPropertiesConfigApplicationTests {
    
    	//装载ioc容器
    	@Autowired
    	ApplicationContext ioc;
    
    	@Test
    	void contextLoads() {
    		//测试这个bean是否已经加载到Spring容器
    		boolean flag =  ioc.containsBean("testService");
    		System.out.println(flag);
    	}
    
    }
    

    经过测试,返回的是true,ok,换Springboot注解的方式实现

    新建一个PropertiesConfig配置类,注意:组件的id就是方法名

    import com.example.springboot.properties.service.TestService;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration //@Configuration注解实践上也是一个Component
    public class PerpertiesConfig {
    	//通过@Bean注解将组件添加到Spring容器,组件的id就是方法名
        @Bean
        public TestService testService1(){
            return new TestService();
        }
    }
    
    

    Junit测试继续:

    
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.ApplicationContext;
    
    @SpringBootTest
    class SpringbootPropertiesConfigApplicationTests {
    
    	@Autowired
    	ApplicationContext ioc;
    
    	@Test
    	void contextLoads() {
    		//传方法名testService1
    		boolean flag =  ioc.containsBean("testService1");
    		System.out.println(flag);
    	}
    
    }
    
    

    Junit测试,返回的还是TRUE,如果改下name为testService就是返回FALSE的,因为组件名称就是@Bean注解对应的方法名

    其实以前写Spring项目的时候,很显然也可以用@Service或者@Controller注解将组件添加到容器里,如果你去点一下源码,其实这些注解都有一个共同点就是都引入了@Component注解,而本博客介绍的@Configuration注解,本质上也是引入了@Component注解,而@Bean是没有引入的,所以,如果你只加@Bean,而不加@Configuration注解的情况,是不可以将组件添加到Spring容器的

    example source:github例子代码下载

  • 相关阅读:
    image 和 barplot 的组合
    par函数mgp 参数-控制坐标轴的位置
    R语言绘图时的边界碰撞问题
    R语言绘制花瓣图flower plot
    mothur 计算稀释性曲线
    R语言 vegan包计算物种累计曲线
    R语言数据框小技巧
    tophat-fusion 鉴定融合基因
    FusionCancer-人类癌症相关的融合基因的数据库
    rrnDB数据库简介-16S基因多拷贝数的证据
  • 原文地址:https://www.cnblogs.com/mzq123/p/11830470.html
Copyright © 2020-2023  润新知