没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
Spring Boot基础
本节目标
1.掌握Spring Boot核心注解 2.掌握Spring Boot配置
1. 核心注解
@SpringBootApplication
在入口类上标注该注解,让Spring Boot自动给程序进行必要的配置,这个配置等同于: @Configuration,
@EnableAutoConfiguration和@ComponentScan三个配置。
@EnableAutoConfiguration
Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置Spring应用。例如:如果
classpath下存在H2库,并且没有手动配置任何数据库连接beans,那么将自动配置一个内存型(in-memory)数
据库”。
可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动
配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁
用它们。
@Configuration
相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配
置主类——可以使用@ImportResource注解加载xml配置文件。
@PropertySource
@Configuration
@EnableAutoConfiguration
public class AppConfiguration {
@Bean(value = "hello")
public String helloString() {
return "hello";
}
@Bean(value = "welcome")
public String welcomeString() {
return "welcome";
}
@Bean(value = "properties")
public Properties properties() {
return new Properties();
}
}
如果需要有自定义的属性文件需要加载,可以使用该注解进行注入,并用@Value配合使用。
@ImportResource
用来加载xml配置文件。
@Bean 用@Bean标注方法等价于XML中配置的bean。
@Component
@PropertySource(value = {"classpath:appconfig.properties"})
public class AppConfig {
@Value("${app.config.host}")
private String host;
@Value("${app.config.port}")
private Integer port;
//此处忽略setter,getter方法
}
#src/main/resources/appconfig.properties
app.config.host=localhost
app.config.port=3306
@SpringBootApplication
//位于:src/main/resourcs/application-bean.xml
@ImportResource(locations = {"classpath:application-bean.xml"})
public class ExampleApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ExampleApplication.class)
.bannerMode(Banner.Mode.CONSOLE)
.run(args);
}
}
@Bean
public Executor executor() {
return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2,
new ThreadFactory() {
final AtomicInteger bizThreadIndex = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "Biz-Thread-" + bizThreadIndex.getAndIncrement());
}
});
}
Environment
org.springframework.core.env.Environment 环境类,Spring3.1以后开始引入。比如JDK环境,Servlet环
境,Spring环境等等;每个环境都有自己的配置数据,如System.getProperties()、System.getenv()等可以拿到
JDK环境数据;ServletContext.getInitParameter()可以拿到Servlet环境配置数据等等;也就是说Spring抽象了一
个Environment来表示环境配置。
在Spring Boot中使用直接用@Resource或@Autowired注入,即可获得系统配置文件application.properties/yml
的属性值,如果是自定义的配置文件,则需要预先通过@PropertySource等其他注解注入后,才能获取。获取通过
getProperty()方法获取。
2. 基本配置
2.1 入口类
良好的习惯,通常Spring Boot项目有一个名为 *Application的入口类,入口类里有 一个main方法,该main方法
其实就是一个标准的Java应用的入口方法。在main方法中使用SpringApplication.run(..)方法来启动Spring Boot应
用项目。
@Bean(initMethod = "init", destroyMethod = "destroy")
@Scope(scopeName = "prototype")
public ExampleBean exampleBean() {
ExampleBean exampleBean = new ExampleBean();
exampleBean.setName("jack");
return exampleBean;
}
@Autowired
private Environment environment;
@RequestMapping(value = "/environment", method = {RequestMethod.GET})
public Map<String, Object> environment() {
Map<String, Object> map = new HashMap<>();
map.put("app.config.host", environment.getProperty("app.config.host"));
map.put("java.home", environment.getProperty("java.home"));
map.put("user.dir", environment.getProperty("user.dir"));
return map;
}
剩余12页未读,继续阅读
林书尼
- 粉丝: 21
- 资源: 315
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0