| 主题:Spring中遇到问题,请高手解答 |
|---|
|
gy93108083
|
|
|
最近用Spring遇到一个问题
<beans> <bean name="mydao" class="com.app.dao.DAO"></bean> <bean name="myservice" class="com.app.service.Service"></bean> </beans> package com.app.dao; public class DAO { public DAO() { super(); System.out.println("create dao !!!!!"); } } package com.app.service; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.app.dao.DAO; public class Service { private static ApplicationContext context = ; static{ new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("after create context!!! "); } public Service() { super(); System.out.println("create Serice!!!!"); } public void UseService(){ System.out.println("before get dao!!!"); DAO dao= (DAO)context.getBean("mydao"); System.out.println("after get dao! "); } } package com.app.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); } } 这样的代码运行没有问题 但是,如果把service里改成 public class Service { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); public Service() { super(); System.out.println("create Serice!!!!"); } public void UseService(){ System.out.println("before get dao!!!"); DAO dao= (DAO)context.getBean("mydao"); System.out.println("after get dao! "); } } 就会出现死循环,请问这是什么原因导致的?????????????????????????? |
|
|
|
|
|
amen
|
|
|
不明白你这程序有什么用?Test那里又没有调用到其它方法,这有什么意义呢?
发帖时麻烦把结果也发上来看看。。 |
|
粤ICP备07511478号