单例设计模式:
工厂设计模式:
一、要理解IOC,首先要理解“工厂、接口、反射”
1、工厂
2、接口
3、反射
IOC理解:
就是把要生成的对象都在xml配置文件中定义,然后利用java的“反射”机制,根据xml中给定的类的名称生成对象。(不用一个个new来实例化).
spring中的IOC是 把所有bean都放在配置文件xml中,最底层最基础的factory Bean开始,先来看org.springframework.beans.factory.Bean
Factory接口,它是一个非常简单的接口,getBean方法是其中最重要的方法,比较常用的是XMLFactoryBean.getBean+xml中对象的名称,来获取对象实例。
如下实例1:
ExampleBean 类:
public class ExampleBean {
private String psnName=null;
private RefBean refbean=null;
private String addinfo=null;
public String getAddinfo() {
return getRefbean().getAddress()+getRefbean().getZipcode();
}
public String getPsnName() {
return psnName;
}
public void setPsnName(String psnName) {
this.psnName = psnName;
}
其xml配置文件 Bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="exampleBean" class="test.ExampleBean">
<property name="psnName"><value>xkf</value></property>
<property name="refbean">
<ref bean="refBean"/>
</property>
</bean>
</beans>
然后可以写个测试类来测试
public class Test{
public static void main(String[] args){
Resource input new ClassPathResource("test/Bean.xml");//获取资源文件xml
System.out.println("resource is:"+input);
BeanFactory factory=new XmlBeanFactory(input);//factorybean装载资源xml
ExampleBean eb=(ExampleBean)factory.getBean("exampleBeanName");
//factorybean通过名称获取bean
System.out.println(eb.getPsnName());
}
}
如下实例2:
spring-beans和spring-context是spring IoC容器的基础,如果只是向下面这样使用最基本的IoC特性,这两个配置就可以了。
另外spring-core提供了spring框架的核心工具类,被很多的其他模块所引用,所以使用时最好也加上。
- spring-beans:是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行IoC操作相关的所有类;
spring-context:为Spring核心提供了大量扩展。可以找到使用ApplicationContext特性时所需的全部类,JDNI所需的全部类等,以及对JavaEE的特性的支持。
配置bean其实就是把bean放到spring的IoC容器中,BeanFactory从名字可以看出它是工厂模式的实现,实际上是实例化,配置和管理众多bean的容器,在实例化bean之前先要实例化spring IoC容器,这样才能从它里面来取得bean实例,它有好多的实现类,如下有几种:
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
UserService service = (UserService) factory.getBean("userService");
但是我们一般都是用ApplicationContext来获取容器中的bean,这个接口是BeanFactory的孙子接口,它不仅提供和BeanFactory相同的功能,而且还有许多额外的功能,除了对内存非常关注,否则一般都会用ApplicationContext,因为这样更方便。
ApplicationContext有两个比较常用的实现类,分别是FileSystemXmlApplicationContext和ClassPathXmlApplicationContext,使用如下:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService) context.getBean("userService");
下面是spring配置文件,其实文件名是随意的(开始时以为必须是applicationContext.xml,因为是默认的),不一定是applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="userService" class="com.demo.SpringIoC.service.UserService"/>
</beans>