IoC
Last updated
Was this helpful?
Last updated
Was this helpful?
@Component
@Scope(
value="prototype",
// proxyMode=ScopedProxyMode.DEFAULT
// proxyMode=ScopedProxyMode.INTERFACES
/* cglib 의 dynamic proxy 가 적용
* proxy 를 거쳐서 참조하도록 함.
* jdk 의 dynamic proxy 는 interface 기반인데
* cglib 는 class 도 proxy 가 가능하게 해준다.
* 위의 interfaces 를 사용하면 jdk 기반의 proxy 를 사용
*/
proxyMode=ScopedProxyMode.TARGET_CLASS
)
public class Proto {
}
@Component
public class Single {
@Autowired
private Proto proto;
public Proto getProto() {
return this.proto;
}
}
//Single class 가 singleton 이더라도 proto
두가지 기능 1. Profile : 환경, 빈들의 집합 2. Properties : key-vaule 쌍에 접근, 계층형
vm option -Dspring.profiles.active=test
@Configuration
@Profile("test")
public class TestConfiguration {
@Bean
//@Profile("test") //이렇게 해도
public BookRepository bookRepository() {
return new TestBookRepository();
}
}
/* test 프로파일은 안줘도 되는건 기분탓..? */
vm option -Dapp.name=Spring5
package herdin.boot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:/app.properties")
public class MainApp {
@Autowired
ApplicationContext ctx;
public static void main(String[] args) {
SpringApplication.run(MainApp.class);
}
}
package herdin.boot;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
ApplicationContext ctx;
@Override
public void run(ApplicationArguments args) throws Exception {
Environment environment = ctx.getEnvironment();
System.out.println(Arrays.deepToString(environment.getDefaultProfiles()));
System.out.println(environment.getProperty("app.name"));
System.out.println(environment.getProperty("app.about"));
}
}
app.about=thisIsAbout
org.springframework.context.MessageSource
package herdin.boot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class);
}
/* MessageSource 를 다른 종류로 변경해 보았음 */
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(3);
return messageSource;
}
}
package herdin.boot;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
// MessageSource messageSource; //이거나
ApplicationContext messageSource; //이거나 같음
@Override
public void run(ApplicationArguments args) throws Exception {
while(true) {
System.out.println(messageSource.getClass());
System.out.println(messageSource.getMessage("greeting", new String[] {"target"}, Locale.getDefault()));
System.out.println(messageSource.getMessage("greeting", new String[] {"target"}, Locale.KOREA));
Thread.sleep(1000L);
}
}
}
greeting=안녕 {0}
greeting=Hello {0}
before Spring 4.2
package herdin.boot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import herdin.boot.event.MyEvent;
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
// ApplicationContext applicationContext;
ApplicationEventPublisher applicationEventPublisher;
@Override
public void run(ApplicationArguments args) throws Exception {
applicationEventPublisher.publishEvent(new MyEvent(this, 1004));
}
}
package herdin.boot.event;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyEventHandler implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
System.out.println("EVENT RECV : " + event.getData());
}
}
package herdin.boot.event;
import org.springframework.context.ApplicationEvent;
public class MyEvent extends ApplicationEvent {
private int data;
public MyEvent(Object source) {
super(source);
}
public MyEvent(Object source, int data) {
super(source);
this.data = data;
}
public int getData() {
return this.data;
}
}
after 4.2
package herdin.boot.event;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class MyEventHandler /*implements ApplicationListener<MyEvent> 구현할 필요가 없어짐*/ {
@EventListener /*Spring annotation 추가*/
public void onApplicationEventCustom(MyEvent event) { /*함수명도 아무렇게나*/
System.out.println("EVENT RECV : " + event.getData());
}
}
package herdin.boot.event;
public class MyEvent /*implements ApplicationEvent 구현 할 필요가 없어짐*/ {
private Object source;
private int data;
public MyEvent(Object source) {
this.source = source;
}
public MyEvent(Object source, int data) {
this.source = source;
this.data = data;
}
public Object getSource() {
return this.source;
}
public int getData() {
return this.data;
}
}
위에서 같은 이벤트를 처리하는 핸들러가 추가되면 이벤트가 발생(publish)했을때 모두 수행된다 이벤트 리스너에서 @Order(int) 로 순서 조정 가능
Ordered.HIGHEST_PRECEDENCE
Ordered.LOWEST_PRECEDENCE
package herdin.boot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Autowired
// ApplicationContext resourceLoader;
ResourceLoader resourceLoader;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(resourceLoader.getResource("classpath:/messages.properties").getFile().getCanonicalPath());
Thread.sleep(2000L);
System.exit(0);
}
}