目录
bean的两种创建规则:applicationcontext 和BeanFactory
spring环境搭建
spring各版本下载地址:https://repo.spring.io/release/org/springframework/spring/
创建项目,导入jar包
创建xml文件:
xml头文件从解压包spring-framework-4.2.4.RELEASE-dist/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html复制.
bean的两种创建规则:applicationcontext 和BeanFactory
applicationcontext属于立即加载,在加载配置文件的时候就创建了各个对象
BeanFactory,属于延时加载,是在获取对象的时候创建。
获取的方法:
package cn.pro.Ui;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import cn.pro.dao.ICustormerDao;
import cn.pro.service.CustormerService;
public class Client {
/**spring的入门案例
* @param args
*
* ClassPathXmlApplicationContext 可加载类路径下的配置文件
* FileSystemXmlApplicationContext 能够加载磁盘上的配置文件,范围比较大,但是不建议使用
*/
/*public static void main(String[] args) {
// TODO Auto-generated method stub
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据容器id获取对象
CustormerService cs = (CustormerService) ac.getBean("custormerService");
ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
cs.saveCustormer();
}*/
@SuppressWarnings("all")
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.获取容器
Resource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
//2.根据容器id获取对象
CustormerService cs = (CustormerService) factory.getBean("custormerService");
//ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
cs.saveCustormer();
}
}
bean的三种实例化方式:
* 第一种方式:调用默认无参构造函数创建 此种方式应用的最多!!
* 默认情况下,如果类中没有默认无参构造函数,则创建失败。
*
* 第二种方式:使用静态工厂中的方法创建对象:
* 需要使用bean标签的factory-method属性,指定工厂中创建对象的方法。
*
* 第三种方式:使用实例工厂中的方法创建
使用静态工厂中的方法创建对象小案例
cn.pro.factory.StaticFactory
package cn.pro.factory;
import cn.pro.service.CustormerService;
import cn.pro.service.impl.CustormerServiceimpl;
//模拟一个静态工厂
public class StaticFactory {
public static CustormerService getCustormerService(){
return new CustormerServiceimpl();
}
}
xml文件中配置:
<!-- 配置使用静态工厂创建bean对象 -->
<bean id="staticcustormerservice" class="cn.pro.factory.StaticFactory" factory-method="getCustormerService"></bean>
cn.pro.Ui.Client
package cn.pro.Ui;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import cn.pro.dao.ICustormerDao;
import cn.pro.service.CustormerService;
public class Client {
/**spring的入门案例
* @param args
*
* ClassPathXmlApplicationContext 可加载类路径下的配置文件
* FileSystemXmlApplicationContext 能够加载磁盘上的配置文件,范围比较大,但是不建议使用
*
*
* Bean的三种创建方式
* 第一种方式:调用默认无参构造函数创建 此种方式应用的最多!!
* 默认情况下,如果类中没有默认无参构造函数,则创建失败。
*
* 第二种方式:使用静态工厂中的方法创建对象:
* 需要使用bean标签的factory-method属性,指定工厂中创建对象的方法。
*
* 第三种方式:使用实例工厂中的方法创建
*/
@SuppressWarnings("all")
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.获取容器
Resource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
//2.根据容器id获取对象
CustormerService cs = (CustormerService) factory.getBean("staticcustormerservice");
//CustormerService cs = (CustormerService) factory.getBean("Instancecustormerservice");
//ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
cs.saveCustormer();
}
}
使用实例工厂中的方法创建
cn.pro.factory.InstanceFactory
package cn.pro.factory;
import cn.pro.service.CustormerService;
import cn.pro.service.impl.CustormerServiceimpl;
//模拟一个实例工厂
public class InstanceFactory {
public CustormerService getCustormerService(){
return new CustormerServiceimpl();
}
}
bean|.xml
<!-- 使用实例工厂创建bean对象 -->
<bean id="instanceFactory" class="cn.pro.factory.InstanceFactory"></bean>
<bean id="Instancecustormerservice" factory-bean="instanceFactory" factory-method="getCustormerService"></bean>
获取bean
Resource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
//2.根据容器id获取对象
CustormerService cs = (CustormerService) factory.getBean("Instancecustormerservice");
//ICustormerDao ia = (ICustormerDao) ac.getBean("icustormerDao");
cs.saveCustormer();