单纯的Spring项目

IOC容器是Spring框架中重要的一部分,使用Spring框架必然要创建IOC容器,Spring项目如下,只看红线圈住
的内容即可

Spring项目与Web项目IOC容器创建方式的区别

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="car" class="com.atguigu.spring.beans.Car">
		<constructor-arg value="Baoma"></constructor-arg>
		<constructor-arg value="Beijing"></constructor-arg>
		<constructor-arg value="3000"></constructor-arg>
	</bean>
</beans>

Car.java

package com.atguigu.spring.beans;

public class Car {
	private String brand;
	private String corp;
	private double price;
	private int maxSpeed;
	
	public Car(String brand, String corp, double price) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.price = price;
	}

	public Car(String brand, String corp, int maxSpeed) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.maxSpeed = maxSpeed;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
	}
}

Main.java-创建IOC容器如下

package com.atguigu.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		// 1.创建Spring的IOC容器对象-通过使用ClassPathXmlApplicationContext类加载配置文件创建容器
		// ApplicationContext代表的是IOC容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 2.从IOC容器中获取Bean示例
		Car car = (Car)ctx.getBean("car");
		System.out.println(car);
	}
}
如果在Spring项目中,没有使用ClassPathXmlApplicationContext,直接使用@Controller,@Service往容器中
注入对象,当在用@Autowired获取对象时,会报空指针异常,如下代码
// 往容器中注入对象
@Controller
public class UserController {
	public void test(){
		System.out.println("userController...");
	}
}

@Component
public class Test {
// 获取容器中注入的对象,此时没有加载配置文件创建IOC容器,所以获取不到对象实例,会报空指针异常
	@Autowired	
	private static UserController userController;
	public static void main(String[] args) {
		userController.test();
	}
}

整合Spring的Web项目

web项目中,由于配置文件的存在,可以不需要开发人员自己使用ClassPathXmlApplicationContext创建IOC容器,
而是在配置文件中写好,在启动项目时,由项目自己创建IOC容器

Spring项目与Web项目IOC容器创建方式的区别

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	
	xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
	id="WebApp_ID" version="3.1">
  	
  	<!--   作用一:创建IOC容器对应的对象
  		   作用二:把那些对象放到了application对应的域里面 -->
  	<context-param>
  	<!-- 配置文件的位置
  		 类路径下的applicationContext.xml -->
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext.xml</param-value>
  	</context-param>
  	<!-- 把 Spring 容器集成到 Web 应用里面 ,采用listener创建ApplicationContext实例
  	     1.spring提供ServletContextListener的一个实现类ContextLoaderListener,
  	               该类可以作为Listener使用,在只有一个xml配置文件时,可以将其放在web-inf文件夹下,
  	               这时不需要<context-param>配置,项目会在创建时自动查找web-inf/applicationContext.xml文件,
  	               因此,如果只有一个配置文件,并且名为applicationContext.xml,只需要在web.xml文件中加入listener配置即可
  	     2.当有多个xml配置文件需要载入(这种情况也适用于只有一个applicationContext.xml文件),
  	              则考虑用<context-param>元素来确定配置文件的文件名与路径。ContextLoadListenter加载时,会查找名为contextConfigLocation的参数。
  	              因此,配置context-param时,参数名应该是contextConfigLocation,param-value则指定文件所在的位置,比如在类路径下的某个文件
    	 3.如是没有通过contextConfigLocation指定配置文件,spring会自动查找applicationContext.xml文件;
    	  如果有contextConfigLocation,则利用该参数确定的配置文件,如果无法找到合适的配置文件,spring将无法正常初始化
    -->
  	<listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  	</listener>
</web-app>

区别

可以看出,Spring项目需要借助类ApplicationContext 来手动建IOC容器
而整合Spring的Web项目,在配置文件中配置监听器listener,在项目启动时,在配置文件中写清楚applicationContext.xml
文件所在位置即可,项目会在启动时,自己创建IOC容器。

相关文章:

  • 2021-11-22
  • 2021-12-25
  • 2022-01-10
  • 2021-09-27
  • 2021-08-06
  • 2022-12-23
  • 2021-04-13
  • 2022-12-23
猜你喜欢
  • 2021-04-02
  • 2021-07-22
  • 2021-10-03
  • 2021-08-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
相关资源
相似解决方案