Spring使用IOC容器创建对象及ClassPathXmlApplicationContext与FileSystemXmlApplicationContext接口详解

引言

使用Spring框架来创建对象,涉及到IOC容器的使用。主要的实现方法为IOC容器中提供的两个接口。BeanFactoryApplicationContext

其中,BeanFactory有很多实现类,ApplicationContext

BeanFactory的子接口,其常用实现类是org.springframework.context.support.FileSystemXmlApplicationContextorg.springframework.context.support.ClassXmlAplicationContext

Spring的配置信息通常采用XML配置文件来设置,因此,创建ApplicationContext实例时,应该提供XML配置文件作为参数。

类的建立

和正常的java类的创建形式是一样的,此处,先建立一个测试类,取名为User.java

package com.action.spring;

public class User {
    
    
	public void add() {
    
    		
		System.out.println("Spring...");
	}
}

配置文件

Spring框架中,主要是通过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="user" class="com.action.spring.User"></bean>

</beans>

其中,中的信息为Spring框架配置文件头信息,固定内容。

添加关联关系,<bean id="user" class="com.action.spring.User">其中,id为类的别名,class所指向的为类的地址。

IOC创建对象

创建一个类用于测试使用IOC容器的形式创建对象。

package testdemo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.action.spring.User;

public class testSpring5 {
    
    
	
	@Test
	public void testAdd() {
    
    		
		ApplicationContext context =
				new FileSystemXmlApplicationContext("src/config/bean1.xml");
		
		User user = context.getBean("user",User.class);
		
		System.out.println(user);
		user.add();
	}

}

创建实例之后,使用.getBean()方法进行调用,两个参数,一个是配置对象id,一个是对象类型

三个文件的项目位置关系如下图。

在这里插入图片描述

运行结果

结果分别为对象的路径和调用对象方法后打印的结果。

在这里插入图片描述

接口详解

在上述方法中,采用的是ApplicationContext接口来创建的实例。

下面详细介绍ApplicationContext的实际运用:

  • (1)接口一ClassPathXmlApplicationContext

    只能读放在web-info/classes目录下的配置文件

    1.没有前缀:默认为项目的classpath(即src目录下)下相对路径
    ApplicationContext appCt = new ClassPathXmlApplicationContext("app.bean1.xml");

    2.前缀classpath:表示的是项目的classpath下相对路径
    ApplicationContext appCt = new ClassPathXmlApplicationContext("classpath:app.bean1.xml");

    3.使用前缀file 表示的是文件的绝对路径
    ApplicationContext appCt = new ClassPathXmlApplicationContext("file:E:/workspace/Springwork/src/app.bean1.xml");

    4.可以同时加载多个文件
    String[] xmlCfg = new String[] { "classpath:base.bean1.xml","app.bean1.xml"}; ApplicationContext appCt = new ClassPathXmlApplicationContext(xmlCfg);

    5.使用通配符加载所有符合要求的文件
    ApplicationContext appCt = new ClassPathXmlApplicationContext("*.bean1.xml");

  • (2)接口二:FileSystemXmlApplicationContext

    1.默认为项目工作路径 即项目的根目录
    ApplicationContext appCt2 = new FileSystemXmlApplicationContext("src/config/bean1.xml");

    2.前缀classpath:表示的是项目的classpath下相对路径
    ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:app.bean1.xml");

    3.使用前缀file 表示的是文件的绝对路径
    ApplicationContext appCt2 = new FileSystemXmlApplicationContext("file:E:/workspace/Springwork/src/app.bean1.xml");

    4.可以同时加载多个文件
    String[] xmlCfg = new String[] { "src/config/bean1.xml","classpath:app.bean1.xml"}; ApplicationContext appCt2 = new FileSystemXmlApplicationContext(xmlCfg);

    5.使用通配符加载所有符合要求的文件
    ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:*.bean1.xml");

猜你喜欢

转载自blog.csdn.net/jerrygaoling/article/details/108621614