@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@SpringBootApplication 封装:@Configuration, @EnableAutoConfiguration,@ComponentScan annotations
@ComponentScan({"controller","service","repository","pojo"}) @EnableJpaRepositories("repository") @EntityScan("pojo") @EnableAutoConfiguration @Configuration public class Config { .... } |
condition annotations are used on @Configuration @Bean @Configuration @ConditionalOnClass and @ConditionalOnMissingClass @ConditionalOnProperty @ConditionalOnResource @Conditional |
@EnableAutoConfiguration @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = Config.class) //@PropertySource("classpath:application.properties") @TestPropertySource(locations ="classpath:application.properties") @ConfigurationProperties("spring.datasource") //@Import (Config.class) public class RepositoryTest { @Autowired Environment env; @Test void printEnv() { System.out.println(env.getProperty("spring.datasource.url")); System.out.println(env.getProperty("spring.datasource.driverClassName")); } } ------------------------------------------------------------------------- @PropertySource("classpath:db.properties") @PropertySource("classpath:server.properties") class SysCfg { @Value( "${spring.datasource.url}" ) private String jdbcUrl;
//with default value @Value( "${spring.datasource.driverClassName:com.mysql.jdbc.Driver}" ) private String jdbcDriver;
------------------------------------------------------------------------- @PropertySources({ @PropertySource("classpath:db.properties") @PropertySource("classpath:server.properties") }) class SysCfg { @Autowired Environment env;
|
application.properties is the default file under folder src/main/resources application-dev.properties |
@Component @Profile("dev") public class DevDatasourceConfig{} |
java -jar xxx.jar --spring.profiles.active=dev |
-Dspring.profiles.active=dev |
application.properties中定义: spring.profiles.active = dev |
@Autowired private ConfigurableEnvironment env; ... env.setActiveProfiles("dev"); |
servletContext.setInitParameter"spring.profiles.active", "dev"); |
web.xml file <context-param> |
@ActiveProfiles("dev") |
@Value("${spring.profiles.active:}") private String activeProfile; |
@RunWith(SpringRunner.class) @TestPropertySource("/foo.properties") public class FilePropertyInjectionUnitTest { @Value("${foo}") private String foo; @Test public void whenFilePropertyProvided_thenProperlyInjected() { assertThat(foo).isEqualTo("bar"); } } |
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = Application.class) @AutoConfigureMockMvc @TestPropertySource( locations = "classpath:application-dev.properties") public class ControllerTest { @Autowired private MockMvc mvc; @Autowired private EmployeeRepository repository; } |
@RunWith(SpringRunner.class) public class EmployeeServiceImplIntegrationTest { @TestConfiguration static class EmployeeServiceImplTestContextConfiguration { @Bean public EmployeeService employeeService() { return new EmployeeService() { // implement methods }; } } @Autowired private EmployeeService employeeService; } |
@TestConfiguration @Autowired // remaining class code |
@RunWith(SpringRunner.class) @TestConfiguration @Autowired @MockBean // write test cases here |
@RunWith(SpringRunner.class) @Autowired @Autowired // write test cases here } |
@RunWith(SpringRunner.class) @Autowired @MockBean // write test cases here |
links:
Properties with Spring and Spring Boot | Baeldung