SpringBoot Series Spring container ways to add components

SpringBoot Series Spring container ways to add components

This blog describes SpringBoot project components will be added to a Spring container, SpringBoot project has an obvious advantage is no need to write xml configuration file, just use SpringBoot annotations can achieve similar functionality, but in fact SpringBoot project or support the introduction of xml configuration files, so this blog to introduce two ways

ok, tell us about @ImportResource comment SpringBoot project, the role of this comment is to introduce some xml resources, loaded into a Spring container

Build a class TestBean

public class TestService {
}

Create a new beans.xml, write a service of bean configuration

<?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 class can then be referenced directly, or you can load Configuration configuration class above

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 test class:

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);
    }

}

After testing, the return is true, ok, way to achieve change Springboot comment

Create a PropertiesConfig configuration class, attention: id is the method name components

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 testing continues:


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 test, or return TRUE, if under the changed name is as testService returns FALSE, methods, because the component name is the name of the corresponding notes @Bean

In fact, I used to write Spring project, it is clear that you can also use @Service @Controller comment or add components to the container, if you look at the source code to the point, in fact, have one thing in common these annotations have introduced @Component notes, and @Configuration comment this blog describes, in essence, is the introduction of @Component comment, but @Bean is not introduced, so the case if you just add @Bean, without adding @Configuration annotated, is not to add components to Spring container

Source Example: GitHub sample code download

Guess you like

Origin www.cnblogs.com/mzq123/p/11830470.html