Skip to content

[박재홍] 3주차 학습 완료 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: jaehong
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jaehong/helloboot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jetty'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.example.helloboot;


import java.util.LinkedList;
import java.util.Queue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

private final HelloService helloService;

public HelloController(HelloService helloService) {
this.helloService = helloService;
}


@RequestMapping("/hello")
public String hello(String name) {
Queue<Integer> queue = new LinkedList<>();
if (name == null || name.trim().length() == 0) {
throw new IllegalArgumentException();
}
return helloService.sayHello(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.helloboot;


import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@Service
@Primary
public class HelloDecorator implements HelloService {
private final HelloService helloService;

public HelloDecorator(HelloService helloService) {
this.helloService = helloService;
}

@Override
public String sayHello(String name) {
return "*" + helloService.sayHello(name) + "*";
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
package com.example.helloboot;


import config.MySpringBootApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;


@Configuration
@ComponentScan
@MySpringBootApplication
public class HellobootApplication {

@Bean
public ServletWebServerFactory servletWebServerFactory(){
return new TomcatServletWebServerFactory();
}

@Bean
public DispatcherServlet dispatcherServlet(){
return new DispatcherServlet();
}
public static void main(String[] args) {
SpringApplication.run(HellobootApplication.class, args);
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.example.helloboot;

public class SimpleHelloService implements HelloService{

import org.springframework.stereotype.Service;

@Service
public class SimpleHelloService implements HelloService {
@Override
public String sayHello(String name) {
return "hello "+ name;
return "hello " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package config;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(MyAutoConfigImportSelector.class)
public @interface EnableMyAutoConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config;

import java.util.ArrayList;
import org.springframework.boot.context.annotation.ImportCandidates;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class MyAutoConfigImportSelector implements DeferredImportSelector {
private final ClassLoader classLoader;

public MyAutoConfigImportSelector(ClassLoader classLoader) {
this.classLoader = classLoader;
}

@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
var autoConfigs = new ArrayList<String>();
ImportCandidates.load(MyAutoConfigImportSelector.class, classLoader).forEach(autoConfigs::add);

//ImportCandidates.load(MyAutoConfiguration.class, classLoader).forEach(autoConfigs::add);
return autoConfigs.toArray(String[]::new);
}
}
14 changes: 14 additions & 0 deletions jaehong/helloboot/src/main/java/config/MyAutoConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package config;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Configuration;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Configuration(proxyBeanMethods = false)
public @interface MyAutoConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Configuration
@ComponentScan
@EnableMyAutoConfiguration
public @interface MySpringBootApplication {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package config.autoconfig;


import config.MyAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.DispatcherServlet;

@MyAutoConfiguration
public class DispatcherServletConfig {
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config.autoconfig;


import config.MyAutoConfiguration;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@MyAutoConfiguration
public class JettyServletWebServerConfig {
@Bean
public ServletWebServerFactory servletWebServerFactory() {
return new JettyServletWebServerFactory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package config.autoconfig;

import config.MyAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@MyAutoConfiguration
public class TomcatServletWebServerConfig {
@Bean
public ServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.autoconfig.TomcatServletWebServerConfig
config.autoconfig.DispatcherServletConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.helloboot;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

class HelloApiTest {

@Test
void hello_test() {
final var rest = new TestRestTemplate();
final var response = rest.getForEntity("http://localhost:8080/hello?name={name}", String.class, "Spring");

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo("*hello Spring*");
assertThat(response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE)).startsWith(TEXT_PLAIN_VALUE);
}

@Test
void fail_test() {
final var rest = new TestRestTemplate();
final var response = rest.getForEntity("http://localhost:8080/hello?name=", String.class);

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.helloboot;

import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;


class HelloControllerTest {


@Test
void success_test() {
final var helloController = new HelloController(name -> name);
final var result = helloController.hello("spring");

Assertions.assertThat(result).isEqualTo("spring");
}

@Test
void fail_test() {
final var helloController = new HelloController(name -> null);

assertThatIllegalArgumentException().isThrownBy(() -> helloController.hello(null));
assertThatIllegalArgumentException().isThrownBy(() -> helloController.hello(""));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.helloboot;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

class HelloServiceTest {


@Test
void success_test() {
final var sampleHello = new SimpleHelloService();
final var result = sampleHello.sayHello("name");

Assertions.assertThat(result).isEqualTo("hello name");
}
}

This file was deleted.

55 changes: 55 additions & 0 deletions jaehong/helloboot/src/test/java/study/ConfigurationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package study;

import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class ConfigurationTest {

@Test
void configuration() {

}

@Configuration
static class MyConfig {
@Bean
Common common() {
return new Common();
}

@Bean
Bean1 bean1() {
return new Bean1(common());
}

@Bean
Bean2 bean2() {
return new Bean2(common());
}
}

static class Bean2 {
private final Common common;

Bean2(Common common) {
this.common = common;
}
}

static class Bean1 {
private final Common common;

Bean1(Common common) {
this.common = common;
}
}

static class Common {

}
// bean1 <-- common
// bean2 <-- common
// 스프링은 스프링컨테이너에 의해서 모든 빈들은 싱글턴으로 관리 되어지기 때문에 bean1과 bean2는 모두 동일한 common을 주입받습니다.

}