标签:red text 分享图片 关系 family odi framework 修改 方法
IOC容器简介
什么是容器?
在java里的类,在Spring中都被称作Bean,容器是用来读取Bean的定义、管理对象的初始化和生产、以及对象之间的依赖关系。同时容器是用来装载对象,描述对象之间的关系。
IOC容器主要由BeanFactory、ApplicationContext两个接口实现。实际开发中,用后者比较多。AppliacationContext继承BeanFactory接口。它除了有BeanFactory的功能之外,还有如下功能:
BeanFactory的常用方法:
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的scope也就是其作用域的意思
Singleton
Prototype
Request
Session
globalsession
一、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就是标识,且不能重复,唯一的。
标签:red text 分享图片 关系 family odi framework 修改 方法
原文地址:https://www.cnblogs.com/feimaoyuzhubaobao/p/10018104.html