springboot2.x(一)springboot基础及搭建

一、springboot的价值

我们在springboot之前最典型的就是ssm架构,spring生态最大的价值是能搭建J2EE生态,其强大的生命力也带来很多负面影响,(1)大量的xml配置文件让代码的可读性变差 (2)大量的架构集成需要架构师去做很多封装,

受新一代开发语言影响,springboot便应用而是:

(1)注解大于配置,大量使用代码注解来提到XML配置文件,降低代码量

(2)约定大于配置,用约定俗成的规范及命名规则代替配置,实现了简化目的

(3)高度集成,通过pom集成常用的开发基础架构,并提供封装类,降低架构师工作量,统一打成jar,便于部署

二、springboot2框架搭建

(1)springboot导航

 

(1)idea可用springboot自带的导航,但要链接到外网速度慢,可直接搭建简单的的maven框架,然后修改pom.xml文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.imooc</groupId>
  <artifactId>girl</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>girl</name>
  <description>Demo project for Spring Boot</description>

  <!--依赖于spring-boot-starter-parent 包-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <!--spring-boot-starter-web默认为我们提供一些SpringMVC必要的组件-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

  



  </dependencies>

 <!--阿里镜像-->
  <repositories>
    <repository>
      <id>nexus-aliyun</id>
      <name>nexus-aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>


  <build>
    <!--打jar包名-->
    <finalName>sbdemo</finalName>
    <plugins>
      <!--spring boot maven插件-->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      
      </plugin>
    </plugins>
  </build>
</project>

三、运行

1.启动类入口

/**
 * @class: com.jyj.soft.GirlApplication
 * @description:
 * @author: jiangzengkui
 * @company: 教育家
 * @create: 2020-12-02 17:38
 */

//启动入口注解
@SpringBootApplication
public class GirlApplication {

    public static void main(String[] args) {
        //应用启动
        SpringApplication.run(GirlApplication.class, args);
    }
}

 2.测试类

//mvc注解
@RestController
public class HelloCtrol {

   //访问路径及方法
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
       return "hello, jzk";
    }
}

3.运行

(1)在编辑器里直接运行GirlApplication类

(2)在pom.xml为当前路径,在dos里运行 mvn spring-boot:run

  (3)  用 mvn install 打包生成jar,然后运行jar   java -jar xxx.jar

浏览器里输入:

http://localhost:8080/hello

四.springboot pom.xml文件详解

注意:在idea里面按住ctrl+鼠标,点击gav相关配置,可进入这个引用对用的pom.xml文件

(1)父类继承,获得依赖版本号的管理
 
在sb的pom.xml  spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-parent </artifactId>
<version>2.2.4.RELEASE </version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
spring-boot-starter-parent里的父亲
<parent>
<groupId>org.springframework.boot </groupId>
<artifactId> spring-boot-dependencies </artifactId>
<version>2.2.4.RELEASE </version>
<relativePath>../../spring-boot-dependencies </relativePath>
</parent>
spring-boot-dependencies核心控制这个sb版本要依赖的其他jar的版本号
<properties>
<activemq.version>5.15.11</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.77</appengine-sdk.version>
<artemis.version>2.10.1</artemis.version>
<aspectj.version>1.9.5</aspectj.version>
<assertj.version>3.13.2</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<awaitility.version>4.0.2</awaitility.version>
<bitronix.version>2.1.4</bitronix.version>
。。。。。
</properties>
 
(2)web场景启动器
 
 
在pom.xml配置web启动器
< dependency>
< groupId>org.springframework.boot</ groupId>
< artifactId>spring-boot-starter-web</ artifactId>
</ dependency>
打开spring-boot-starter-web.pom发现其已经引入了
json tomcat web-mvc等其他包
< dependencies>
< dependency>
< groupId>org.springframework.boot</ groupId>
< artifactId>spring-boot-starter</ artifactId>
< version>2.2.4.RELEASE</ version>
< scope>compile</ scope>
</ dependency>
< dependency>
< groupId>org.springframework.boot</ groupId>
< artifactId>spring-boot-starter-json</ artifactId>
< version>2.2.4.RELEASE</ version>
< scope>compile</ scope>
</ dependency>
< dependency>
< groupId>org.springframework.boot</ groupId>
< artifactId>spring-boot-starter-tomcat</ artifactId>
< version>2.2.4.RELEASE</ version>
< scope>compile</ scope>
</ dependency>
< dependency>
< groupId>org.springframework.boot</ groupId>
< artifactId>spring-boot-starter-validation</ artifactId>
< version>2.2.4.RELEASE</ version>
< scope>compile</ scope>
< exclusions>
< exclusion>
< artifactId>tomcat-embed-el</ artifactId>
< groupId>org.apache.tomcat.embed</ groupId>
</ exclusion>
</ exclusions>
</ dependency>
< dependency>
< groupId>org.springframework</ groupId>
< artifactId>spring-web</ artifactId>
< version>5.2.3.RELEASE</ version>
< scope>compile</ scope>
</ dependency>
< dependency>
< groupId>org.springframework</ groupId>
< artifactId>spring-webmvc</ artifactId>
< version>5.2.3.RELEASE</ version>
< scope>compile</ scope>
</ dependency>
场景说明
springboot把各种应用组合成一个个应用场景-start,只需要选择一个场景,则sb就会把这个场景里所有依赖的第三方框架和 包都引入
例如spring-boot-starter-web场景,
其会引入tomcat json mvc等各种第三方依赖
 
 
所有的聚合场景
ame
Description
Pom
spring-boot-starter
Core starter, including auto-configuration support, logging and YAML
Pom
spring-boot-starter-activemq
Starter for JMS messaging using Apache ActiveMQ
Pom
spring-boot-starter-amqp
Starter for using Spring AMQP and Rabbit MQ
Pom
spring-boot-starter-aop
Starter for aspect-oriented programming with Spring AOP and AspectJ
Pom
spring-boot-starter-artemis
Starter for JMS messaging using Apache Artemis
Pom
spring-boot-starter-batch
Starter for using Spring Batch
Pom
spring-boot-starter-cache
Starter for using Spring Framework’s caching support
Pom
spring-boot-starter-cloud-connectors
Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku. Deprecated in favor of Java CFEnv
Pom
spring-boot-starter-data-cassandra
Starter for using Cassandra distributed database and Spring Data Cassandra
Pom
spring-boot-starter-data-cassandra-reactive
Starter for using Cassandra distributed database and Spring Data Cassandra Reactive
Pom
spring-boot-starter-data-couchbase
Starter for using Couchbase document-oriented database and Spring Data Couchbase
Pom
spring-boot-starter-data-couchbase-reactive
Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive
Pom
spring-boot-starter-data-elasticsearch
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch
Pom
spring-boot-starter-data-jdbc
Starter for using Spring Data JDBC
Pom
spring-boot-starter-data-jpa
Starter for using Spring Data JPA with Hibernate
Pom
spring-boot-starter-data-ldap
Starter for using Spring Data LDAP
Pom
spring-boot-starter-data-mongodb
Starter for using MongoDB document-oriented database and Spring Data MongoDB
Pom
spring-boot-starter-data-mongodb-reactive
Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive
Pom
spring-boot-starter-data-neo4j
Starter for using Neo4j graph database and Spring Data Neo4j
Pom
spring-boot-starter-data-redis
Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client
Pom
spring-boot-starter-data-redis-reactive
Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client
Pom
spring-boot-starter-data-rest
Starter for exposing Spring Data repositories over REST using Spring Data REST
Pom
spring-boot-starter-data-solr
Starter for using the Apache Solr search platform with Spring Data Solr
Pom
spring-boot-starter-freemarker
Starter for building MVC web applications using FreeMarker views
Pom
spring-boot-starter-groovy-templates
Starter for building MVC web applications using Groovy Templates views
Pom
spring-boot-starter-hateoas
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS
Pom
spring-boot-starter-integration
Starter for using Spring Integration
Pom
spring-boot-starter-jdbc
Starter for using JDBC with the HikariCP connection pool
Pom
spring-boot-starter-jersey
Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to  spring-boot-starter-web
Pom
spring-boot-starter-jooq
Starter for using jOOQ to access SQL databases. An alternative to  spring-boot-starter-data-jpa  or  spring-boot-starter-jdbc
Pom
spring-boot-starter-json
Starter for reading and writing json
Pom
spring-boot-starter-jta-atomikos
Starter for JTA transactions using Atomikos
Pom
spring-boot-starter-jta-bitronix
Starter for JTA transactions using Bitronix
Pom
spring-boot-starter-mail
Starter for using Java Mail and Spring Framework’s email sending support
Pom
spring-boot-starter-mustache
Starter for building web applications using Mustache views
Pom
spring-boot-starter-oauth2-client
Starter for using Spring Security’s OAuth2/OpenID Connect client features
Pom
spring-boot-starter-oauth2-resource-server
Starter for using Spring Security’s OAuth2 resource server features
Pom
spring-boot-starter-quartz
Starter for using the Quartz scheduler
Pom
spring-boot-starter-rsocket
Starter for building RSocket clients and servers.
Pom
spring-boot-starter-security
Starter for using Spring Security
Pom
spring-boot-starter-test
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito
Pom
spring-boot-starter-thymeleaf
Starter for building MVC web applications using Thymeleaf views
Pom
spring-boot-starter-validation
Starter for using Java Bean Validation with Hibernate Validator
Pom
spring-boot-starter-web
Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container
Pom
spring-boot-starter-web-services
Starter for using Spring Web Services
Pom
spring-boot-starter-webflux
Starter for building WebFlux applications using Spring Framework’s Reactive Web support
Pom
spring-boot-starter-websocket
Starter for building WebSocket applications using Spring Framework’s WebSocket support
Pom
 
(3)核心注解
 
SpringBootApplication:实现了配置的主动加载功能
@SpringBootApplication
SpingBoot应用程序启动配置,里面有两个核心的配置
@SpringBootConfiguration
@EnableAutoConfiguration
@SpringBootConfiguration
配置注解类,里面有包含 @Configuration
其实就是替换掉springcfg.xml等大量配置文件的
 
@Configuration
标注起 @Configuration表示本类为配置类,加了@Configuration
表示把配置类纳入sping容器里
@EnableAutoConfiguration
自动配置类,我们没配置有spring的大量配置,为什么程序能运行,
是因为这个配置类把自动装置配置:约定优于配置
里面包含:
 
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector. class})
@AutoConfigurationPackage
自动扫描包
可找到@SpringBootConfiguration注解类所在的包,可将这个包及其子包所有类都加入到spring容器
 
传统的写法是手动扫描到scan到spring容器中
@Import({AutoConfigurationImportSelector. class})
引入第三方依赖(jar,配置)
springboot启动时,能查找spring-boot-autoconfigure-2.2.4.RELEASE.jar/META-INF/spring.factories里的第三方引用包
 
总结
编写项目是,一般会对自己些的代码和引入第三方的进行配置,springboot
将自动帮您配置
(1)自己写的代码, @AutoConfigurationPackage
(2)第三方依赖, 能查找spring-boot-autoconfigure-2.2.4.RELEASE.jar/META-INF/spring.factories里的第三方引用包,
 
spring-boot-autoconfigure-2.2.4.RELEASE.jar整合了j2ee需要的各种应用
 
 
(4).属性文件的配置读取
 
通过自动装载,springboot内置了很多默认的配置(约定大于配置),但这些情况都可以通过配置文件修改
application.properties、  application.yml都是可以维护数据配置
 
yml文件格式
 
server:
  port: 8082
  servlet:
    path: /girle
jiba: long
 
person:
     userName: jzk
     sex: 1
     height: 40
    weigth: 80
    address:  星湖大道
   desc:  ${person.userName}住在${person.address}   //属性文件的值可以相互引用
yml文件规则 : 
  1. :后面必须是空格加值
  2.  换行缩进一空格表示是分组,相当于一个类的属性
  3. 第一行必须顶格,不能加空格
 
person:
     userName: jzk
 等价于properties 属性文件的person.userName=蒋增奎
java读取属性方法
 
  (1)在类的直接用属性读取
     @Value("${person.jiba}")
    private String jiba;
 
(2)读取到属性bean里面去
 
//注解
@Component
@ConfigurationProperties(prefix = "person") //对应分组名
public class Person {
    private String userName;
    private Integer sex;
    private Integer height;
    private Integer weigth;
    private String address;
    private String desc;
  .......
}
 
 
//其他类里引用
  @Autowired
    private Person person;
 
(3)技巧,属性文件用于不同场景
  三个文件,注意默认名人,application.yml,其他的是application+中划线+名称

  

首先执行application.yml,其文件内容
spring:
  profiles:
    active: dev
意思是执行application-dev.yml文件,如改成prod,则执行prod文件 (约定大于配置)
 
//获得在启动时,带启动参数,则可以动态执行哪个文件
 
D:\java\idea_iu\project\target>java -jar girl-0.0.1-SNAPSHOT.jar   --spring.profiles.active=prod
 
注意:properties和yml文件可以共存,相互补充,并可相互调用
 
外部配置文件的读取
把一个属性文件application.properties放到c盘,
在启动时带上参数: --spring.config.location=C:/application.properties
eg:
java -jar sb-demo-0.0.1-SNAPSHOT.jar  --spring.config.location=C:/application.properties
外部配置文件优先级大于内部配置文件
 
外部配置文件的作用:修改一些配置,又不想重新打包发布
 
外部修改少量参数
直接带上参数即可
eg:java -jar sb-demo-0.0.1-SNAPSHOT.jar   --server.port=8888
 
优先级:外部大于内部,properties大于yml
 
 
(5).手动装置类
 
springboot自动装载各种配置文件,如果我们自己手写的配置文件,sb不会加载,必须
方法1:配置文件
在主类入口增加 @ImportResource注解
 
 
配置文件spring.xml
<? xml version ="1.0" encoding ="UTF-8" ?>
< bean id ="UserService" class ="com.jyj.sbdemo.service.UserService"></ bean>
</ beans>
主入口类用
ImportResource
注解
@SpringBootApplication
@ImportResource(locations = { "classpath:spring.xml" })
public class SbDemoApplication {
 
public static void main(String[] args) {
SpringApplication. run(SbDemoApplication. class, args);
}
 
}
运用
@Autowired
ApplicationContext cont;
@Test
public void t1(){
UserService mg=(UserService) cont.getBean( "UserService");
System. out.println(mg+ "====");
}
 
 
 
方法2:通过注解的方式来配置
 
springboot不推荐使用配置文件来配置,推荐用注解类
通过 @Configuration,@Bean注解的配置类来实现
 
配置类
@Configuration
public class AppConfig {
@Bean
public UserService myUserServer(){
//在启动的时候就会执行这个方法
UserService mg= new UserService();
return mg;
}
}
注意:方法名myUserServer就是bean的id名
等价于xml配置
< bean id =" myUserServer " class ="com.jyj.sbdemo.service.UserService"></ bean>
运用
@Autowired
UserService  mg ;
@Test
public void  t1(){
System. out .println( mg + "====" );
}
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值