Spring @ContextConfiguration comment

Original Address: https://www.cnblogs.com/bihanghang/p/10023759.html

@ContextConfigurationThis annotation generally @RunWith(SpringJUnit4ClassRunner.class)used to test the combined use of

When a class adds comment @Component, then he automatically becomes a bean, you do not need to show the Spring configuration file is configured. These bean collected usually in two ways, Java and XML way way. When these bean collected, when we want to use in a test class @Autowiredwhen introduced bean annotations to collect these up, just give this test class to add @ContextConfigurationannotations to mark the bean we want to import some of the test class.

XML

Let's look at the XML way for the elderly:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd  >

    <!-- 自动扫描该包 -->
    <context:component-scan base-package="com" />
</beans>

This XML file by <context:component-scan base-package="com" />label under the bean bag com automatically scans all incoming.

Here we will be able to test.

General write:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/*.xml"})
public class CDPlayerTest {

}

@ContextConfigurationBrackets locations = {"classpath*:/*.xml"}, says the class path in all the .xml files are included, so that XML file you just created will be included, it automatically scans the inside of the bean you can get, then you can test in class use @Autowiredautomatic scanning under the package before the annotation to get all bean

classpath和classpath*区别:

  • classpath: only to find your class path to find the file.

  • classpath *: includes not only the class path, also includes a jar file (class path) to find it.

Java

If you use the Java way will be very simple, we do not have to write XML documents so complicated, we can create a Java class instead of the XML file, only need to add in this class above @Configurationcomment, then add @ComponentScanannotations to open automatic scanning, if the notes did not write stuff inside the parentheses, the same @ ComponentScan default scan configuration class package.

@Configuration
@ComponentScan
public class CDPlayConfig {

}

At this point if you want to test it, you can write:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayConfig.class)
public class CDPlayerTest {

}

Compared to XML, it is not very cool, which is the official advocate of the way.

In Spring Boot test

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class Test {
}

This means that the annotation @SpringBootTest SpringBoot introduced into the main class bean all included.

At this time SpringBoot main class is also used as the collector bean. Similar CDPlayConfig above.

@SpringBootApplication
@SpringBootConfiguration
@ComponentScan(basePackages = {"com.bihang.*"})
public class CarOrderWebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(CarOrderWebApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(CarOrderWebApplication.class, args);
    }

}

 

Guess you like

Origin www.cnblogs.com/dyh004/p/11584129.html