package com.jeestudy.spring.bean;
public class Person {
public String name;
public int age;
public String address;
public String phone;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address + ", phone=" + phone + "]";
}
}
<bean id="person" class="com.jeestudy.spring.bean.Person"></bean>
package com.jeestudy.spring.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author www.jeestudy.com
*
*/
public class Test {
public static void main(String[] args) {
//创建Spring的IOC容器
//ClassPathXmlApplicationContext : 从类路径下读取配置文件
//ApplicationContext.xml 为Spring配置文件名称
ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//从IOC容器中获取bean 根据bean的id获取
Person person= (Person)ctx.getBean("person");
System.out.println(person);
}
}
Person person2 = (Person)ctx.getBean(Person.class);
//创建IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//从容器中获取bean 根据bean的id获取
Person person1 = (Person)ctx.getBean("person");
//从容器中获取bean 根据bean的类型获取
Person person2 = (Person)ctx.getBean(Person.class);
欢迎光临 JEE Study|JAVA EE|企业级开发学习网 (http://jeestudy.com/) | Powered by Discuz! X3.2 |