主要内容:
Spring的工厂类:
测试代码:
package com.imooc.ioc.demo1;
import org.junit.Test;
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.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
public class SpringDemo1 {
@Test
/**
* 传统方式开发
*/
public void demo1(){
//UserService userService = new UserServiceImpl();
UserServiceImpl userService = new UserServiceImpl();
// 设置属性:
userService.setName("张三");
userService.sayHello();
}
@Test
/*
Spring的方式实现
使用ClassPathXmlApplicationContext,从本工程的路径读取配置文件
*/
public void demo2(){
//创建Spring的工厂
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类:
UserService userService=(UserService) applicationContext.getBean("userService");
userService.sayHello();
}
@Test
/*
Spring的方式实现
使用FileSystemXmlApplicationContext,从别的位置读取配置文件
*/
public void demo3(){
//创建Spring的工厂
ApplicationContext applicationContext=new FileSystemXmlApplicationContext("//Users//lcs//Downloads//applicationContext.xml");
//通过工厂获得类:
UserService userService=(UserService) applicationContext.getBean("userService");
userService.sayHello();
}
@Test
/*
传统的方式实现
使用BeanFactory的ClassPathResource
*/
public void demo4(){
//创建Spring的工厂
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//通过工厂获得类:
UserService userService=(UserService) beanFactory.getBean("userService");
userService.sayHello();
}
@Test
/*
传统的方式实现
使用Beanactory的FileSystemResource
*/
public void demo5(){
//创建Spring的工厂
BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("//Users//lcs//Downloads//applicationContext.xml"));
//通过工厂获得类:
UserService userService=(UserService) beanFactory.getBean("userService");
userService.sayHello();
}
}
输出结果:
Hello Spring 李四
Hello Spring 张三
Hello Spring 王五
Hello Spring 王五
Hello Spring 李四
Bean的实例化三种方式:
三种配置方式在xml文件:
<!-- Bean的实例化的三种方式-->
<!-- 第1种:无参构造器的方式-->
<bean id="bean1" class="com.imooc.ioc.demo2.Bean1"></bean>
<!-- 第2种:静态工厂的方式-->
<bean id="bean2" class="com.imooc.ioc.demo2.Bean2Factory" factory-method="createBean2"></bean>
<!-- 第3种:实例工厂的方式-->
<bean id="bean3Factory" class="com.imooc.ioc.demo2.Bean3Factory"/>
<bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"/>
第一种:使用无参构造(默认)
package com.imooc.ioc.demo2;
/**
* Bean的三种实例化方式:采用无参数的构造方法的方式
*/
public class Bean1 {
public Bean1(){
System.out.println("Bean1被初始化了");
}
}
第二种:采用静态工厂的构造方法的方式
package com.imooc.ioc.demo2;
/**
* Bean的三种实例化方式:采用静态工厂的构造方法的方式
*/
public class Bean2 {
}
package com.imooc.ioc.demo2;
/**
* Bean2的静态工厂
*/
public class Bean2Factory {
public static Bean2 createBean2(){
System.out.println("Bean2Factory的静态方法已经执行了...");
return new Bean2();
}
}
第三种:采用实例工厂的方式
package com.imooc.ioc.demo2;
/**
* Bean的三种实例化方式:采用实例工厂的方式
*/
public class Bean3 {
}
package com.imooc.ioc.demo2;
/**
* Bean3的实例工厂
*/
public class Bean3Factory {
public Bean3 createBean3(){
System.out.println("Bean3Factory已经执行了...");
return new Bean3();
}
}
测试代码:
package com.imooc.ioc.demo2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
* Bean实例化的三种方式
*/
public class SpringDemo2 {
@Test
//第一种:无参构造
public void demo1(){
//创建工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类实例
Bean1 bean1 = (Bean1)applicationContext.getBean("bean1");
}
@Test
//第二种:静态工厂
public void demo2(){
//创建工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类实例
Bean2 bean2 = (Bean2)applicationContext.getBean("bean2");
}
@Test
//第二种:实例工厂
public void demo3(){
//创建工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类实例
Bean3 bean3 = (Bean3)applicationContext.getBean("bean3");
}
}
输出结果:
Bean1被初始化了
Be