study's Introduction
study's People
study's Issues
Spring, Dependency Injection
생성자 주입
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();
}
}
close_test3
close_test
close_test
close_test2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.