吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Spring_ResourceLoaderAware

<?xml version="1.0" encoding="GBK"?>
<project name="spring" basedir="." default="">
    <property name="src" value="src"/>
    <property name="dest" value="classes"/>

    <path id="classpath">
        <fileset dir="../../lib">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement path="${dest}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dest}"/>
        <mkdir dir="${dest}"/>
        <copy todir="${dest}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="${dest}" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
            <compilerarg value="-Xlint:deprecation"/>
        </javac>
    </target>

    <target name="run" description="Run the main class" depends="compile">
        <java classname="lee.SpringTest" fork="yes" failonerror="true">
            <classpath refid="classpath"/>
        </java>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <bean id="test" class="org.crazyit.app.service.TestBean"/>
</beans>
<?xml version="1.0" encoding="GBK"?>
<计算机书籍列表>
    <书>
        <书名>疯狂Java讲义</书名>
        <作者>李刚</作者>
    </书>
    <书>
        <书名>轻量级Java EE企业应用实战</书名>
        <作者>李刚</作者>
    </书>
</计算机书籍列表>
package lee;

import org.springframework.context.*;
import org.springframework.context.support.*;
import org.springframework.core.io.*;

import org.dom4j.io.XPPReader;
import org.dom4j.Document;
import org.dom4j.Element;

import java.util.*;

import org.crazyit.app.service.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class SpringTest
{
    public static void main(String[] args)
    {
        // 创建ApplicationContext容器
        ApplicationContext ctx = new
            ClassPathXmlApplicationContext("beans.xml");
        // 获取容器中名为test的Bean实例
        TestBean tb = ctx.getBean("test" , TestBean.class);
        // 通过tb实例来获取ResourceLoader对象
        ResourceLoader rl = tb.getResourceLoader();
        // 判断程序获得ResourceLoader和容器是否相同
        System.out.println(rl == ctx);
    }
}
package org.crazyit.app.service;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class TestBean implements ResourceLoaderAware
{
    private ResourceLoader rd;
    // 实现ResourceLoaderAware接口必须实现的方法
    // 如果把该Bean部署在Spring容器中,该方法将会由Spring容器负责调用
    // Spring容器调用该方法时,Spring会将自身作为参数传给该方法
    public void setResourceLoader(ResourceLoader resourceLoader)
    {
        this.rd = resourceLoader;
    }
    // 返回ResourceLoader对象的引用
    public ResourceLoader getResourceLoader()
    {
        return rd;
    }
}
原文地址:https://www.cnblogs.com/tszr/p/12372271.html