ApplicationContext解析

ApplicationContext 这个接口作为作为spring框架下最重要的一个接口他提供了能力结构图如上。

他自己提供了以下方法

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

	/**
	 * 返回这个context的唯一id
	 */
	@Nullable
	String getId();

	/**
	 * 返回这个context所属的应用名称
	 */
	String getApplicationName();

	/**
	 * 返回一个context名称
	 */
	String getDisplayName();

	/**
	 * 返回context加载的时间
	 */
	long getStartupDate();

	/**
	 * 返回级的上下文
	 */
	@Nullable
	ApplicationContext getParent();

	/**
	 * 公开AutowireCapableBeanFactory接口的能力给到context
	 * <p>This is not typically used by application code, except for the purpose of
	 * initializing bean instances that live outside of the application context,
	 * applying the Spring bean lifecycle (fully or partly) to them.
	 * <p>Alternatively, the internal BeanFactory exposed by the
	 * {@link ConfigurableApplicationContext} interface offers access to the
	 * {@link AutowireCapableBeanFactory} interface too. The present method mainly
	 * serves as a convenient, specific facility on the ApplicationContext interface.
	 * <p><b>NOTE: As of 4.2, this method will consistently throw IllegalStateException
	 * after the application context has been closed.</b> In current Spring Framework
	 * versions, only refreshable application contexts behave that way; as of 4.2,
	 * all application context implementations will be required to comply.
	 * @return the AutowireCapableBeanFactory for this context
	 * @throws IllegalStateException if the context does not support the
	 * {@link AutowireCapableBeanFactory} interface, or does not hold an
	 * autowire-capable bean factory yet (e.g. if {@code refresh()} has
	 * never been called), or if the context has been closed already
	 * @see ConfigurableApplicationContext#refresh()
	 * @see ConfigurableApplicationContext#getBeanFactory()
	 */
	AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果在代码中出现了无法解析符号 'applicationContext' 的错误,可能是因为没有正确地引入相关的依赖包或者没有在配置文件中配置相关的bean。 在Spring MVC中,需要在配置文件中配置一个名为 "applicationContext.xml" 的文件,并在其中定义相关的bean。同时,在需要使用ApplicationContext的Controller中,需要通过@Autowired或者@Resource等注解注入ApplicationContext。 下面是一个示例的配置文件: ```xml <!-- 配置 MyController1 --> <bean id="myController1" class="com.example.MyController1"> <property name="myController2" ref="myController2" /> </bean> <!-- 配置 MyController2 --> <bean id="myController2" class="com.example.MyController2" /> <!-- 配置 ApplicationContext --> <bean id="applicationContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg> <list> <value>classpath:applicationContext.xml</value> </list> </constructor-arg> </bean> ``` 在上面的配置文件中,定义了两个Controller:MyController1和MyController2,并将MyController2注入到了MyController1中。同时,还定义了一个名为 "applicationContext" 的bean,它的类是ClassPathXmlApplicationContext,它会读取classpath路径下的applicationContext.xml文件,并将其中定义的bean加载到应用程序中。 在使用ApplicationContext的Controller中,可以通过@Autowired或者@Resource等注解来注入ApplicationContext,然后使用getBean方法获取其他Controller的实例。例如: ```java @Controller public class MyController1 { @Autowired private ApplicationContext applicationContext; private MyController2 myController2; public void setMyController2(MyController2 myController2) { this.myController2 = myController2; } @RequestMapping("/myController1") public String myController1Method(Model model) { if (myController2 == null) { myController2 = applicationContext.getBean(MyController2.class); } String result = myController2.myMethod(); model.addAttribute("result", result); return "myView"; } } @Controller public class MyController2 { public String myMethod() { return "Hello World!"; } } ``` 在上面的代码中,MyController1通过@Autowired注解注入了ApplicationContext,并定义了一个名为 "myController2" 的私有变量。在myController1Method方法中,会先判断myController2是否为空,如果为空则通过getBean方法获取MyController2的实例。最后调用MyController2中的myMethod方法获取返回值,并将返回值通过Model传递给视图层。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值