SpringBoot的入门(一)

简介: SpringBoot的入门(一)

1:SpringBoot的简介:

SpringBoot是由Spring4.0版本出来以后诞生出来一个新的框架,它崇尚的是一个简易的配置和零配置的一种做法,比如刚开始学习Spring的时候需要做很多很多的配置,而用SpringBoot的话这些的配置就不需要我们自己去做,它的内部给我们都做好了,归根揭底它用的就是注解,把Spring5提出的新特征和java提出的注解的方式把它用的非常的棒,做了很多的解析器对它来进行处理,全部都是用注解来做的,所以在开发中都是用一个@+一个注解就可以把所有要管理的对象纳入到Spring容器中去管理。SpringBoot是依赖于Spring4.X版本以上来研发的。

1.1:快速的构建SpringBoot项目:

注意:

在我们创建maven项目或者把项目从一个远程的仓库里面迁到自己的机器上,有时会报错的情况下,归根揭底:1:可能环境不匹配。2:依赖不存在。就是说很多的时候会自己把一些依赖安装到本地仓库里面,但是想要在本地中 运行的话还需要自己手动安装一遍。把本地的jar文件安装到本地的仓库里面去。

如果这里不改1.8的话默认是1.5,用一些的高级特征是用不了的。

  1.  <properties>
  2.    <!--源文件的编码格式-->
  3.    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4.    <!--编译源文件的jdk的版本-->
  5.    <maven.compiler.source>1.8</maven.compiler.source>
  6.    <!--编译后目标文件的jdk的版本-->
  7.    <maven.compiler.target>1.8</maven.compiler.target>
  8.  </properties>

1:引入springBoot的依赖:

第一种引入方式:引入SpringBoot项目的starter。

  1. <!--引入boot项目的starter-->
  2.    <dependency>
  3.      <groupId>org.springframework.boot</groupId>
  4.      <artifactId>spring-boot-starter-web</artifactId>
  5.      <version>2.0.5.RELEASE</version>
  6.    </dependency>

第二种引入方式:采用父模块来引入:

  1. <parent>
  2.    <groupId>org.springframework.boot</groupId>
  3.    <artifactId>spring-boot-starter-parent</artifactId>
  4.    <version>2.0.5.RELEASE</version>
  5.  </parent>
  6.    <dependency>
  7.      <groupId>org.springframework.boot</groupId>
  8.      <artifactId>spring-boot-starter-web</artifactId>
  9.    </dependency>

2:构建一个SpringBoot项目的启动类:

@SpringBootApplication:是一个复合的注解:@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan

@SpringBootConfiguration:是一个@Configuration注解,这个注解是由Spring 框架提供的,把配置的东西配置到Spring容器中去。@ComponentScan:扫描当前包,及其下面子包的标注着Spring能识别的的类上面注解的,类将会纳入到Spring容器中去管理。统统来源于Spring4.X以上的版本来演变的。

@EnableAutoConfiguration:这个注解是自动装配的意思,下次写的时候会写这个特性。

  1. package com.weizhaoyang.boot;

  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. /**
  5. * 快速构建一个SpringBoot的项目
  6. * 1:@SpringBootApplication:代表SpringBoot的启动类
  7. * 2:提供应用程序的入口去执行
  8. * run方法是静态方法,里面的第一个参数是主要的源,第二个参数是一个可变的参数
  9. */
  10. @SpringBootApplication
  11. public class App {
  12.    public static void main( String[] args ) {
  13.        SpringApplication.run(App.class,args);
  14.    }
  15. }

在源代码中:Class primarySource:这个参数是主要加载的类,这个方法返回的结果就是Spring容器对象:ApplicationContext。

  1.  public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
  2.        return run(new Class[]{primarySource}, args);
  3.    }

拿到PrimarySource这个类的主要作用是什么呢?在run里面还有一个静态方法:run。这个方法相当于创建了一个SpringApplication的对象

  1. public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
  2.        return (new SpringApplication(primarySources)).run(args);
  3.    }

点击SpringApplication再往下面走:这里是调用自己本身的构造函数来对这个资源进行装载:ResourceLoader是一个资源装载器

  1.    public SpringApplication(Class<?>... primarySources) {
  2.        this((ResourceLoader)null, primarySources);
  3.    }
  4.     public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
  5.        this.sources = new LinkedHashSet();
  6.        this.bannerMode = Mode.CONSOLE;
  7.        this.logStartupInfo = true;
  8.        this.addCommandLineProperties = true;
  9.        this.headless = true;
  10.        this.registerShutdownHook = true;
  11.        this.additionalProfiles = new HashSet();
  12.        this.isCustomEnvironment = false;
  13.        this.resourceLoader = resourceLoader;
  14.        Assert.notNull(primarySources, "PrimarySources must not be null");
  15.        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
  16.        this.webApplicationType = this.deduceWebApplicationType();
  17.        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
  18.        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
  19.        this.mainApplicationClass = this.deduceMainApplicationClass();
  20.    }

而在资源装载器的接口里面:有一个getResource方法:

  1.        Resource getResource(String var1);

Resource接口里面又继承了InputStreamSource接口。

  1.  public interface Resource extends InputStreamSource

在InputStreamSource的接口里面又提供了:getInputStream方法。

  1.  InputStream getInputStream() throws IOException;

上面的就是Spring的源码。

SpringBoot其实还是用ResourceLoader来把当前的传过来的对象转成流,转成资源来进行处理,这个资源必须是标注着@SpringBootApplication的类才可以的。

如果把这个注解去掉的话:无法去驱动一个Servlet的Web的应用程序。启动不了一个web Server

因为这个注解标上以后相当于通过run方法去驱动servlet容器,从而让内置的tomcat去把Servlet的应用程序给发布出去。

注意:1:@SpringBootApplication这个注解不一定标在App类上,可以标在任何的类上它都可以的。

这样写仍然可以驱动应用程序的。

多个类标上这个注解也是可以同时驱动的。例如:这个就是加载多个源

  1. package com.weizhaoyang.boot;

  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. import java.util.Arrays;

  5. /**
  6. * 快速构建一个SpringBoot的项目
  7. * 1:@SpringBootApplication:代表SpringBoot的启动类
  8. * 2:提供应用程序的入口去执行
  9. * run方法是静态方法,里面的第一个参数是主要的源,第二个参数是一个可变的参数
  10. */
  11. //@SpringBootApplication
  12. public class App {
  13.    public static void main( String[] args ) {
  14.        SpringApplication application = new SpringApplication();
  15.        application.addPrimarySources(Arrays.asList(Application.class,Application1.class));
  16.        application.run(args);
  17.    }
  18. }

运行的结果如下:这样也是可以驱动的,这就是多个Spring的入口。但是在SpringBoot中是不建议这么做的,一般在多环境的在做测试的时候这么做的。

3:SpringBoot的思想的来源

3.1:引入Spring的依赖:

  1.    <dependency>
  2.      <groupId>org.springframework</groupId>
  3.      <artifactId>spring-context</artifactId>
  4.      <version>4.3.14.RELEASE</version>
  5.    </dependency>

3.2:Spring4以前是要写一些配置文件,而在Spring4以后就不用写配置文件了,原有的方式想要把一个类交给Spring容器管理的话:加上一个@Component注解,代码如下:

  1. package com.weizhaoyang.spring;

  2. import org.springframework.stereotype.Component;

  3. @Component
  4. public class User {
  5. }



  1. import org.springframework.context.annotation.AnnotationConfigApplicationContext;

  2. /**
  3. * Hello world!
  4. *
  5. */
  6. public class App {
  7.    public static void main( String[] args ) {
  8.        //创建上下文对象
  9.        AnnotationConfigApplicationContext context =
  10.                new AnnotationConfigApplicationContext("com.weizhaoyang.spring");
  11.        //获取User类的实例
  12.        User user = context.getBean(User.class);
  13.        System.out.println(user);
  14.        context.close();
  15.    }
  16. }

运行的结果如下:这就得到了User对象的实例,所以这个类就装载到Spring容器里面去了。


但是如何去做到像SpringBoot那样玩呢?步骤如下:

1:创建UserConfig的类:一但类上标注了@Configuration的类将会被Spring容器管理起来。因为@Configuration=+@Target({ElementType.TYPE}) +@Target({ElementType.TYPE})+@Retention(RetentionPolicy.RUNTIME) +@Documented+@Component

  1. package com.weizhaoyang.spring;

  2. import org.springframework.context.annotation.Configuration;

  3. @Configuration
  4. public class UserConfig {
  5. }

3.3 上面的User类不想要用@Component来装载,想要用这个配置对象来装载:代码如下:在Spring容器中对象都是单例的。

  1. package com.weizhaoyang.spring;

  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;

  4. @Configuration
  5. public class UserConfig {
  6.    @Bean
  7.    public  User createUser(){
  8.        return  new User();
  9.    }
  10. }

运行结果如下:

但是一旦在User类上标上了@Component注解的话,在运行的话:会报错,这个名字不是唯一的,发现了两个。解释:因为@Component的注解或者@Service或者@Controller标着的注解的类它们默认的别名是当前类首字母小写。



如果按照名称去装配的话:

  1.  User user = context.getBean("user",User.class);

在配置类中,如果把方法返回的类类型装配到Spring容器中的话,别名应为方法名。

  1.     User user1 = context.getBean("createUser",User.class);

运行的结果如下:user和user1不是同一个对象,因为他们的别名不一样。

Spring4里面的单例怎么做的呢?这时在Bean的后面加上user,代码如下:

  1. package com.weizhaoyang.spring;

  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;

  4. @Configuration
  5. public class UserConfig {
  6.    @Bean("user")
  7.    public  User createUser(){
  8.        return  new User();
  9.    }
  10. }
  11. package com.weizhaoyang.spring;

  12. import org.springframework.context.annotation.AnnotationConfigApplicationContext;

  13. /**
  14. * Hello world!
  15. *
  16. */
  17. public class App {
  18.    public static void main( String[] args ) {
  19.        //创建上下文对象
  20.        AnnotationConfigApplicationContext context =
  21.                new AnnotationConfigApplicationContext("com.weizhaoyang.spring");
  22.        //获取User类的实例
  23.        User user = context.getBean("user",User.class);
  24.        User user1 = context.getBean("user",User.class);
  25.        System.out.println(user+","+user1);
  26.        context.close();
  27.    }
  28. }

运行的结果如下:这时拿到的是同一个对象。在Spring中的单例是通过Key来进行覆盖的,Key相同就认为是同一个对象。


通过Spring容器也可以拿到UserConfig类的实例,代码如下:

  1. System.out.println(context.getBean(UserConfig.class));

运行的结果如下:由于UserConfig是一个普通的类,并没有实现接口,所以这里生成了EnhancerBySpringCGLIB的代理,去生成一个代理类,通过代理类动态的去调用createUser方法去创建User对象的实例。

综上所述:不管怎么变,SpringBoot中只是把@Configuration做了一次封装而已。

4:在SpringBoot中如何去读取配置文件,SpringBoot中有属性的配置文件和结构化的配置文件两种。首先在属性的配置文件中写上相应的配置信息 例如:

  1. local.ip.addr =192.168.1.110

现在想把上面的配置信息给读取出来:1):通过Enviroment获取 在主函数run的时候先得到得是监听器,创建了环境对象,这里给自动得创建了一个环境对象Enviroment这个接口得实现类会放到Spring容器中去管理。

  1.  public ConfigurableApplicationContext run(String... args) {
  2.        StopWatch stopWatch = new StopWatch();
  3.        stopWatch.start();
  4.        ConfigurableApplicationContext context = null;
  5.        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
  6.        this.configureHeadlessProperty();
  7.        SpringApplicationRunListeners listeners = this.getRunListeners(args);
  8.        listeners.starting();

  9.        Collection exceptionReporters;
  10.        try {
  11.            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
  12.            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
  13.            this.configureIgnoreBeanInfo(environment);
  14.            Banner printedBanner = this.printBanner(environment);
  15.            context = this.createApplicationContext();
  16.            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
  17.            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
  18.            this.refreshContext(context);
  19.            this.afterRefresh(context, applicationArguments);
  20.            stopWatch.stop();
  21.            if (this.logStartupInfo) {
  22.                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
  23.            }

  24.            listeners.started(context);
  25.            this.callRunners(context, applicationArguments);
  26.        } catch (Throwable var10) {
  27.            this.handleRunFailure(context, var10, exceptionReporters, listeners);
  28.            throw new IllegalStateException(var10);
  29.        }

  30.        try {
  31.            listeners.running(context);
  32.            return context;
  33.        } catch (Throwable var9) {
  34.            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
  35.            throw new IllegalStateException(var9);
  36.        }
  37.    }

SpringApplication.run方法得返回值是ConfigurableApplicationContext 这个接口继承了ApplicationContext,所以说还是一个容器。

  1. public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable

通过这个容器可以获取到Spring中装配得对象,代码如下:

  1.      Environment  env=context.getEnvironment();

通过这个配置对象可以获取到属性配置文件里得配置信息:因为所有得配置信息都会存储到环境对象里面去。

  1. package com.boot.config.configdemo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.ConfigurableApplicationContext;
  5. import org.springframework.core.env.Environment;

  6. @SpringBootApplication
  7. public class ConfigDemoApplication {

  8.    public static void main(String[] args) {
  9.        ConfigurableApplicationContext context =
  10.                SpringApplication.run(ConfigDemoApplication.class, args);
  11.        Environment  env=context.getEnvironment();
  12.        String property = env.getProperty("local.ip.addr ");
  13.        System.out.println(property);
  14.        context.close();
  15.    }

  16. }

运行结果如下:

还可以通过环境变量获取当前文件的目录:user.dir是系统的环境变量。

  1.   System.out.println(env.getProperty("user.dir"));

运行结果如下:

SpringBoot默认装载属性的配置文件默认是有四个顺序的。优先级从高到低 resource/config/xxx.properties>resource/config/xxx.yml>resource/xxx.properties>resource/yyy.yml 比如:在resources文件下建个config文件夹:


这时读取的信息运行结果如下:

比如:在Connfig文件下创建一个xxx.yml的文件:

再次运行这时也是读取的是config/xxx.properties的配置文件的信息:

这时把config/application.properties的文件的名字改为application1.properties,再次运行 :这时读取的是config/xxx.yml配置文件里的内容。

如果这时把config的文件名改为conf的话:这时只会读取resource下的application.properties文件里的内容,如果都没找到的话就为null。

总结:先从config的文件下去查找xxx.properties如果找不到再去confgi下的xxx.yml文件里去查找,如果都查不到再去resource下的根目录去查找,默认的只会去读取一个配置文件里的内容,默认只会读取前缀为application名字的配置文件,默认找的是config里面的文件。

上面的方式在实际的开发中用的不多,只是在框架中用的很多。2):通过@Value获取 新建一个类:RedisConfig:在Spring容器中如何去读取redisConfig类中的属性


这样运行 的话会报错的:因为这个类是交给Spring容器管理的,而这几个属性的值在属性的配置文件里面是读取不出来的,所以会报错:

解决办法:1):给设置默认值:


再次运行的话:这时是没有报错的。

运行的结果如下:

但是有的时候端口不一定是6379,有可能会变得话:2):在属性得配置文件里面去配置redis的一些信息:


运行的结果如下:这时就不会读取默认的值了,而是读取配置文件里的信息了。

在Spring中上面的方式是经常用的,但是在SpringBoot中还有更友好的方式去使用它。现在在conf的文件下的xxx.yml文件里加上redis的配置信息,想要读取conf/application.yml文件里的内容:

1):指定配置文件的路径及名称

然后再次运行:这就是从conf/app.yml文件里面去读取redis的配置信息了。

现在想读取conf/app1.properties和conf/app.yml文件里的内容:


换下环境的配置信息:如果在VM options写-D也是一样的效果。

运行的结果如下:

2):通过@ConfigurationProperties(prefix="xx")上面的方式是通过环境的参数来配置,现在可以通过代码的方式去配置:这个注解的意思是把属性配置文件中的属性的配置装配到这个类里面去。


  1. package com.boot.config.configdemo;

  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;

  4. @ConfigurationProperties(prefix = "weizhaoyang.datasource")
  5. @Component
  6. public class DateSourceProperties {
  7.    private  String  driverClassName;
  8.    private  String  url;
  9.    private  String  username;
  10.    private  String  password;

  11.    public String getDriverClassName() {
  12.        return driverClassName;
  13.    }

  14.    public void setDriverClassName(String driverClassName) {
  15.        this.driverClassName = driverClassName;
  16.    }

  17.    public String getUrl() {
  18.        return url;
  19.    }

  20.    public void setUrl(String url) {
  21.        this.url = url;
  22.    }

  23.    public String getUsername() {
  24.        return username;
  25.    }

  26.    public void setUsername(String username) {
  27.        this.username = username;
  28.    }

  29.    public String getPassword() {
  30.        return password;
  31.    }

  32.    public void setPassword(String password) {
  33.        this.password = password;
  34.    }

  35.    @Override
  36.    public String toString() {
  37.        return "DateSourceProperties{" +
  38.                "driverClassName='" + driverClassName + '\'' +
  39.                ", url='" + url + '\'' +
  40.                ", username='" + username + '\'' +
  41.                ", password='" + password + '\'' +
  42.                '}';
  43.    }
  44. }

运行的结果如下:

3):正常的用法是在配置类里面或者主类里面配置上面的信息:这种写法方便调试。注释掉这两个注解,然后在主类里面去写:


这样写也是ok的。

运行的结果如下:

或者按照下面的写法创建一个config文件夹也是ok的。config文件夹统统都是配置项的一些信息,这个就是项目开发中正常这么做的。

这个运行的结果也是ok的。

4):如果想要统一集中化的去管理配置信息的路径:这样也是可以的。这样就可以不用在运行的时候参数配置了。但是不支持xxx.yml的配置。这样的配置会把所有的路径进行综合,然后放入到环境对象里面去的,会进行一个合并的。

  1. package com.boot.config.configdemo;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.context.annotation.PropertySource;

  4. @Configuration
  5. @PropertySource("classpath:conf/app.yml")//不支持
  6. @PropertySource("classpath:conf/app1.properties")
  7. @PropertySource("file:/E:/temp/app.properties")
  8. public class FileConfig {
  9. }
相关文章
|
13小时前
|
Java 容器 Spring
SpringBoot入门(六)
SpringBoot入门(六)
|
13小时前
|
Java Spring 容器
SpringBoot的入门(三)
SpringBoot的入门(三)
|
13小时前
|
监控 Java Spring
SpringBoot的入门(四)
SpringBoot的入门(四)
|
13小时前
|
JSON Java 数据格式
SpringBoot入门(七)
SpringBoot入门(七)
|
13小时前
|
消息中间件 NoSQL Java
SpringBoot的入门(五)
SpringBoot的入门(五)
|
13小时前
|
Java API 容器
SpringBoot入门(十)
SpringBoot入门(十)
|
13小时前
|
Java 应用服务中间件 nginx
SpringBoot入门(九)
SpringBoot入门(九)
|
13小时前
|
JSON Java API
SpringBoot入门(八)
SpringBoot入门(八)
|
13小时前
|
Java Spring 容器
SpringBoot的入门(二)
SpringBoot的入门(二)
|
3月前
|
Java 数据库连接 Maven