有人会问 下面 的 dao 以及 dao2 的地址 是否是相同?
我们直接实验一下
<?xml version="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="dao" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="dao2" class="com.itheima.dao.impl.BookDaoImpl"></bean>
</beans>
测试
public class AppForDIAutoware {
public static void main( String[] args ) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
BookDaoImpl dao = (BookDaoImpl)ctx.getBean("dao");
BookDaoImpl dao2 = (BookDaoImpl)ctx.getBean("dao2");
System.out.println(dao );
System.out.println(dao2);
}
}
运行结果
com.itheima.dao.impl.BookDaoImpl@343f4d3d
com.itheima.dao.impl.BookDaoImpl@53b32d7
总结
从上面的运行结果, 相信大家可以知道两个对象的地址是不一样的