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


    <?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="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basenames">
                <list>
                    <value>message</value>
                </list>
            </property>
        </bean>
        <!-- Spring容器会检测容器中所有Bean,如果发现某个Bean实现了
        ApplicationContextAware接口,Spring容器会在创建该Bean之后,
        自动调用该Bean的setApplicationContext()方法,调用该方法时,
        会将容器本身作为参数传给该方法。-->
        <bean id="person" class="org.crazyit.app.service.Person"/>
    </beans>
    hello=欢迎你,{0}
    now=现在时间是:{0}
    hello=welcome,{0}
    now=now is :{0}
    hello=u6b22u8fceu4f60uff0c{0}
    now=u73b0u5728u65f6u95f4u662fuff1a{0}
    package lee;
    
    import java.io.*;
    import org.springframework.context.*;
    import org.springframework.context.support.*;
    
    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)throws Exception
        {
            ApplicationContext ctx = new
                ClassPathXmlApplicationContext("beans.xml");
            Person p = ctx.getBean("person" , Person.class);
            p.sayHi("孙悟空");
        }
    }
    package org.crazyit.app.service;
    
    import org.springframework.context.*;
    import org.springframework.beans.BeansException;
    import java.util.Locale;
    /**
     * 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 Person implements ApplicationContextAware
    {
        // 将BeanFactory容器以成员变量保存
        private ApplicationContext ctx;
    
        /* Spring容器会检测容器中所有Bean,如果发现某个Bean实现了ApplicationContextAware接口,
        Spring容器会在创建该Bean之后,自动调用该方法,调用该方法时,
        会将容器本身作为参数传给该方法。*/
        public void setApplicationContext(ApplicationContext ctx)
            throws BeansException
        {
            this.ctx = ctx;
        }
        public void sayHi(String name)
        {
            System.out.println(ctx.getMessage("hello" , new String[]{name}
                , Locale.getDefault(Locale.Category.FORMAT)));
        }
    }
  • 相关阅读:
    RPC
    动词 or 名词 :这是一个问题 【转载】
    js 如何清除setinterval
    封装动画特效
    飞入特效
    建字段_添加数据_生成json.php
    mybatis由浅入深day02_9.3.5使用生成的代码_9.4逆向工程注意事项
    mybatis由浅入深day02_9逆向工程
    mybatis由浅入深day02_8spring和mybatis整合
    mybatis由浅入深day02_7.4mybatis整合ehcache_7.5二级缓存应用场景_7.6二级缓存局限性
  • 原文地址:https://www.cnblogs.com/tszr/p/12370289.html
Copyright © 2020-2023  润新知