• 自己写spring boot starter


    自己写spring boot starter

    学习了:《spring boot实战》汪云飞著 6.5.4节

    pom.xml

    <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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.stono</groupId>
      <artifactId>sboot161</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>sboot161</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-autoconfigure</artifactId>
          <version>1.5.9.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>

    HelloService.java

    package com.stono;
    
    public class HelloService {
        private String msg;
    
        public String sayHello(){
            return "Hello"+msg;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    HelloServiceProperties.java
    package com.stono;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "hello")
    public class HelloServiceProperties {
        private static final String MSG = "world";
        private String msg = MSG;
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    HelloServiceAutoConfiguration.java
    package com.stono;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableConfigurationProperties(HelloServiceProperties.class)
    @ConditionalOnClass(HelloService.class)
    @ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true)
    public class HelloServiceAutoConfiguration {
        @Autowired
        private HelloServiceProperties helloServiceProperties;
    
        @Bean
        @ConditionalOnMissingBean(HelloService.class)
        public HelloService helloService(){
            HelloService helloService = new HelloService();
            helloService.setMsg(helloServiceProperties.getMsg());
            return helloService;
        }
    }

    编译完毕之后,使用IntelliJ 自带的Edit Configurations... 进行maven 编译设置,参数为 clean package

    运行配置好的maven命令即可编译jar包;

    如果在terminal中直接运行mvn clean package

    会出现错误:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project sboot161:
     Compilation failure: Compilation failure:
    [ERROR] 不再支持源选项 1.5。请使用 1.6 或更高版本。
    [ERROR] 不再支持目标选项 1.5。请使用 1.6 或更高版本。
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

    学习了:http://blog.csdn.net/sosous/article/details/78312867

    进行pom.xml文件修改:

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>

    就可以正常运行 mvn clean package了;

    运行之后,需要使用mvn install命令将jar包安装本地库:

    mvn install:install-file -Dfile=D:JavaIdeaProjectssboot161	argetsboot161-1.0-SNAPSHOT.jar -DgroupId=com.stono -DartifactId=sboot161 -Dversion=1.0-SNAPSHOT -Dpackaging=jar

    然后在其他项目中就可以添加该pom依赖,在程序中自动引入该HelloService了;

  • 相关阅读:
    grep 同时满足多个关键字、满足任意关键字和排除关键字
    随笔_生活感想
    oracle中to_number(),LPAD(),NVL()函数
    向数据库插入数据为null——忘记加@RequestBody
    自定义css样式覆盖Element-ui的样式
    ORA-00001: 违反唯一约束条件
    ORA-01400: 无法将 NULL 插入 ("JXKH"."SYS_MENU"."MENU_ID")
    'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
    Error: Cannot find module 'webpack-merge'
    ERROR:oracle.jdbc.driver.OracleDriver is deprecated.
  • 原文地址:https://www.cnblogs.com/stono/p/8447975.html
Copyright © 2020-2023  润新知