public interface HutaoDao {
public void save();
}
public class HutaoDaoImpl implements HutaoDao{
private HutaoDao hutaoDao = new HutaoDaoImpl();
public void save(){
System.out.println("打雷啦,起身啦");
hutaoDao.save();
}
}
public interface HutaoService {
public void save();
}
public class HutaoServiceImpl implements HutaoService {
public void save(){
System.out.println("再会啦");
}
}
xml配置:
"1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hutaoDao" class="com.mihoyo.dao.impl.HutaoDaoImpl"/>
<bean id="hutaoService" class="com.mihoyo.dao.impl.service.impl.HutaoServiceImpl"/>
beans>
pom配置:
"1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.mihoyogroupId>
<artifactId>Spring---yuanshen1artifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.3.22version>
dependency>
dependencies>
project>
你这两行代码有问题
public class HutaoDaoImpl implements HutaoDao{
private HutaoDao hutaoDao = new HutaoDaoImpl();
怎么能自己new自己,然后再自己引用自己再new自己呢,这样无限循环导致堆栈溢出了哇
改成这样:
public interface HutaoDao {
public void save();
}
public class HutaoDaoImpl implements HutaoDao{
public void save(){
System.out.println("打雷啦,起身啦");
}
}
然后在你需要调用的地方去new,然后调用
HutaoDao hutaoDao = new HutaoDaoImpl();
hutaoDao.save();
Bean 注入失败了,DeBug下代码看看