コンストラクタ・ベースのDIは、コンテナがクラス・コンストラクタを一連のパラメータで呼び出したときに完了します。
スペルチェッカー.java
public class spellChecker {
public spellChecker(){
System.out.println("spellcheck init");
}
public void check(){
System.out.println(this.getClass().getName());
System.out.println("spellchecker check");
}
}
textEditor.java
public class textEditor {
private spellChecker checker;
public textEditor(spellChecker checker){
System.out.println("textEditor init");
this.checker = checker;
}
public void check(){
System.out.println(this.getClass().getName());
this.checker.check();
}
}
springconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://..//ns"
xmlns:xsi="http://..//-ce"
xsi:schemaLocation="http://..// http://..///-.sd">
<bean id="spellChecker" class="spellChecker"></bean>
<bean id="textEditor" class="textEditor">
<constructor-arg ref="spellChecker"></constructor-arg>
</bean>
</beans>
spellchecke.java
public class spellChecker {
public spellChecker(){
System.out.println("spellcheck init");
}
public void check(){
System.out.println(this.getClass().getName());
System.out.println("spellchecker check");
}
}
texteditor.java
public class textEditor {
private spellChecker checker;
public textEditor(){
System.out.println("textEditor init");
}
public void setChecker(spellChecker checker){
this.checker = checker;
}
public void check(){
System.out.println(this.getClass().getName());
this.checker.check();
}
}
springconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://..//ns"
xmlns:xsi="http://..//-ce"
xsi:schemaLocation="http://..// http://..///-.sd">
<bean id="spellChecker" class="spellChecker"></bean>
<bean id="textEditor" class="textEditor">
<property name="checker" ref="spellChecker"></property>
</bean>
</beans>
Javaの内部クラスは、他のクラスのスコープ内で定義され、同様に、内部Beanは、他のBeanのスコープ内で定義されたBeanです。したがって、or要素内の要素は、内部Beanと呼ばれます。
spellcheck.java
public class spellChecker {
public spellChecker(){
System.out.println("inner spellcheck init");
}
public void check(){
System.out.println(this.getClass().getName());
System.out.println("inner spellchecker check");
}
}
textEditor.java
public class textEditor {
private spellChecker spellChecker;
public textEditor(){
System.out.println("textEditor init");
}
public void setSpellChecker(spellChecker spellChecker) {
this.spellChecker = spellChecker;
}
public spellChecker getChecker() {
return spellChecker;
}
public void callcheck(){
System.out.println(this.getClass().getName());
this.spellChecker.check();
}
}
springconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://..//ns"
xmlns:xsi="http://..//-ce"
xsi:schemaLocation="http://..// http://..///-.sd">
<bean id="spellChecker" class="spellChecker"></bean>
<bean id="textEditor" class="textEditor">
<property name="spellChecker">
<bean id="spellChecker" class="spellChecker"></bean>
</property>
</bean>
</beans>
試験
javacollection.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class javacollection {
private List addlist;
private Set addset;
private Map addmap;
private Properties addprop;
public void setAddlist(List addlist) {
this.addlist = addlist;
}
public List getAddlist() {
return addlist;
}
public void setAddset(Set addset) {
this.addset = addset;
}
public Set getAddset() {
return addset;
}
public void setAddmap(Map addmap) {
this.addmap = addmap;
}
public Map getAddmap() {
return addmap;
}
public void setAddprop(Properties addprop) {
this.addprop = addprop;
}
public Properties getAddprop() {
return addprop;
}
}
springconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://..//ns"
xmlns:xsi="http://..//-ce"
xsi:schemaLocation="http://..// http://..///-.sd">
<bean id="javacollection" class="javacollection">
<property name="addlist">
<list>
<value>india</value>
<value>japan</value>
<value>Japan</value>
<value>pekke</value>
</list>
</property>
<property name="addset">
<set>
<value>india</value>
<value>japan</value>
<value>Japan</value>
<value>pekke</value>
</set>
</property>
<property name="addmap">
<map>
<entry key="1" value="india"></entry>
<entry key="2" value="japan"></entry>
<entry key="3" value="Japan"></entry>
<entry key="4" value="pekke"></entry>
</map>
</property>
<property name="addprop">
<props>
<prop key="one">india</prop>
<prop key="two">japan</prop>
<prop key="three">Japan</prop>
<prop key="four">pekke</prop>
</props>
</property>
</bean>
</beans>
springconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://..//ns"
xmlns:xsi="http://..//-ce"
xsi:schemaLocation="http://..// http://..///-.sd">
<bean id="javacollection" class="javacollection">
<property name="addlist">
<list>
<ref bean=".."></ref>
<ref bean=".."></ref>
<value>Japan</value>
<value>pekke</value>
</list>
</property>
<property name="addset">
<set>
<ref bean=".."></ref>
<ref bean=".."></ref>
<value>Japan</value>
<value>pekke</value>
</set>
</property>
<property name="addmap">
<map>
<entry key="1" value-ref=".."></entry>
<entry key="2" value-ref=".."></entry>
<entry key="3" value="Japan"></entry>
<entry key="4" value="pekke"></entry>
</map>
</property>
</bean>
</beans>
究極
ここで見ていただきありがとうございます、あなたが理解できないものを読んだ後、コメント欄で私に尋ねることができ、記事は私に承認のうなずきを与えることを忘れないであなたに役立つと思いますが、毎日Java関連の技術記事や業界情報を共有し、記事に注意を払うと転送することを歓迎します!





