栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot :自定义starter的创建和使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot :自定义starter的创建和使用

一、创建spring boot 的starter项目,添加依赖,完整pom如下:



    4.0.0

    cn.edu.tju
    spring-boot-starter-demo
    2.0.6

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.7.RELEASE
         
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            com.google.code.gson
            gson
            2.8.6
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            mysql
            mysql-connector-java
            5.1.30
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.1.5.RELEASE
        

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.13
        

        
            org.springframework.boot
            spring-boot-autoconfigure
        

        
            org.springframework.boot
            spring-boot-autoconfigure-processor
            true
        

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    


二、创建实体类,这个类将被作为自动装配的bean类

package cn.edu.tju.domain;

public class UserInfo {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public UserInfo(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public UserInfo(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                '}';
    }
}

三、创建配置类,用来从application.properties中读取配置:

package cn.edu.tju.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "tju")
public class MyConfig {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

四、创建Configuration类,注入步骤三中的配置类,并使用@Bean注解来自动装配bean

package cn.edu.tju.config;

import cn.edu.tju.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Random;

@Configuration
@EnableConfigurationProperties(MyConfig.class)
public class MyBeanConfig {
    @Autowired
    private MyConfig myConfig;
    @Bean
    public UserInfo getMyUserInfo(){
       String username=myConfig.getUsername();
       String password=myConfig.getPassword();
       int id= new Random().nextInt(100);
       UserInfo userInfo=new UserInfo(id,username,password);
       return userInfo;

    }
}

五、在项目的resources目录里建目录META-INF,并在META-INF里建spring.factories文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  cn.edu.tju.config.MyBeanConfig

六、打包这个项目,在idea里运行

mvn clean package install

,将生成的jar包安装到本地maven仓库。
七、新创建一个spring boot项目,并通过maven引入上述jar包,完整pom如下:



    4.0.0

    com.example
    auto-config-test
    1.0-SNAPSHOT



    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.7.RELEASE
         
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        


        
            org.springframework.boot
            spring-boot-starter-data-redis
        


        
            com.google.code.gson
            gson
            2.8.6
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            mysql
            mysql-connector-java
            5.1.30
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.1.5.RELEASE
        

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.13
        

        
            cn.edu.tju
            spring-boot-starter-demo
            2.0.6
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

        
    



八、配置application.properties,其中配置了starter项目中需要注入的属性:
tju.username
tju.password

server.port=8087
spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx/test
spring.datasource.username=root
spring.datasource.password=xxxxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
tju.username=hi
tju.password=hello

九、创建controller,并使用@Autowired注解注入starter项目中定义的实体类(UserInfo)

package com.example.controller;

import cn.edu.tju.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private UserInfo userInfo;


    @RequestMapping("/myStarter")
    public String getInfo(){
        String result=userInfo.toString();
        return result;
    }
}

十、创建启动类,并启动程序

package com.example;

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

@SpringBootApplication
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class,args);

    }
}

十一、访问接口http://localhost:8087/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948243.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号