Spring_IOC

IOC容器简介

什么是容器?

在java里的类,在Spring中都被称作Bean,容器是用来读取Bean的定义、管理对象的初始化和生产、以及对象之间的依赖关系。同时容器是用来装载对象,描述对象之间的关系。

IOC容器主要由BeanFactory、ApplicationContext两个接口实现。实际开发中,用后者比较多。AppliacationContext继承BeanFactory接口。它除了有BeanFactory的功能之外,还有如下功能:

  1. 资源访问
  2. 对国际化的支持
  3. 对时间的支持

BeanFactory的常用方法:

  1. ObjectgetBean(String name):根据Bean标识获得Bean实例(常用)
  2. ObjectgetBean(String name , ClassrequiredType):根据Bean标识获得Bean实例,并转换为指定的类型
  3. boolean containsBean(String name):判断当前BeanFactory中是否包含该Bean
  4. boolean isSingleton(String name):判断当前的Bean的scope是否是singleton
  5. ClassgetTyoe(String name):获得当前Bean的类型
  6. String[] getAliases(String name):获得当前bean的别名

BeanFactory的实现类

   beanFactory的实现类有很多最常用的是—xmlBeanFactory

BeanFactory的实例化

1 Resource resource = new FileSystemResource("bean.xml");
2 
3 BeanFactory factory = new XmlBeanFactory(resource);
4 
5 ClassPathResource resource = new ClassPathResource("bean.xml");
6 
7 BeanFactory factory = new XmlBeanFactory(resource);

 ApplicationContext的实现类

ClassPathXmlApplicationContext(常用)

FileSystemXmlApplicationContext

XmlWebApplicationContext

ApplicationContext实例化

1 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
2 
3 ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans1.xml" , "beans2.xml"});
4 使用file:/、classpath:、http://等URL前缀
5 
6 ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:beans.xml");

Bean的定义标识和别名

定义:

Spring中的bean被定义在一个xml文件中或属性文件中,例如:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 <bean id=”……” class=”……”>
 6 <!-配置bean属性->
 7 </bean>
 8 <bean id=”……” class=”……”>
 9 <!-配置bean属性->
10 </bean>
11 <bean id=”……” class=”……”>
12 <!-更多的bean定义->
13 </bean>
14 </beans>

Xml 就是schema,如果是DTD就是文档类型定义。

Id就是标识,且不能重复,唯一的。

Bean的实例化 就是取代new实例化对象,spring中不再使用new

bean的实例化

 1.默认的构造方法

使用默认的构造方法来实例化的类没有特殊要求,只要提供一个没有参数的构造方法即可,例如Bean可如下定义:

1 <bean id = "exampleBean" class = "examples.ExampleBean"/>
2 
3 <bean nane = "anotherExample" class = "examples.ExampleBeanTwo"/>

2.工厂方法

当采用静态工厂方法创建Bean时,除了需要制定class属性外,还需要通过factory-method来指定创建Bean事例的构造方法,例如:

1  <bean nane = "anotherExample" class = "examples.ExampleBeanTwo" factory-method = "createInstance"/>

createInstance必须是静态方法

3.工厂类的工厂方法

与使用静态工厂方法实例化类似,用来进行实例化的实例工厂方法位于另外一个已有的bean中,容器将调用该bean的工厂方法来创建一个新的bean实例,例如:

1 <bean id = "myFactoryBean" class = "……"></bean>
2 
3 <bean nane = "exampleBean" factory-bean = "myFactoryBean" factory-method = "createInstance"/>

Bean的scope也就是其作用域的意思

Singleton

在Spring中,从容器中获得的实例默认都是singleton的,也就是默认每个bean名称只维护一个bean实例

Prototype

如果要想每次都获得一个新的实例,则可以把bean的scope属性设置为prototype,例如:

1 <bean id = "bean1" class = "Bean1" scope =” prototype” />

Request

表示在web环境中的request范围

Session

表示在web环境中的session范围

Globalsession

它仅仅在基于protlet的web应用才有意义

一、IOC简单实例

首先的创建spring工程,如果用idea的话,会自动加载一些spring jar文件。

新建Person类

 1 package com.feimao.IOC.test;
 2 
 3 public class Person {
 4     private String name;
 5     private int age;
 6 
 7     public String getName() {
 8         return name;
 9     }
10 
11     public int getAge() {
12         return age;
13     }
14 
15     public void setName(String name) {
16         this.name = name;
17     }
18 
19     public void setAge(int age) {
20         this.age = age;
21     }
22 }

创建Tester测试类

 1 package com.feimao.IOC.test;
 2 
 3 import org.springframework.beans.factory.BeanFactory;
 4 import org.springframework.beans.factory.xml.XmlBeanFactory;
 5 import org.springframework.core.io.ClassPathResource;
 6 import org.springframework.core.io.Resource;
 7 
 8 
 9 public class Tester {
10     public static void main(String[] args){
11         Resource r = new ClassPathResource("beans.xml");
12         BeanFactory factory = new XmlBeanFactory(r);
13         Person per = (Person) factory.getBean("per");
14         System.out.println(per.getName());
15         System.out.println(per.getAge());
16     }
17 }

创建beans.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE beans PUBLIC "_//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
 3         >
 4 <beans>
 5     <bean id = "per" class = "com.feimao.IOC.test.Person">
 6         <property name = "name" value = "feimao"/>
 7         <property name = "age" value = "28"/>
 8     </bean>
 9 
10 </beans>

测试结果如下图:

用ApplicationContext实例化修改上面的程序

 1 package com.feimao.IOC.test;
 2 
 3 import javafx.application.Application;
 4 import org.springframework.beans.factory.BeanFactory;
 5 import org.springframework.beans.factory.xml.XmlBeanFactory;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 import org.springframework.core.io.ClassPathResource;
 9 import org.springframework.core.io.Resource;
10 
11 
12 public class Tester {
13     public static void main(String[] args){
14         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
15         /*Resource r = new ClassPathResource("beans.xml");
16         BeanFactory factory = new XmlBeanFactory(r);*/
17         Person per = (Person) context.getBean("per");
18         System.out.println(per.getName());
19         System.out.println(per.getAge());
20         
21 
22     }
23 
24 }

 二、同时加载2个配置文件


 1 package com.feimao.IOC.test;
 2 
 3 import javafx.application.Application;
 4 import org.springframework.beans.factory.BeanFactory;
 5 import org.springframework.beans.factory.xml.XmlBeanFactory;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 import org.springframework.core.io.ClassPathResource;
 9 import org.springframework.core.io.Resource;
10 
11 
12 public class Tester {
13     public static void main(String[] args){
14         /*Resource r = new ClassPathResource("beans.xml");
15         BeanFactory factory = new XmlBeanFactory(r);*/
16         String[] str = {"beans.xml" , "beans-forum.xml"};//通过数组加载两个配置文件
17         ApplicationContext context = new ClassPathXmlApplicationContext(str);
18         Person per = (Person) context.getBean("per");
19         System.out.println(per.getName());
20         System.out.println(per.getAge());
21         Customer cus = (Customer) context.getBean("cus");
22         System.out.println(cus.getName());
23 
24 
25     }
26 
27 }


 

定义:

Spring中的bean被定义在一个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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id=”……” class=”……”>

<!-配置bean属性->

</bean>

<bean id=”……” class=”……”>

<!-配置bean属性->

</bean>

<bean id=”……” class=”……”>

<!-更多的bean定义->

</bean>

 

</beans>

Xml 就是schema,如果是DTD就是文档类型定义。

Id就是标识,且不能重复,唯一的。

 三、创建单例模式

 1 package com.feimao.IOC.test;
 2 
 3 public class Stu {
 4     private String name;
 5     private static Stu instance = new Stu();//static静态属性,所有对象都是共有的
 6     public static Stu getInstance(){
 7         return instance;
 8     }
 9     private Stu(){
10 
11     }
12 
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20     public static void main(String[] args){
21         Stu s = Stu.getInstance();
22         Stu s2 = Stu.getInstance();
23         System.out.println(s == s2);
24     }
25 }

测试单例模式

1    Person per = (Person) context.getBean("per");
2         Person per2 = (Person) context.getBean("per");
3        /* System.out.println(per.getName());
4         System.out.println(per.getAge());
5         Customer cus = (Customer) context.getBean("cus");
6         System.out.println(cus.getName());*/
7        System.out.println(per == per2);

如果是多例的话,每次拿到的对象都是新对象,上面的测试代码结果返回就是false

原文地址:https://www.cnblogs.com/feimaoyuzhubaobao/p/10018104.html