spring-boot-starter 核心Spring Boot starter,包括自动配置支持,日志和YAML
spring-boot-starter-actuator 生产准备的特性,用于帮你监控和 管理应用spring-boot-starter-web 对全栈web开发的支持,包括Tomcat和 spring-webmvcspring-boot-starter-aop 对面向切面编程的支持,包括 spring-aop 和AspectJspring-boot-starter-jdbc 对JDBC数据库的支持spring-boot-starter-security 对 spring-security 的支持
Spring Boot默认将应用打包成一个可执行的jar包文件,构建成功后使用java -jar命令即可运行应用。
或者在应用项目的主程序中运行main函数即可,不需要依赖tomcat、jetty等外部的应用服务器。其中内置的servlet Container:此外,你仍然可以部署Spring Boot项目到任何兼容Servlet3.0+的容器。
开启devtools特性
devtools的热部署和自动重启要想在Eclipse中使用Devtools的重启功能,需要将自动编译功能打开。每次保存文件并自动编译后,devtools会检测到classpath内容的修改,并触发应用重启。重启时实际只重新加载了一部分类,因此速度会非常快。详细原理会在后面教程里介绍。devtools的livereload开启devtools特性的应用在启动时会启动一个livereload的server,浏览器(如chrome,Firefox)安装livereload插件后,该插件会监测到livereload server的更新,并自动刷新页面。 @SpringBootApplication申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置 @ResponseBody该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful的api,该注解一般会配合@RequestMapping一起使用。示例代码:@RequestMapping("/test") @ResponseBody public String test(){ return"ok"; }@Controller
用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解@RequestMapping。@RestController
@ResponseBody和@Controller的合集。@EnableAutoConfiguration
Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。 @ComponentScan 表示将该类自动发现(扫描)并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类。 @Configuration相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。@Configuration @EnableAutoConfiguration public class RedisConfig { @Bean(name="jedisPoolConfig") @ConfigurationProperties(prefix="spring.redis") public JedisPoolConfig getRedisConfig(){ return new JedisPoolConfig(); }}@PropertySource
如果需要有自定义的属性文件需要加载,可以使用该注解进行注入,并用@Value配合使用。@Component@PropertySource(value = "classpath:config.properties")public class ConfigUtil { @Value("${hos.id}") private String hosId; @Value("${hos.name}") private String hosName;}@ImportResource
用来加载xml配置文件。@Bean
用@Bean标注方法等价于XML中配置的bean。@Value
注入Spring boot application.properties配置的属性的值。@Value(value = "#{message}") private String message; Environment org.springframework.core.env.Environment,环境类,spring3.1以后开始引入。比如JDK环境,Servlet环境,Spring环境等等;每个环境都有自己的配置数据,如System.getProperties()、System.getenv()等可以拿到JDK环境数据;ServletContext.getInitParameter()可以拿到Servlet环境配置数据等等;也就是说Spring抽象了一个Environment来表示环境配置。 在springBoot中使用直接用@Resource注入,即可获得系统配置文件application.properties/yml的属性值,如果是自定义的配置文件,则需要预先通过@PropertySource等其他注解注入后,才能获取。获取通过getProperty()方法获取。 1.与MyBatis的集成 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>2、与Redis的集成 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>3、Junit进行单元测试 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> springBoot的发布1.将springBoot项目打包成jar可以使用maven将项目打包成jar文件,并使用java -jar命令运行主main方法,将项目运行起来。2.将springBoot项目打包成wara1.pom文件的命令将<packaging>jar</packaging>修改为war。a2.入口类实现SpringBootServletInitializer方法,重写方法: @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); }a3.这里指定打包的时候不再需要tomcat相关的包 <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> Summarize:1. SpringBoot让开发过程变得简单,开发过程中配置变得容易2. SpringBoot让部署过程变得简单3. SpringBoot让运维工作变得简单缺点:
-缺少注册、发现等外围方案-缺少外围监控集成方案-缺少外围安全管理方案-缺少REST落地的URI规划方案所以SpringBoot只是一个入门级的微框架
==========================================================================================
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency>
<groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>runtime</scope></dependency><dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId></dependency> @RequestMapping("/list") public ModelAndView PageList() { String sql = "select * from student"; List<Map<String, Object>> list = jdbcTemplate.queryForList(sql); ModelAndView mav = new ModelAndView("/WEB-INF/views/list.jsp"); mav.addObject("list", list); return mav; }nohup /opt/converse_jar_QA/bin/jdk1.8.0_131/bin/java -javaagent:/opt/jacoco/lib/jacocoagent.jar=destfile=/opt/converse_jar_QA/jacoco.exec,includes=com.acxiom.standard.controller -jar /opt/converse_jar_QA/bin/converse.ws.jar spring.config.location=/opt/converse_jar_QA/bin/application.properties 2>&1 &
application.yml 中 配置DB的Sample
注意缩进, 冒号之后的空格
spring:
datasource: name: test url: jdbc:sqlserver://xxxxxxxx:1433;databaseName=xxx username: xxxxx password: P3b#xxx* # 使用druid数据源 driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20