当ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");执行的时候,
我们的spring容器对象被创建,同时applicationContext.xml中配置bean就会被创建(),基于java的
反射机制
内存(applicationContext对象引用)[结构类型HashMap]
id 对象
userService(ox123) UserSer[name, byeService]
2.UserService us =(UserService) ac.getBean("userService");
java反射机制的深入了解(dom4j+java反射机制)
userService =Class.forName("com.service.UserService");
userService.setName("zhujingao");
bybService=Class.forName("com.service.BybService");
bybService.setName("xiaoming");
userService.setByeService("bybService");
applicationContext.put("userService",userService);
applicationContext.put("bybService",bybService);
package com.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main (String [] args){
//用传统方法调用Userservice的sayhello方法
/*Userservice userservice =new Userservice();
userservice.setName("朱敬傲");
userservice.sayHello();
*/
//现在使用spring来完成上面的任务
//1.现在得到spring的applicationContext对象(容器对象)
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Userservice us =(Userservice) ac.getBean("application");
us.sayHello();
}
}
package com.service;
public class Userservice {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void sayHello(){
System.out.println("sayHello");
}
}