Spring Boot and Cloud part4

Spring Boot and Cloud part4

1、build.gradle文件

buildscript {
    ext{
        springBootVersion = '2.3.1.RELEASE'
    }
    repositories {
        maven{
            url 'https://repo.spring.io/release'
        }

        mavenCentral()
    }
    dependencies {
        classpath(
                "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}",
                "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
        )
    }
}
apply{
    plugin("java")
    plugin("maven")
    plugin("idea")
    plugin("org.springframework.boot")
    plugin("io.spring.dependency-management")

}

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven{
        url 'https://repo.spring.io/release'
    }

    mavenCentral()
}

dependencies {
    compile(
            "org.springframework.boot:spring-boot-starter-web",
            "org.springframework.boot:spring-boot-loader"
    )
}

其中:

dependencies {
        classpath(
                "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}",
                "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
        )

里的
"org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"插件提供一些Springboot使用的一些插件。
“io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE” 插件是一些依赖管理,例如添加不指定版本号的依赖

2、application.yml中自定义的配置(简单方式)

application.yml文件中配置自定义配置

	myConfig:  
		myObject:  
    		myName: zhangsan  
    		myAge: 20  

可以在控制器中使用@value注解进行获取值
例如:

public class MyController {

    @Value("${myConfig.myObject.myName}")
    private String myName;

    @Value("${myConfig.myObject.myAge}")
    private int myAge;
    }

其中的@Value注解则是获取上文中application.yml中自定义的配置

3、@Configuration

@Configuration注解是表明这是一个配置类
例如:
存在一个MyConfigBean类

public class MyConfigBean {

	@Value("${myConfig.myObject.myName}")
	private String myName;

	@Value("${myConfig.myObject.myAge}")
	private String  myAge;

	public String getMyName() {
   	 return myName;
	}

	public void setMyName(String myName) {
    	myName = myName;
	}

	public String getMyAge() {
  	  return myAge;
	}

	public void setMyAge(String myAge) {
   	 myAge = myAge;
	}
}  

使用@Configuration注解是表明这是一个配置类

@Configuration  
public class MyConfig {

	@Bean
	public MyConfigBean myConfigBean(){
    	return  new MyConfigBean();//返回一个MyConfig对象
	}
}

4、@Autowired

@Autowired注解表明让springboot自动装配这个类

@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MyController {

	@Value("${myConfig.myObject.myName}")
	private String myName;

	@Value("${myConfig.myObject.myAge}")
	private int myAge;

	@Autowired
	private MyConfigBean myConfigBean;


	@RequestMapping(value = "/person", method = RequestMethod.GET)
	public Person getPerson(){
    	Person person=new Person();
    	person.setId(25);
    	person.setName("张三");
    	person.setBirthaday(new Date());
    	System.out.println(this.myName);
    	System.out.println(this.myAge);
    	return person;
	}
}

注意:此时会出现Connot found autowired的异常,这是由于idea的命名纠错机制问题,在spring boot 2.0后可以在DAO层加上@Repository注解解决此错误(也有可能是在Config中没有加上@Bean注解的问题),要注意,@Repository注解与@Bean注解不可以同时存在,二者存在其一即可解决问题

5、@Bean

@Configuration  
public class MyConfig {

	@Bean
	public MyConfigBean myConfigBean(){
    	return  new MyConfigBean();//返回一个MyConfig对象
	}
}

其中@Bean注解表明这是一个spring boot管理的bean对象,没有@Bean注解则无法自动装配

6、“org.springframework.boot:spring-boot-loader”

此包一般只做为一个插件使用就好,并不需要做为一个依赖级别放在compile中,其作用是用于将项目工程打为可运行的jar包
compile(

        "org.springframework.boot:spring-boot-starter-web", 
        
        "org.springframework.boot:spring-boot-loader"  //可以不需要添加此依赖
        
)  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
An active internet connection Java 8+ Docker Maven Git client Chapter 1, Introduction to Microservices, will introduce you to the microservices architecture, cloud environment, etc. You will learn the difference between a microservice based application and a monolith application while also learning how to migrate to a microservices application. Chapter 2, Spring for Microservices, will introduce you Spring Boot framework. You will learn how to effictively use it to create microservice application. We will cover such topics like creating REST API using Spring MVC annotations, providing API documentation using Swagger2, and exposing health checks and metrics using Spring Boot Actuator endpoints. Chapter 3, Spring Cloud Overview, will provide a short description of the main projects being a part of Spring Cloud. It will focus on describing the main patterns implemented by Spring Cloud and assigning them to the particular projects. Chapter 4, Service Discovery, will describe a service discovery pattern with Spring Cloud Netflix Eureka. You will learn how to run Eureka server in standalone mode and how to run multiple server instances with peer-to-peer replication. You will also learn how to enable discovery on the client side and register these clients in different zones. Chapter 5, Distributed Configuration with Spring Cloud Config, will describe how use distributed configuration with Spring Cloud Config in your applications. You will learn how to enable different backend repositories of property sources and push change notifications using Spring Cloud Bus. We will compare discovery first bootstrap and config first bootstrap approaches to illustrate integration between discovery service and configuration server. Chapter 6, Communication Between Microservices, will describe the most important elements taking a part in an inter-service communication: HTTP clients and load balancers. You will learn how to use Spring RestTemplate, Ribbon, and Feign clients with or without

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值