💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
![](https://img.kancloud.cn/85/ae/85aef7127eaebe5df2c402c089360925_400x172.png) 这节内容讲的是 Spring 容器在初始化阶段,如果是 stand-alone 应用,使用传统的 Java main() 方法创建容器的话。那么刚开始那个阶段算是 dependency-pull 类型的控制反转。就两个文件,一个主启动类,一个xml配置文件如下: ```java public class DependencyPull { public static void main(String... args) { ApplicationContext ctx = new ClassPathXmlApplicationContext ("spring/app-context.xml"); MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class); mr.render(); } } ``` ~~~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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="provider" class="com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider"/> <bean id="renderer" class="com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer" p:messageProvider-ref="provider"/> </beans> ~~~ 不过上边的 xml 配置文件当中 bean 的类型是引用了第二章 hello-world 工程中的代码,这样复用,而不是复制的方式,会好一些。