目录
java -jar xxx.jar
就可以成功运行项目,https://start.spring.io/
<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。
<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文件中的属性进行修改
<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声明被标注为配置类
@SpringBootApplicaion是一个聚合注解,类似的还有@RestController等
@SpringBootApplicaion被用于激活@EnableAutoConfiguration、@ComponentScan、@Configuration三个注解的特性,可以理解为前者等同包含于三个后者:
其中@SpringBootConfiguration属于@Configuration的派生注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplicaion{
...
}
@AlisaFor注解能够将一个或多个注解的属性‘别名’到某个注解中
@SpringBootApplicaion(scanBasePackages = ‘com.song.xxx‘)
在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、@Component
@Component作为一种由spring容器托管的通用模式组件,任何被@Component标注的组件均为组件扫描的候选对象,类似地,凡是被@Component元标注的注解,如@Service所标注的任何组件,也被视作组件的候选对象。
例如:
@TransacrionlService组合了@Transacrion和@Service这两个注解
@SpringBootApplication既是模式注解,也是组合注解
较低层注解能覆盖其元注解的同名属性
@Component
|-@Service
|-@TransacrionlService
其中@TransacrionlService可以覆盖@Service
@Service可以覆盖@Component
原文:https://www.cnblogs.com/songzhen/p/11484036.html