zoukankan      html  css  js  c++  java
  • Example of ApplicationContextAware in Spring--转

    原文地址:http://www.concretepage.com/spring/example_applicationcontextaware_spring

    In spring we can get ApplicationContext anywhere in our code with the help of ApplicationContextAware. ApplicationContextAware is the interface and there is only one method setApplicationContext() in it. When a class implements ApplicationContextAware, that class needs to override the setApplicationContext() method. ApplicationContextAware is used for bean lookup purpose and for those objects which needs to access file resources. Find the example below.

    Implement ApplicationContextAware Interface

    ApplicationContextAwareTest.java

    package com.concretepage;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    public class ApplicationContextAwareTest implements ApplicationContextAware  {
    	ApplicationContext context;
    	public ApplicationContext getContext() {
    		return context;
    	}
    	@Override
    	public void setApplicationContext(ApplicationContext context)
    			throws BeansException {
    		this.context=context;
    	}
    }
     

    Configure Bean

    app-conf.xml

    <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-3.0.xsd ">
      
        <bean id="testA" class="com.concretepage.A"/>
        <bean id="appcontext" class="com.concretepage.ApplicationContextAwareTest"/>
    </beans>


    A.java

    package com.concretepage;
    public class A {
    	public void doTask(){
    		System.out.println("Do some task.");
    	}
    }

    Run Demo

    SpringTest.java

    package com.concretepage;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class SpringTest {
    	public static void main(String[] args) {
    		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf.xml");
    		ApplicationContextAwareTest appcontext= (ApplicationContextAwareTest)context.getBean("appcontext");
    		ApplicationContext appCon =appcontext.getContext();
    		A a= (A)appCon.getBean("testA");
    		a.doTask();
    		context.registerShutdownHook();
    	}
    }

    Find the output.

    Do some task.
  • 相关阅读:
    TP5.1 查看具体版本
    require(): open_basedir restriction in effect. 解决方法
    解决php -v查看到版本于phpinfo()打印的版本不一致问题
    Session机制详解
    c# 获取项目的根目录
    会修电脑不会修收音机?-闲聊设计模式原则
    CSV格式数据如何导入SqlServer?
    CSV格式数据如何导入MySQL?
    反射—程序员的快乐 -08
    策略模式 是一种好策略 -07
  • 原文地址:https://www.cnblogs.com/davidwang456/p/5555453.html
Copyright © 2011-2022 走看看