티스토리 뷰
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
생성자 주입
annotation
MsgProvider.java
package constructor.annotation;
import org.springframework.stereotype.Component;
@Component("provider")
public class MsgProvider {
private String msg = "Hello World!(Constructor Injection annotation)";
public String getMsg() {
return msg;
}
}
MsgRender.java
package constructor.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("render")
public class MsgRender {
private MsgProvider msgProvider;
@Autowired
public MsgRender(MsgProvider msgProvider) {
this.msgProvider = msgProvider;
}
public void render() {
System.out.println(msgProvider.getMsg());
}
}
ConstructorConfig.java
package constructor.annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan
@Configuration
public class ConstructorConfig {
}
Main.java
package constructor.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConstructorConfig.class);
MsgRender msgRender = ctx.getBean("render", MsgRender.class);
msgRender.render();
}
}
xml
MsgProvider.java
package constructor.xml;
public class MsgProvider {
private String msg = "Hello World!(Constructor Injection xml)";
public String getMsg() {
return msg;
}
}
MsgRender.java
package constructor.xml;
public class MsgRender {
private MsgProvider msgProvider;
public MsgRender(MsgProvider msgProvider) {
this.msgProvider = msgProvider;
}
public void render() {
System.out.println(msgProvider.getMsg());
}
}
constructor-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="provider"
class="constructor.xml.MsgProvider">
</bean>
<bean id="render"
class="constructor.xml.MsgRender">
<constructor-arg ref="provider"/>
</bean>
</beans>
Main.java
package constructor.xml;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:constructor-config.xml");
MsgRender msgRender = ctx.getBean("render", MsgRender.class);
msgRender.render();
}
}
수정자 주입
annotation
MsgProvider.java
package setter.annotation;
import org.springframework.stereotype.Component;
@Component("provider")
public class MsgProvider {
private String msg = "Hello World!(Setter Injection annotation)";
public String getMsg() {
return msg;
}
}
MsgRender.java
package setter.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("render")
public class MsgRender {
private MsgProvider msgProvider;
@Autowired
public void setMsgProvider(MsgProvider msgProvider) {
this.msgProvider = msgProvider;
}
public void render() {
System.out.println(msgProvider.getMsg());
}
}
SetterConfig.java
package setter.annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan
@Configuration
public class SetterConfig {
}
Main.java
package setter.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SetterConfig.class);
MsgRender render = ctx.getBean("render", MsgRender.class);
render.render();
}
}
xml
MsgProvider.java
package setter.xml;
public class MsgProvider {
private String msg = "Hello World!(Setter Injection xml)";
public String getMsg() {
return msg;
}
}
MsgRender.java
package setter.xml;
public class MsgRender {
private MsgProvider msgProvider;
public void setMsgProvider(MsgProvider msgProvider) {
this.msgProvider = msgProvider;
}
public void render() {
System.out.println(msgProvider.getMsg());
}
}
setter-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="provider"
class="setter.xml.MsgProvider"/>
<bean id="render"
class="setter.xml.MsgRender"
p:msgProvider-ref="provider">
</bean>
<bean id="render_property"
class="setter.xml.MsgRender">
<property name="msgProvider" ref="provider"/>
</bean>
</beans>
Main.java
package setter.xml;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:setter-config.xml");
MsgRender msgRender = ctx.getBean("render", MsgRender.class);
msgRender.render();
MsgRender msgRender2 = ctx.getBean("render_property", MsgRender.class);
msgRender2.render();
}
}
필드 주입
annotation
MsgProvider.java
package field.annotation;
import org.springframework.stereotype.Component;
@Component("provider")
public class MsgProvider {
private String msg = "Hello World!(Field Injection annotation)";
public String getMsg() {
return msg;
}
}
MsgRender.java
package field.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("render")
public class MsgRender {
@Autowired
private MsgProvider msgProvider;
public void render() {
System.out.println(msgProvider.getMsg());
}
}
FieldConfig.java
package field.annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan
@Configuration
public class FieldConfig {
}
Main.java
package field.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FieldConfig.class);
MsgRender msgRender = ctx.getBean("render", MsgRender.class);
msgRender.render();
}
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 리눅스
- command
- onetomany
- tkinter
- ManyToOne
- Composite Key
- Python
- borderwidth
- Excel
- highlightbackground
- Java
- Linux
- FetchType
- Private
- activeforeground
- Module
- indicatoron
- checkbutton
- 폼
- 상수
- fetch join
- activebackground
- vba
- highlightthickness
- apache
- JPA
- tkinter command & bind [명령어묶기와 사건묶기] Python
- 파이썬
- disabledforeground
- IdClass
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함