Spring Boot 6-SpringBoot运行原理实现

Spring Boot自动配置实战

1、新建Spring-boot-starter-hello项目。

2、新建HelloService.java
package com.tzp.helloworld.helloservice;

public class HelloService {

    private String msg;

    public String sayHello() {
        return "=======>>" + msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
2、新建HelloServiceProperties.java
package com.tzp.helloworld.helloproperties;

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;
    }

}
3、新建HelloServiceAutoConfigration.java
package com.tzp.helloworld.helloServiceConfigration;

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.ConditionalOnMissingClass;
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;

import com.tzp.helloworld.helloproperties.HelloServiceProperties;
import com.tzp.helloworld.helloservice.HelloService;

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)
public class HelloServiceAutoConfigration {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }

}
4、mvn install将项目打成jar包,并将jar包添加到本地maven仓库。5、接下来我们就可以在别的工程上使用该jar包了。
<dependency>
    <groupId>com.tzp</groupId>
    <artifactId>spring-boot-starter-hello</artifactId>
  <version>1.0.0</version>
</dependency>
hello:
  msg: 唐增平的springboot
package com.tzp.helloworld.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tzp.helloworld.helloservice.HelloService;

@RestController
public class HelloSpringBoot {

    @Value("${melo.name}")
    private String name;

    @Autowired
    private HelloService helloService;

    @RequestMapping("/")
    public String hello() {
        return "hello spring boot " + name + helloService.getMsg();
    }

}
6、运行项目并访问。


原文地址:https://www.cnblogs.com/zengpingtang/p/10803706.html

时间: 2024-08-30 01:07:51

Spring Boot 6-SpringBoot运行原理实现的相关文章

spring boot项目后台运行

spring boot项目后台运行 Spring Boot应用的几种运行方式: (1)运行Spring Boot的应用主类 (2)使用Maven的Spring Boot插件mvn spring-boot:run来运行 (3)打成jar包后,使用java -jar运行 Linux/Unix: nohup java -jar XXX.jar & 这个命令就可以实现后台运行. 原文地址:https://www.cnblogs.com/heqiyoujing/p/9457138.html

Spring Boot Executable jar/war 原理

spring boot executable jar/war spring boot里其实不仅可以直接以 java -jar demo.jar的方式启动,还可以把jar/war变为一个可以执行的脚本来启动,比如./demo.jar. 把这个executable jar/war 链接到/etc/init.d下面,还可以变为linux下的一个service. 只要在spring boot maven plugin里配置: <plugin> <groupId>org.springfram

spring boot 自动装配的原理

参考: https://blog.csdn.net/Dongguabai/article/details/80865599.如有侵权,请联系本人删除! 入口: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.se

使用pm2来保证Spring Boot应用稳定运行

Spring Boot开发web应用就像开发普通的java程序一般简洁,因为其内嵌了web容易,启动的时候只需要一条命令java -jar server.jar即可,非常方便.但是由此而来的问题是万一应用挂了怎么办? 别担心,有pm2进程管理工具可以帮到你. PM2简介 pm2原先是nodejs应用的进程管理工具,不过其良好的设计以及扩展性可以手动执行执行进程. PM2安装 1.安装NodeJs npm install pm2 -g PM2基本命令 pm2 list 查看所有被PM2管理的进程列

spring boot 配置文件动态更新原理 以Nacos为例

配置文件的动态更新 通常获取配置文件的方式 1, @Value 2. @ConfigurationProperties(Prefix) 如果是在运行时要动态更新的话, 第一种方式要在bean上加@RefreshScope 第二种方式是自动支持的. 以Nacos为为例,我们可以看下源码是如何实现的: Nacos获取配置中心是通过单独一个线程的长轮询获取的: com.alibaba.nacos.client.config.impl.ClientWorker.LongPollingRunnable 当

Spring Boot 学习----SpringBoot整合Redis

SpringBoot整合Redis 说明:由于使用的是window系统,所以本文中使用的redis是再Docker中运行的. 至于如何启动redis这里不再说明,需要的请自行百度. 配置pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </depend

springboot运行原理

1.springboot自动配置 2.可以通过下面三种方式查看当前项目中已启用和未启用的自动配置的报告 a.运行jar时增加--debug参数: java  -jar  xxx.jar --debug b.在application.properties文件中设置属性: debug=true c.在sts中设置  Run Configurations -----------------------------------------之后再总结 原文地址:https://www.cnblogs.co

Spring Boot常用注解和原理整理

一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExc

spring boot 整合mapreduce运行的ClassNotFoundException

问题 一个wordcount运行总是报错 java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.hadoop.mapreducedemo1.mapreducedemo.mapper.MyMapperTask 网上最常见的解决方案就是 job.setJarByClass(Main.class); //job.setJar("/root/user/mapreducedemo-0.0.1-SNAPSHOT.jar

Spring Cloud是怎么运行的?

导读 在之前的文章中给大家介绍了Spring Boot的基本运行原理(链接),收到了很多读者朋友们关于目前比较流行的微服务框架Spring Cloud的问题反馈.因此,在这篇文章中小码哥打算和大家一起通过梳理下Spring Cloud的运行原理来相对全面的了解下Spring Cloud. 随着微服务架构在越来越多的公司得到实践和应用,基于Spring Cloud的这一套微服务生态框架体系,也在这个过程中得到大量的实践和运用.虽然目前Service Mesh的概念也越来越得到关注,但是目前微服务开