//bean配置 <bean id="haiDao" class="com.HaiDaoImpl" /> <bean id="haiService" class="com.HaiServiceImpl"> <property name="haiDao" ref="haiDao"/> <property name="str" value="the string"></property> </bean> //HaiService 类 public interface HaiService { public abstract String getResult(); } public class HaiServiceImpl implements HaiService { private String str; public void setStr(String str) { this.str = str; } public String getResult() { return haiDao.getResult(); } } public class TestHaiServive extends TestCase { public void getResult() { // 可行方法。也只有先得到context然后通过getBean的方式才能 //得到bean里面注入的属性。没有context就得不到注入的bean,这一点不同意javaEE ApplicationContext context = new FileSystemXmlApplicationContext( "src/applicationContext.xml"); // HaiService service=(HaiService) context.getBean("haiService"); HaiService service=(HaiService) context.getBean(HaiService.class); System.out.println(service.getResult()); this.haiDao.getResult(); } }
转载于:https://blog.51cto.com/8266677/1404487