zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Spring_lifecycle

    <?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.BeanTest" fork="yes" failonerror="true">
                <classpath refid="classpath"/>
            </java>
        </target>
    
    </project>
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        <!-- 自动扫描指定包及其子包下的所有Bean类 -->
        <context:component-scan 
            base-package="org.crazyit.app.service"/>
    </beans>
    package lee;
    
    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 BeanTest
    {
        public static void main(String[] args)
        {
            // 创建Spring容器
            AbstractApplicationContext ctx = new
                ClassPathXmlApplicationContext("beans.xml");
            // 注册关闭钩子
            ctx.registerShutdownHook();
            Person person = ctx.getBean("chinese" , Person.class);
            person.useAxe();
        }
    }
    package 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 interface Axe
    {
        public String chop();
    }
    package 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 interface Person
    {
        public void useAxe();
    }
    package org.crazyit.app.service.impl;
    
    import org.springframework.stereotype.*;
    
    import javax.annotation.*;
    
    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
     */
    @Component
    public class Chinese implements Person
    {
        // 执行Field注入
        @Resource(name="steelAxe")
        private Axe axe;
        // 实现Person接口的useAxe()方法
        public void useAxe()
        {
            // 调用axe的chop()方法,
            // 表明Person对象依赖于axe对象
            System.out.println(axe.chop());
        }
        @PostConstruct
        public void init()
        {
            System.out.println("正在执行初始化的init方法...");
        }
        @PreDestroy
        public void close()
        {
            System.out.println("正在执行销毁之前的close方法...");
        }
    }
    package org.crazyit.app.service.impl;
    
    import org.springframework.stereotype.*;
    
    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
     */
    @Component
    public class SteelAxe implements Axe
    {
        public String chop()
        {
            return "钢斧砍柴真快";
        }
    }
  • 相关阅读:
    Java多线程学习(吐血超具体总结)
    java.lang.Integer can not be cast to java.lang.Long
    【转】随身HiFi 安卓OTG功能在音频上的妙用
    【转】锋狂百科:手机也能接外设 OTG技术详解
    【转】用串口登录Beaglebone Black、用usb共享电脑网络、内核模块的本地编译
    【转】Beagleboard:BeagleBoneBlack
    【转】
    【转】Beaglebone Black
    【转】你应该知道的 10 个 VirtualBox 技巧与高级特性
    如何把SKYPE的发送消息由enter改为ctrl+enter?
  • 原文地址:https://www.cnblogs.com/tszr/p/12372141.html
Copyright © 2011-2022 走看看