Skip to content

Instantly share code, notes, and snippets.

@renatoapcosta
Last active April 5, 2021 19:12
Show Gist options
  • Save renatoapcosta/ca6f73d015696baef702c045cec42ab8 to your computer and use it in GitHub Desktop.
Save renatoapcosta/ca6f73d015696baef702c045cec42ab8 to your computer and use it in GitHub Desktop.

Spring Boot

http://start.spring.io

Introdução

Spring Boot 2.X - Requisito mínimo java 8

Spring Boot 1.X - Suporta java 6,7,8 e não tem planos para suporte java 9

Sub Projetos

Spring Boot Web

Spring Boot JPA

Criando Projeto

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>br.com.ractecnologia.templates</groupId>
	<artifactId>spring-boot2</artifactId>
	<version>0.0.1-RC1</version>
	<packaging>jar</packaging>

	<name>spring-boot2</name>
	<description>Project Spring Boot Template</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	 <dependencies>

		 <dependency>
			 <groupId>org.springframework.boot</groupId>
			 <artifactId>spring-boot-starter</artifactId>
		 </dependency>

	</dependencies>

	 <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Classe de inicialização

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

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

Executando um comando

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class CommandLineRunnerImpl implements CommandLineRunner {

    private static Logger log = LoggerFactory
            .getLogger(CommandLineRunnerImpl.class);

    @Override
    public void run(String... args) throws Exception {
        log.info("EXECUTING : command line runner");
    }
}

Projetos essenciais

Lombok

Eclipse

maven

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

uso

@Getter
@Setter
@Slf4

Devtools - Durante o desenvolvimento para Hot Deploy

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<scope>runtime</scope>
</dependency>

LiveReload - plugin do Chrome para atualizar página

Actuator - Monitorando a saúde da aplicação

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Necessita de um projeto web. Gera diversos endpoints para testar nosso app.

Rodando projeto em Linha de comando

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<executions>
				<execution>
					<goals>
					    <goal>repackage</goal>
					</goals>
				    </execution>
			</executions>
		</plugin>
	</plugins>
</build>

Na linha de comando (Spring Boot CMD):

java -jar nome_projeto.jar

mvn spring-boot:run

mvn spring-boot:run -Dspring-boot.run.profiles=dev

mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=local --app.datasource.conecta.jdbcUrl=jdbc:oracle:thin:@10.206.53.100:1521:banco --spring.jackson.time-zone=UTC" -Drun.jvmArguments="-Duser.timezone=UTC"

Para CreateProcess error=206

mvn spring-boot:run -Dspring-boot.run.profiles=dev -Dspring-boot.run.fork=false

Mudando a porta: java -Dserver.port=8080 -jar nome_projeto.jar

Configurando logging

application.properties

logging.level.org.springframework = debug
logging.level.br.com.ractecnologia = info

Habilitando logging.level.org.springframework = debug para debug, podemos ver todas as configurações quando iniciamos o servidor.

Profile

application.properties application-dev.properties application-hml.properties application-prd.properties

Na linha de comando

-Dspring.profiles.active=dev

Versões

Spring Boot 2

spring 5.0.4.RELEASE

slf4 1.7.25

tomcat 8.5

hibernate 5.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment