Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

@Component
public class AutowiredBean {
/*
어떤 방법으로 Component에 Bean을 주입하는지 학습하기
*/
private SpringBean springBean;
@Autowired
private SpringBean springBean;

public String sayHello() {
return springBean.hello();
}
public String sayHello() {
return springBean.hello();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
/*
어떤 어노테이션을 붙였을 때 Bean으로 생성되는지 학습하기
*/
@Component
public class SpringBean {
public String hello() {
return "Hello";
}
public String hello() {
return "Hello";
}
}
Comment on lines +8 to 13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금 보면서 생각이 들었는데, @Component 를 붙인 클래스와 @Bean 을 붙여서 생성한 스프링 빈의 차이점은 무엇일까요? 🤔

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

@Service
public class ConstructorInjection {
private InjectionBean injectionBean;

/*
ConstructorInjection으로 InjectionBean 주입받기
*/
public String sayHello() {
return injectionBean.hello();
}
private InjectionBean injectionBean;

public ConstructorInjection(InjectionBean injectionBean) {
this.injectionBean = injectionBean;
}

public String sayHello() {
return injectionBean.hello();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

@Service
public class FieldInjection {
@Autowired
private InjectionBean injectionBean;

/*
FieldInjection으로 InjectionBean 주입받기
*/
private InjectionBean injectionBean;

public String sayHello() {
return injectionBean.hello();
}
public String sayHello() {
return injectionBean.hello();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

@Service
public class SetterInjection {
private InjectionBean injectionBean;
private InjectionBean injectionBean;

/*
Setter Injection으로 InjectionBean 주입받기
*/
@Autowired
public void setSetterInjection(InjectionBean injectionBean) {
this.injectionBean = injectionBean;
}

public String sayHello() {
return injectionBean.hello();
}
public String sayHello() {
return injectionBean.hello();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cholog.scan;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
/*
ComponentScan에 대해 학습하고, ComponenetScanBean을 Bean으로 등록하기
*/
@ComponentScan(basePackages = {"cholog.scan"})
public class ContextConfiguration {
}