SSH学习九 依赖注入及加载Spring配置文件的方法

依赖注入(DI)和控制反转(IoC),意思是一样的

传统方式中,如果JAVA实例A需要另一个实例B,那么需要new B(),然后给A用,也就是有调用者A创建被调用者B的实例

依赖注入模式下:创建被调用者B的工作不再由A完成,而是由Spring容器完成(或者说工厂模式的工厂完成),然后注入调用者,因此也成为依赖注入,因为A和B是依赖关系。

依赖注入有两种方式:

(1)设值注入

Spring通过Set方法为类A的成员注入值。

(2)构造注入

通过构造器注入。

例子如下:

代码逻辑:

Service通过构造器注入dog和cat,也就是构造注入,而dog和cat的属性-年龄是通过set方法注入,也就是设值注入,实现的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	
	<!-- 
	<bean id="dog" class="com.ehr.service.Service"></bean>
	Dog dog =new Dog();
	 -->
	 <bean id = "dog" class="com.learn.DAO.impl.Dog">
	 	<property name="dage" value="12"></property>
	 </bean>
	 <bean id="cat" class="com.learn.DAO.impl.Cat">
	 	<property name="cage" value="21"></property>
	 </bean>
	 <bean id = "service" class="com.ehr.service.Service">
	 	<constructor-arg ref="dog"></constructor-arg>
	 	<constructor-arg ref="cat"></constructor-arg>
	 </bean>
</beans>

Service:
package com.ehr.service;

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

import com.learn.DAO.ICat;
import com.learn.DAO.IDog;
import com.learn.DAO.impl.Cat;
import com.learn.DAO.impl.Dog;

public class Service {
	IDog dog;
	ICat cat;
	
	public Service(IDog dog,ICat cat){
		this.dog = dog;
		this.cat = cat;
	}
	

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		        "applicationContext.xml");
		Service s1 = (Service) context.getBean("service");
		s1.dog.bark();
		s1.cat.bark();
	}
}


IDog
package com.learn.DAO;

public interface IDog {
	public void bark();
}


Cat
package com.learn.DAO.impl;

import com.learn.DAO.ICat;

public class Cat implements ICat {
	int cage;

	public void setCage(int cage) {
		this.cage = cage;
	}
	@Override
	public void bark() {
		System.out.println("Cat.bark()"  + cage);
	}
	
}

ICat
package com.learn.DAO;

public interface ICat {
	public void bark();
}

加载配置文件的方法:

转载这里:http://noobjava.iteye.com/blog/976472


一:Spring中的几种容器都支持使用xml装配bean,包括:
XmlBeanFactory ,
ClassPathXmlApplicationContext ,
FileSystemXmlApplicationContext ,
XmlWebApplicationContext
加载这些容器的配置文件的xml有一下几种常见的方法:
1:引用资源用XmlBeanFactory(不能实现多个文件相互引用)

Java代码
  1. Resourceresource=newClassPathResource("appcontext.xml");
  2. BeanFactoryfactory=newXmlBeanFactory(resource);

从factory中获取相应资源文件中的bean,但是这种bean读不到引用了其他文件中的bean!
2:引用应用上下文用ClassPathXmlApplicationContext
ApplicationContextfactory=newClassPathXmlApplicationContext("classpath:applicationContext.xml");
  • newClassPathXmlApplicationContext("conf/userConfig.xml");//src/conf目录下的
  • newClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");

  • 3:用文件系统的路径引用应用上下文用FileSystemXmlApplicationContext
    newFileSystemXmlApplicationContext("src/applicationContext.xml");
  • newFileSystemXmlApplicationContext("classpath:appcontext.xml");
  • newFileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
  • newFileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");

  • 注意:在2、3的加载方式中可以加载多个配置文件,获取到ApplicationContext 对象中
    String[]configs={"applicationContext.xml","user_spring.xml"};
  • ApplicationContextctx=newClassPathXmlApplicationContext(configs);
  • //ApplicationContextctx=newFileSystemXmlApplicationContext(configs);
  • AbstractDaomyUserDAO=(AbstractDao)ctx.getBean("userDao");

  • 4:Web工程定制的加载方法 XmlWebApplicationContext
    ServletContextservletContext=request.getSession().getServletContext();
  • ApplicationContextctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);


  • 注:web.xml里面可以定义两种参数:
    1、application范围内的参数,存放在servletcontext中。<context-param>中的参数(可以指定多个文件)
    2、servlet范围内的参数,只能在servlet的init()方法中取得, <init-param>中的参数,在init方法中用this.getInitParameter("param1")获取
    二:要是spring配置多个xml,并且这些文件相互应用的加载方式
    1:在web.xml配置,应用服务去加载
    Xml代码
      <servlet>
    1. servlet-name>app</servlet-class org.springframework.web.servlet.DispatcherServlet
    2. context-paramparam-name>contextConfigLocationparam-value>/WEB-INF/applicationContext*.xml,/WEB-INF/user_spring*.xmlload-on-startup>1>

    2:在/WEB-INF/applicationContext.xml配置应用服务去加载
    可以在applicationContext.xml中用import引入其他的配置文件
    importresource="user_spring.xml"/>

    相关文章

    什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
    单一职责原则定义(Single Responsibility Principle,SRP)...
    动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
    适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
    策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
    设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...