Spring笔记:基于XML的DI-IoC容器中bean的简单装配
2018-03-28 13:28
507 查看
此仅为个人笔记,若有不周之处,万望指正,不胜感激。
在Spring框架中对象是不需要自己创建的,由容器提供,需要的时候像容器索取即可,这样的好处在于降低代码间耦合,提高代码复用性,且有助于后期维护。Spring框架中主要使用BeanFactory和ApplicationContext两种容器,下面例子使用的是ApplicationContext容器。
ApplicationContext容器的简单使用分以下几步:
配置xml文件,配置文件名Spring官方建议使用applicationContext.xml,代码如下:
使用ApplicationContext声明一个容器引用,而ApplicationContext本身是一个接口,所以便用它的实现类ClassPathXmlApplicationContext或FileSystemXmlApplicationContext声明对象。通过ClassPathXmlApplicationContext类来声明容器ApplicationContext对象是在类路径下寻找xml文件,而通过FileSystemXmlApplicationContext类来创建容器ApplicationContext对象是在项目根目录下寻找xml文件,还可以在系统目录下寻找。代码如下:
上述代码中doSome()函数为该Bean的一个方法。
在Spring框架中对象是不需要自己创建的,由容器提供,需要的时候像容器索取即可,这样的好处在于降低代码间耦合,提高代码复用性,且有助于后期维护。Spring框架中主要使用BeanFactory和ApplicationContext两种容器,下面例子使用的是ApplicationContext容器。
ApplicationContext容器的简单使用分以下几步:
配置xml文件,配置文件名Spring官方建议使用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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 注册Bean --> <bean id="MyService" class="top.bwextend.service.SomeServiceImp1"/> </beans><bean id="MyService" class="top.bwextend.service.SomeServiceImp1"/>作用等价于:"SomeServiceImp1 MyService=new SomeServiceImp1();"注册Bean等同于使用无参构造函数声明了一个对象,所以必须保证此Bean有无参构造函数。至于参数的初始化,先不做讨论。
使用ApplicationContext声明一个容器引用,而ApplicationContext本身是一个接口,所以便用它的实现类ClassPathXmlApplicationContext或FileSystemXmlApplicationContext声明对象。通过ClassPathXmlApplicationContext类来声明容器ApplicationContext对象是在类路径下寻找xml文件,而通过FileSystemXmlApplicationContext类来创建容器ApplicationContext对象是在项目根目录下寻找xml文件,还可以在系统目录下寻找。代码如下:
package top.bwextend.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import top.bwextend.service.ISomeService; import top.bwextend.service.SomeServiceImp1; public class MyTest { public void test01() { ISomeService service=new SomeServiceImp1(); service.doSome(); } @Test public void test02() { //创建容器对象,加载Spring配置文件 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); ISomeService service=(ISomeService) ac.getBean("MyService"); service.doSome(); } }ClassPathXmlApplicationContext("applicationContext.xml")代码中applicationContext.xml为第一步中配置的xml文件。声明完容器对象之后便可通过容器对象的getBean()方法得到Bean,getBean()方法的参数为xml文件中声明的对应Bean的id属性。
上述代码中doSome()函数为该Bean的一个方法。
相关文章推荐
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- 在IoC容器中装配Bean(基于XML配置)
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring--IoC--基于XML的DI-异类抽象Bean
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- IoC容器装配Bean(xml配置方式)(Bean的生命周期)
- Spring--IoC--基于XML的DI-内部Bean
- Spring(三):IoC容器装配Bean(xml配置方式和注解方式)
- 04_IOC容器装配Bean(xml方式)
- Spring--IoC--基于XML的DI-同类抽象Bean
- Spring学习-06:IOC容器装配Bean(xml配置方式)详解
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- spring ioc容器的学习笔记4---XmlBeanFactory ioc容器的简单设计原理解析
- Spring框架 之 反射机制简单实现基于XML标签的IOC与DI
- spring基础-Bean的装配方式(一)基于xml的装配
- spring笔记(二)---基于XML的Bean装配
- 框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示
- 基于XML的DI-同类抽象Bean
- Spring--IoC--基于XML的DI-自动注入-byType
- 通过IOC手写简单Spring Bean容器