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

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>dev</param-value>
</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");

}

}


 


@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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值