Spring提供两种IOC容器实现类型。基本的一种是Bean工厂(Bean Factory)。更高级的是称为应用程序上下文(Application context),是对Bean工厂的一种兼容的扩展。应用程序上下文提供比Bean工厂更高级的特性,同时保持基本特性的兼容。除非是资源有限的应用程序(例如运行一个小脚本或移动设备),否则最好使用Application context。Bean工厂和Application context 的接口分别是BeanFactory和ApplicationContext。ApplicationContext是BeanFactory的子接口。
ApplicationContext只是个接口,必须实例化一个接口的实现可以使用以下的一些实现类:
1、ClassPathXmlApplicationContext:实现装载一个xml配置文件,构建一个应用程序上下文。
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
2、FileSystemXmlApplicationContext:用于从文件系统或URL装载xml配置文件。
3、XmlWebApplicationContext和XmlPortletApplicationContext:仅能用于Web和入口应用程序。
为了从ApplicationContext中得到已声明的bean,只需要调用getBean()方法,并且传入唯一的bean名称。返回的是Object类型,所有要进行类型转换
User user=(User)ac.getBean("user");
到这里就可以获取到Bean实例了
package com.lkt.entity;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Student student=(Student)ac.getBean("studentBean");
System.out.println(student.toString());
}
}
Spring可以使用xml文件,属性文件,注解来配置Spring Ioc容器中的Bean。Spring允许在一个或在多个bean配置文件中配置bean。
首先创建Bean类,即实体类
package com.lkt.entity;
public class Student {
private String userName;
private String password;
private String realName;
private Clazz clazz;
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
public Student() {
}
public Student(String userName,String password,String realName) {
this.userName=userName;
this.password=password;
this.realName=realName;
}
@Override
public String toString() {
return "userName:"+userName+" password:"+password+" realName:"+realName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
}
创建Bean配置文件
使用xml方式,名为applicationContext.xml在classpath根路径下
<?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-2.5.xsd
">
<!-- 配置一个Bean -->
<bean name="studentBean" class="com.lkt.entity.Student" scope="prototype">
<!-- 配置属性 -->
<property name="userName" value="zhangsan"></property>
<property name="password" value="zhangsan123"></property>
<property name="realName" value="张三"></property>
<!-- 配置构造函数 -->
<constructor-arg value="zhangsans"/>
<constructor-arg value="zhangsan123"/>
<constructor-arg value="张三"/>
</bean>
</beans>
同时,还有个更简写的方法,即p schema
需要添加xmlns:p="http://www.springframework.org/schema/p"的声明
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
<!-- 配置一个Bean -->
<bean name="studentBean" class="com.lkt.entity.Student"
p:password="lisi"
p:userName="lisi"
p:realName="lisi" scope="prototype"/>
</beans>
在类中引用别的类
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
<!-- 配置一个Bean -->
<bean name="studentBean" class="com.lkt.entity.Student"
p:password="lisi"
p:userName="lisi"
p:realName="lisi"
p:clazz-ref="classBean"
scope="prototype"/>
<bean name="classBean" class="com.lkt.entity.Clazz" scope="prototype">
<property name="className" value="一年三班"></property>
</bean>
</beans>