[Java] How Spring DI works

Features of Spring DI container

DI.png

About the routing mechanism

Project Root
└─src
    └─ main
        └─ java  
            └─ com.example
                └─ demo
                    └─DemoApplication
                    └─SampleApplication

Failure example

結果.png

DemoApplicaton.java


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    @RequestMapping("/") //From which address do you get it? "/“Is http://localhost:8080/Point to
    String index(){
        return "Hello World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

SampleApplicaton.java


package com.example.hoge;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SampleController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    //Receive when a request comes to the root of the URL
    //DemoApplication and root (=8080) is covered
    public String index(Model model) {
        model.addAttribute("message", "Hello World!!");
        return "index";
    }
}

Routing mechanism

Project Root
└─src
    └─ main
        └─ java  
            └─ com.example
                └─ demo
                    └─DemoApplication
                └─ hoge
                    └─SampleApplication

DemoApplication.java


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    @RequestMapping("/hoge")
    String index(){
        return "Hello World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Whitelabel Error Page

err.png

SampleApplicaton.java


package com.example.hoge;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SampleController {
    @RequestMapping(value = "/bar", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("message", "Hello World!!");
        return "index";
    }
}

Recommended Posts

[Java] How Spring DI works
[Java] Spring DI ③
Spring Java
[Java] Spring DI ④ --Life cycle management
How Dispatcher servlet works in Spring MVC
How Spring Security works with Hello World
How jul-to-slf4j works
Java Performance Chapter 4 How the JIT Compiler Works
Deep dive into how HashMap works in Java
Spring Framework Summary-About DI
About DI of Spring ①
First Spring Boot (DI)
About DI of Spring ②
Spring Basics ~ DI Edition ~
How to set Dependency Injection (DI) for Spring Boot
[Java] How to omit spring constructor injection with Lombok
[Java] How to use Map
Java Config with Spring MVC
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to use java Optional
How to minimize Java images
How to write java comments
[Java] How to use Optional ②
[Java] How to use removeAll ()
Introduction to Spring Boot ① ~ DI ~
[Java] How to use string.format
How to use Java Map
How to call and use API in Java (Spring Boot)
[Java] Spring AOP execution order
CICS-Run Java application-(4) Spring Boot application
Using Mapper with Java (Spring)
Java tips --Spring execution Summary
How to use Java variables
How to convert Java radix
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
About Spring DI related annotations
How to use Lombok in Spring
Spring Boot + Java + GitHub authentication login
How to study Java Silver SE 8
How to use Java HttpClient (Get)
How to unit test Spring AOP
How about TECH ACADEMY ?? [Java course]
Studying Java # 6 (How to write blocks)
How to use Spring Data JDBC
Java Spring environment in vs Code
[Java] How to update Java on Windows
How to make a Java container
How to disassemble Java class files
[How to install Spring Data Jpa]
How to use Java HttpClient (Post)
How to set Spring Boot + PostgreSQL
Elastic Beanstalk (Java) + Spring Boot + https
How slow is a Java Scanner?
Java --Jersey Framework vs Spring Boot
How to learn JAVA in 7 days