package com.company.design.ioc;
public class Main {
public static void main(String[] args) {
String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";
// Base 64 encoding
/*
Encoder encoder = new Encoder();
String result = encoder.encode(url);
System.out.println(result);
*/
// url encoding
UrlEncoder urlEncoder = new UrlEncoder();
String urlResult = urlEncoder.encode(url);
System.out.println(urlResult);
}
}
package com.company.design.ioc;
import java.util.Base64;
public class Encoder {
public String encode(String message) {
return Base64.getEncoder().encodeToString(message.getBytes());
}
}
package com.company.design.ioc;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class UrlEncoder {
public String encode(String message) {
try {
return URLEncoder.encode(message, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
package com.company.design.ioc;
public interface IEncoder {
String encode(String message);
}
package com.company.design.ioc;
import java.util.Base64;
public class Base64Encoder implements IEncoder{
public String encode(String message) {
return Base64.getEncoder().encodeToString(message.getBytes());
}
}
package com.company.design.ioc;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class UrlEncoder implements IEncoder{
public String encode(String message) {
try {
return URLEncoder.encode(message, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
package com.company.design.ioc;
public class Main {
public static void main(String[] args) {
String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";
// Base 64 encoding
IEncoder base64Encoder = new Base64Encoder();
String base64Result = base64Encoder.encode(url);
System.out.println(base64Result);
// url encoding
IEncoder urlEncoder = new UrlEncoder();
String urlResult = urlEncoder.encode(url);
System.out.println(urlResult);
}
}
package com.company.design.ioc;
import java.util.Base64;
public class Encoder{
private IEncoder iEncoder;
public Encoder() {
this.iEncoder = new Base64Encoder();
//this.iEncoder = new UrlEncoder();
}
public String encode(String message) {
return iEncoder.encode(message);
}
}
package com.company.design.ioc;
public class Main {
public static void main(String[] args) {
String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";
Encoder encoder = new Encoder();
String result = encoder.encode(url);
System.out.println(result);
}
}
package com.company.design.ioc;
public class Encoder{
private IEncoder iEncoder;
public Encoder(IEncoder iEncoder) {
this.iEncoder = iEncoder;
}
public String encode(String message) {
return iEncoder.encode(message);
}
}
package com.company.design.ioc;
public class Main {
public static void main(String[] args) {
String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";
Encoder encoder = new Encoder(new Base64Encoder());
String result = encoder.encode(url);
System.out.println(result);
}
}
package com.example.springioc;
import org.springframework.stereotype.Component;
import java.util.Base64;
// bean으로 등록된다
// SpringIocApplication의 @SpringBootApplication에서 등록된다
@Component
public class Base64Encoder implements IEncoder{
public String encode(String message) {
return Base64.getEncoder().encodeToString(message.getBytes());
}
}
package com.example.springioc;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@Component
public class UrlEncoder implements IEncoder{
public String encode(String message) {
try {
return URLEncoder.encode(message, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
package com.example.springioc;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static ApplicationContext getContext() {
return context;
}
}
package com.example.springioc;
public class Encoder{
private IEncoder iEncoder;
public Encoder(IEncoder iEncoder) {
this.iEncoder = iEncoder;
}
public String encode(String message) {
return iEncoder.encode(message);
}
public void setIEncoder(IEncoder iEncoder) {
this.iEncoder = iEncoder;
}
}
package com.example.springioc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class SpringIocApplication {
public static void main(String[] args) {
SpringApplication.run(SpringIocApplication.class, args);
ApplicationContext context = ApplicationContextProvider.getContext();
// Bean 찾는 법
// context에 받아온 class type으로 Bean 찾기기
Base64Encoder base64Encoder = context.getBean(Base64Encoder.class);
UrlEncoder urlEncoder = context.getBean(UrlEncoder.class);
Encoder encoder = new Encoder(base64Encoder);
String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";
String result = encoder.encode(url);
System.out.println(result);
encoder.setIEncoder(urlEncoder);
result = encoder.encode(url);
System.out.println(result);
}
}
package com.example.springioc;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class Encoder{
private IEncoder iEncoder;
// @Qualifier("Base64Encoder")
public Encoder(@Qualifier("base74Encoder") IEncoder iEncoder) {
this.iEncoder = iEncoder;
}
public String encode(String message) {
return iEncoder.encode(message);
}
public void setIEncoder(IEncoder iEncoder) {
this.iEncoder = iEncoder;
}
}
package com.example.springioc;
import org.springframework.stereotype.Component;
import java.util.Base64;
// bean으로 등록된다
// SpringIocApplication의 @SpringBootApplication에서 등록된다
@Component("base74Encoder")
public class Base64Encoder implements IEncoder{
public String encode(String message) {
return Base64.getEncoder().encodeToString(message.getBytes());
}
}
package com.example.springioc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class SpringIocApplication {
public static void main(String[] args) {
SpringApplication.run(SpringIocApplication.class, args);
ApplicationContext context = ApplicationContextProvider.getContext();
// Bean 찾는 법
Encoder encoder = context.getBean(Encoder.class);
String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";
String result = encoder.encode(url);
System.out.println(result);
}
}
package com.example.springioc;
public class Encoder{
private IEncoder iEncoder;
public Encoder(IEncoder iEncoder) {
this.iEncoder = iEncoder;
}
public String encode(String message) {
return iEncoder.encode(message);
}
public void setIEncoder(IEncoder iEncoder) {
this.iEncoder = iEncoder;
}
}
package com.example.springioc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
public class SpringIocApplication {
public static void main(String[] args) {
SpringApplication.run(SpringIocApplication.class, args);
ApplicationContext context = ApplicationContextProvider.getContext();
// Bean 찾는 법
// context에 받아온 class type으로 Bean 찾기기
// Encoder encoder = context.getBean(Encoder.class);
// Bean 이름으로 찾기
Encoder encoder = context.getBean("urlEncode",Encoder.class);
String url = "www.naver.com/books/it?page=10&size=20&name=spring-boot";
String result = encoder.encode(url);
System.out.println(result);
}
}
// Bean으로 등록
// @Configuration 한개의 클래스에서 여러개의 빈을 등록하는 어노테이션
@Configuration
class AppConfig {
// 이름을 동일하게 해두면 안된다
@Bean("base64Encode")
public Encoder encoder(Base64Encoder base64Encoder) {
return new Encoder(base64Encoder);
}
// 이름을 동일하게 해두면 안된다
@Bean("urlEncode")
public Encoder encoder(UrlEncoder urlEncoder) {
return new Encoder(urlEncoder);
}
}