@SpringBootApplication
@EnableAutoConfiguration
@Configuration
@ImportResource(locations = {"classpath:applicationContext.xml", "classpath:${dubbo.consumer.xml}", "classpath:${dubbo.provider.xml}"})
@ComponentScan(basePackages = {"com.oppo.bot.common","com.oppo.bot.common.redis","com.oppo.bot.adapter.baidu","com.oppo.bot.adapter.music"})
@MapperScan(basePackages = { "com.oppo.bot.nlu.dal.mapper","com.oppo.bot.common.dal.mapper" })
@EnableScheduling
public class Application {
public static void main(String ... args){
SpringApplication springApplication = new SpringApplication(BaiduMainApplication.class);
springApplication.addListeners(new ApplicationInit());
springApplication.run(args);
}
其中@ImportResource为加载的配置文件路径${dubbo.consumer.xml}为在配置文件中找dubbo.consumer.xml参数的值进行引用
@ComponentScan为扫描的@component和@service和@controller等的spring对象路径,默认为与Application在同一个目录下的路径
@MapperScan为mybatis配置文件XML中的Mapper对象的路径
@EnableAutoConfiguration帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。
@EnableScheduling 开启对定时任务的支持,在需要使用定时任务的方法上添加 @Scheduled(fixedRateString = "2000",initialDelay = 1000)
例如:
@Service
public class TestService2 {
private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
//初始延迟1秒,每隔2秒
@Scheduled(fixedRateString = "2000",initialDelay = 1000)
public void testFixedRate(){
System.out.println("fixedRateString,当前时间:" +format.format(new Date()));
}
//每次执行完延迟2秒
@Scheduled(fixedDelayString= "2000")
public void testFixedDelay(){
System.out.println("fixedDelayString,当前时间:" +format.format(new Date()));
}
//每隔3秒执行一次
@Scheduled(cron="0/3 * * * * ?")
public void testCron(){
System.out.println("cron,当前时间:" +format.format(new Date()));
}
}
@Configuration中可以通过@Bean注入其他对象
@Configuration
public class ComponentConfig {
@Bean
public Person getPerson() {
return new Person("百度好帅", 10000);
}
}
在spring-boot中,
1、如果@ConfigurationProperties所注的类可以被springboot扫描并添加进容器中作为bean(比如使用@Component等注解,或者配置扫描该类所在包等手段),那么spring容器会自动使该类上的@ConfigurationProperties生效,创建一个该类的实例,然后把对应配置属性绑定进该实例,再把该实例作为bean添加进spring容器。
2、如果该类只使用了@ConfigurationProperties注解,然后该类没有在扫描路径下或者没有使用@Component等注解,导致无法被扫描为bean,那么就必须在配置类上使用@EnableConfigurationProperties注解去指定这个类,这个时候就会让该类上的@ConfigurationProperties生效,然后作为bean添加进spring容器中
3.在javaconfig中@ConfigurationProperties与@Bean一起用,也是把配置文件中的属性注入该@Bean对应的要添加到容器中的bean实例中。