1.新建一个Demo实体类;
packagecom.eduask.entity;
publicclassDemo4 {
publicDemo4(){
System.out.println("测试三种获得对象方式");
}
}
- 新建一个TestDemo测试类;
packagecom.eduask.entity;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassTestDemo4 {
publicstaticvoidmain(String[] args) {
ClassPathXmlApplicationContext cx=newClassPathXmlApplicationContext("demo4.xml");
Demo4 demo41=(Demo4) cx.getBean("Demo4");
Demo4 demo42=(Demo4) cx.getBean(Demo4.class);
Demo4 demo43=(Demo4) cx.getBean("Demo4",Demo4.class);
System.out.println(demo41);
System.out.println(demo42);
System.out.println(demo43);
}
}
- 新建一个xml配置文件;
<?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="Demo4"class="com.eduask.entity.Demo4"></bean>
</beans>