您好,登錄后才能下訂單哦!
在Spring框架中,方法注入是通過使用
public interface DataService {
public String getData();
}
public class DataServiceBean implements DataService {
public String getData() {
return "Data from DataServiceBean";
}
}
public abstract class MyService {
public void process() {
System.out.println(getDataService().getData());
}
public abstract DataService getDataService();
}
public class MyServiceBean extends MyService {
private DataService dataService;
public void setDataService(DataService dataService) {
this.dataService = dataService;
}
@Override
public DataService getDataService() {
return dataService;
}
}
<bean id="dataService" class="com.example.DataServiceBean" />
<bean id="myService" class="com.example.MyServiceBean">
<lookup-method name="getDataService" bean="dataService" />
</bean>
在這個配置文件中,
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
MyService myService = (MyService) context.getBean("myService");
myService.process();
}
}
通過上面的步驟,就可以在Spring框架中使用方法注入來注入依賴對象。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。