Springboot Annotation

@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(DataSource.class)
class DataSourceCfg {
}

@ConditionalOnClass and @ConditionalOnMissingClass
@ConditionalOnBean and @ConditionalOnMissingBean

 @ConditionalOnProperty

@ConditionalOnResource
@ConditionalOnWebApplication and @ConditionalOnNotWebApplication
@ConditionalExpression

@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
application-uat.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


    spring.profiles.active
    dev

@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");

}

}


 


@RunWith(SpringRunner.class)

@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
public class EmployeeServiceImplTestContextConfiguration {
    
    @Bean
    public EmployeeService employeeService() {
        return new EmployeeService() { 
            // implement methods 
        };
    }
}
@RunWith(SpringRunner.class)
@Import(EmployeeServiceImplTestContextConfiguration.class)
public class EmployeeServiceImplIntegrationTest {

    @Autowired
    private EmployeeService employeeService;

    // remaining class code
}

@RunWith(SpringRunner.class)
public class EmployeeServiceImplIntegrationTest {

    @TestConfiguration
    static class EmployeeServiceImplTestContextConfiguration {
 
        @Bean
        public EmployeeService employeeService() {
            return new EmployeeServiceImpl();
        }
    }

    @Autowired
    private EmployeeService employeeService;

    @MockBean
    private EmployeeRepository employeeRepository;

    // write test cases here
}

@RunWith(SpringRunner.class)
@DataJpaTest
public class EmployeeRepositoryIntegrationTest {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private EmployeeRepository employeeRepository;

    // write test cases here

}

@RunWith(SpringRunner.class)
@WebMvcTest(EmployeeRestController.class)
public class EmployeeRestControllerIntegrationTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private EmployeeService service;

    // write test cases here
}

 links:

 Properties with Spring and Spring Boot | Baeldung

Spring Boot Tutorial - Learn Spring Boot

Spring Boot Reference Documentation

你可能感兴趣的:(spring,boot,java,后端)