Wstrzykiwanie zależności w Spring - temat ogólnie znany ale ...
Wstrzykiwanie do konstruktora:
package com.blogspot.programmingmt.spring.injection;
import org.apache.camel.CamelContext;
import org.apache.log4j.Logger;
public class MyTest {
private static final Logger log = Logger.getLogger(MyTest.class);
public MyTest(String param1, CamelContext param2, Long param3) {
if(param1==null)
log.error("'param1' is NULL");
if(param2==null)
log.error("'param2' is NULL");
if(param3==null)
log.error("'param3' is NULL");
}
}
Konfiguracja spring:
<bean id="myTest" class="com.blogspot.programmingmt.spring.injection.MyTest">
<constructor-arg type="java.lang.String" value="..." />
<constructor-arg type="org.apache.camel.CamelContext" ref="myCamelContext" />
<constructor-arg type="java.lang.Long" value="60" />
</bean>
Wstrzykiwanie do zmiennych:
package com.blogspot.programmingmt.spring.injection;
import org.apache.camel.CamelContext;
import org.apache.log4j.Logger;
public class MyTest2 {
private static final Logger log = Logger
.getLogger(MyTest2.class);
private String param1;
private CamelContext param2;
private Long param3;
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
public CamelContext getParam2() {
return param2;
}
public void setParam2(CamelContext param2) {
this.param2 = param2;
}
public Long getParam3() {
return param3;
}
public void setParam3(Long param3) {
this.param3 = param3;
}
public void check() {
if(param1==null) log.error("'param1' is NULL");
if(param2==null) log.error("'param2' is NULL");
if(param3==null) log.error("'param3' is NULL");
}
}
Konfiguracja spring:
<bean id="myTest2" class="com.blogspot.programmingmt.spring.injection.MyTest2">
<property name="param1" value="..." />
<property name="param2" ref="myCamelContext" />
<property name="param3" value="60" />
</bean>
Brak komentarzy:
Prześlij komentarz