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,包括:
从factory中获取相应资源文件中的bean,但是这种bean读不到引用了其他文件中的bean! 2:引用应用上下文用ClassPathXmlApplicationContext ApplicationContextfactory=newClassPathXmlApplicationContext("classpath:applicationContext.xml");
3:用文件系统的路径引用应用上下文用FileSystemXmlApplicationContext newFileSystemXmlApplicationContext("src/applicationContext.xml"); 注意:在2、3的加载方式中可以加载多个配置文件,获取到ApplicationContext 对象中 String[]configs={"applicationContext.xml","user_spring.xml"}; 4:Web工程定制的加载方法 XmlWebApplicationContext ServletContextservletContext=request.getSession().getServletContext(); 注:web.xml里面可以定义两种参数: 1、application范围内的参数,存放在servletcontext中。<context-param>中的参数(可以指定多个文件) 2、servlet范围内的参数,只能在servlet的init()方法中取得, <init-param>中的参数,在init方法中用this.getInitParameter("param1")获取 二:要是spring配置多个xml,并且这些文件相互应用的加载方式 1:在web.xml配置,应用服务去加载
2:在/WEB-INF/applicationContext.xml配置应用服务去加载 可以在applicationContext.xml中用import引入其他的配置文件 importresource="user_spring.xml"/> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |