Spring Boot

Brenda Gabriela Senra da Silva
Curso por Brenda Gabriela Senra da Silva, atualizado 24 dias atrás Colaboradores

Descrição

Spring boot

Informações do módulo

Sem etiquetas
   @Component @Component tells Spring: “Create this class and manage it.” Example: @Component class Cat { } @Autowired Cat cat; // ✅ works Without @Component: class Cat { } @Autowired Cat cat; // ❌ Spring cannot find it   Compare normal Java vs Spring Important sentence (remember this) Spring creates objects for you. You do not use new. Important sentence (remember this) Spring creates objects for you. You do not use new. ✅ Normal Java (NO Spring)   PaymentServiceImpl service = new PaymentServiceImpl(); Java creates the object. Spring is NOT involved. ✅ Spring way In Spring, you do NOT write new. Instead, you ask Spring: @Autowired PaymentService service; Spring answers: “Ok, I’ll give you one.”                      
Mostrar menos
Sem etiquetas
IOC: Inversion on control container AOP:Aspect Oriented Programming MVC DAF Spring Bean @Configuration declares class as ‘full’ configuration class Class must be public and non-final     Step-by-step explanation 🧠 1️⃣ @Configuration @Configuration public class AppConfig {   This tells Spring: “This class defines beans. I will manage them.” Important: Spring creates a proxy of this class Ensures singleton behavior   Bean declares bean configuration inside configuration class @Bean method must be non-final and non-private     What does proxy mean (in Spring)? A proxy is a wrapper object that sits between your code and the real object. 👉 Your code thinks it’s calling the real class, 👉 but it’s actually calling a Spring-generated class that controls what happens.   Singleton behavior means: Spring creates only one instance of a bean and always returns the same instance. The most important concept 🔑 Even though you see: new PaymentServiceImpl(...) new JdbcAccountRepository(...) Spring guarantees: Only one instance of each bean All dependencies are shared No duplicated objects This works because: The class is annotated with @Configuration Spring uses a proxy to control method calls        
Mostrar menos
Sem etiquetas
A component is a class that Spring creates and manages. Spring has special annotations to mark classes. Meaning: Spring needs a mark to know: which class to create which class to manage Core container (easy meaning) Core container = Spring itself So this sentence: “managed by the core container” Means: “managed by Spring” @Service, @Repository, @Controller These are special types of @Component. They all mean the same basic thing: “Spring, create this class.” Difference (ONLY meaning, no theory) @Repository → data / database @Service → business logic @Controller → web / API But for Spring: 👉 They are ALL @Component.
Mostrar menos
Sem etiquetas
One sentence to remember ✨ With @Bean, the method name is the bean name.   First: what is a bean? A bean is an object created by Spring. What is Bean Naming? Bean naming = the name of the object in Spring.   Look at this code 👇 @Bean public PaymentService paymentService() {     return new PaymentServiceImpl(accountRepository()); } Question: What is the bean name? 👉 The bean name is paymentService   Very important rule 🧠 The bean name is the method name.  
Mostrar menos
Sem etiquetas
Dependency Injection (DI) – Summary Dependency Injection means that Spring provides the objects a class needs. A dependency is something a class needs to work. With DI, the class does not create objects by itself. Spring creates and manages the objects. @Autowired tells Spring to inject the dependency automatically. DI makes the code cleaner, easier to test, and loosely coupled.   Dependency Injection – Types 1. Constructor Injection Dependencies are passed through the constructor. This is the recommended way in Spring. Makes the class Imutável and easy to test. Example idea: Spring gives the dependency when the object is created. 2. Field Injection Dependency is injected directly into the field. Uses @Autowired on the variable. Easy, but not recommended for large projects. 3. Configuration (Method Injection) Dependencies are defined using @Configuration and @Bean. Spring creates objects using configuration methods. Useful when you want full control over object creation. 4. Setter Injection Dependency is injected using a setter method. Allows changing the dependency later. Used when the dependency is optional.
Mostrar menos
Sem etiquetas
❓ É obrigatório implementar CommandLineRunner? Português 🇧🇷 Não é obrigatório. Você só implementa CommandLineRunner quando quer executar código automaticamente ao iniciar a aplicação Spring Boot. English 🇺🇸 No, it is not mandatory. You only implement CommandLineRunner when you want to run code automatically at application startup. 🟢 Quando USAR CommandLineRunner Português 🇧🇷 Use quando: a aplicação não é web você quer imprimir algo no terminal quer testar lógica quer rodar código logo que o Spring inicia English 🇺🇸 Use it when: the application is not web you want to print something in the terminal you want to test logic you want to run code at startup   🔵 Quando NÃO usar Português 🇧🇷 Não use quando: a aplicação é web (controllers, endpoints) você não precisa executar código automaticamente English 🇺🇸 Do not use it when: the application is web (controllers, endpoints) you don’t need to run code automatically
Mostrar menos
Sem etiquetas
Para que serve @JsonAlias? @JsonAlias serve para dizer ao Java/Jackson: 👉 “Esse campo do Java pode vir com outro nome no JSON.” Ou seja: ele liga o nome do JSON ao nome do atributo Java.
Mostrar menos
Sem etiquetas
Serve tanto para o precesso de serialização como para desserialização It works for both deserialization and serialization.   Serialization > Object ➜ JSON Deserialization >  JSON ➜ Object   @JsonProperty Used to define the official JSON field name. ✔ Works for: Deserialization (JSON → Java) Serialization (Java → JSON) ❌ Only one name @JsonProperty("Title") String title;   @JsonAlias Used to accept alternative JSON field names. ✔ Works only for reading ✔ Accepts multiple names @JsonAlias({"Title", "title", "name"}) String title;  
Mostrar menos
Sem etiquetas
1.criar um novo serviço para converter dados e obter flexibilidade create a dedicated service to handle data conversion, improving flexibility.
Mostrar menos

.

Sem etiquetas
processo de desserialização em uma classe
Mostrar menos
Sem etiquetas
Jackson — o que é e como ele se encaixa no seu projeto O Jackson é uma biblioteca Java usada para trabalhar com JSON. 📌 O que o Jackson faz? Ele faz duas coisas principais: Desserialização → converte JSON em objeto Java. Serialização → converte objeto Java em JSON. 🔄 No seu projeto ScreenMatch Você faz uma requisição para a API do OMDb e recebe um JSON como este: Esse JSON é só texto. O Java não entende automaticamente que isso representa uma série. ➡️ É aí que entra o Jackson Você cria um record: E usa o ObjectMapper: Agora o Jackson transforma automaticamente: Ou seja: 🧠 Como relacionar com o que você já aprendeu Fluxo completo do seu projeto 📦 E o Maven? O Jackson não faz parte do Java padrão. Por isso você adicionou a dependência no pom.xml: O Maven lê o pom.xml e baixa a biblioteca Jackson automaticamente.
Mostrar menos
Sem etiquetas
@JsonIgnoreProperties é uma anotação do Jackson usada para ignorar propriedades do JSON que você não quer mapear. EX:  import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public record SeriesData(         String title,         Integer totalSeasons,         @JsonAlias("imdbRating") String rating ) { }
Mostrar menos