[toc]java
java -jar xxx.jar
就能够成功运行项目,<properties> <java.version>1.8</java.version> </properties>
<properties> <project.build.sourceEncoding>GBK</project.build.sourceEncoding> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-releasetrain</artifactId> <version>Fowler-SR2</version> <scope>import</scope> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
spring boot 的web应用开发必须使用spring-boot-starter-web,其默认嵌入的servlet容器是Tomcat。spring
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <dependencies> <!-- TOMCAT --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
嵌入的servlet容器版本在pom的如下父依赖项中定义,好比上面的version1.4.3引入了Tomcat版本8.5.6。 若是想改变tomcat版本,也能够更改pom.xml或application.properties文件中的属性进行修改apache
<properties> <tomcat.version>8.5.6</tomcat.version></properties> </properties>
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-el</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-websocket</artifactId> <version>${tomcat.version}</version> </dependency>
若是想使用其它servlet容器,则须要先移除tomcat容器编程
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
将默认的嵌入式容器tomcat切换至jetty设计模式
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
官网提出激活自动化装配须要将注解@EnableAutoConfiguration 和 @SpringBootApplicaion,将二者选其一标注在@Configuration类上,@Configuration声明被标注为配置类tomcat
@SpringBootApplicaion是一个聚合注解,相似的还有@RestController等 @SpringBootApplicaion被用于激活@EnableAutoConfiguration、@ComponentScan、@Configuration三个注解的特性,能够理解为前者等同包含于三个后者:springboot
其中@SpringBootConfiguration属于@Configuration的派生注解 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan public @interface SpringBootApplicaion{ ... }
@AlisaFor注解可以将一个或多个注解的属性‘别名’到某个注解中 @SpringBootApplicaion(scanBasePackages = 'com.song.xxx')websocket
在applicationContext.xml文件中加一行:<context:component-scan base-package="com.song.xxx"/>后 @Component、@Repository、@Service、@Controller都是将类实例化注入到spring管理器中,名字只是一 个分类,实质做用是同样的 @Repository用于标注数据访问组件,即DAO组件 @Component泛指组件,当组件很差归类的时候,咱们可使用这个注解进行标注。 @Service通常标注在业务接口实现类上,用于标注业务层组件 @Controller用于标注控制层组件 @Configuration标注在类上,至关于把该类做为spring的xml配置文件中的<beans>, 做用为:配置spring容器(应用上下文) 用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被 AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext 类进行扫描,并用于构建bean定义,初始化Spring容器
@ComponentScan:扫描知道package下标注Spring模式注解的类,若是不添加此注解,则其余的添加注解的类 不会被springboot扫描到,更不会装入spring容器中。
:) @Autowired默认按类型装配 :) @Autowired默认状况下必需要求依赖对象必须存在,若是要容许null值,能够设置 它的required属性为false,如:@Autowired(required=false) :) 若是接口有多个实现类,spring并不知道用哪一个实现类,这个时候能够结合@Qualifer注解, 注意@Qualifier注解括号里面的必须是Person接口实现类的类名:@Qualifier("StudentService") :) @Resource后面没有任何内容,默认经过name属性去匹配bean,找不到再按type去匹配 :) @Resource指定了name或者type则根据指定的类型去匹配bean: @Resource(name = "teacher") / @Resource(type = Student.class) :) @Resource属于java注解,@Autowired和@Qualifer属于spring注解,建议使用@Resource注解, 以减小代码和Spring之间的耦合。
能申明在其余注解上的注解,例如:@Documented、@Componentapp
@Component做为一种由spring容器托管的通用模式组件,任何被@Component标注的组件均为组件扫描的候选对象,相似地,凡是被@Component元标注的注解,如@Service所标注的任何组件,也被视做组件的候选对象。
例如:
@TransacrionlService组合了@Transacrion和@Service这两个注解 @SpringBootApplication既是模式注解,也是组合注解
较低层注解能覆盖其元注解的同名属性 @Component |-@Service |-@TransacrionlService 其中@TransacrionlService能够覆盖@Service @Service能够覆盖@Component