3.4.8解析依赖
当我们有一个类a需要在类b使用之前初始化,那么我们就需要使用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="b" depends-on="a"/> <bean id="a" name="code">package com.apress.prospring2.ch03.beandependency;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;/** * @author janm */public class DependencyDemo { public static void main(String[] args) { XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("/META-INF/spring/beandependencydemo1-context.xml")); B b = (B) bf.getBean("b"); A a = (A) bf.getBean("a"); System.out.println(a); System.out.println(b); }}
A{}Shared.getValue()=Completed}B{}Shared.getValue()=Completed}