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

<?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.SpELTest" fork="yes" failonerror="true">
            <classpath refid="classpath"/>
        </java>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的根元素和Schema
    导入p:命名空间和util:命名空间的元素 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- 使用util.properties加载指定资源文件 -->
    <util:properties id="confTest"
        location="classpath:test_zh_CN.properties"/>
    <!--
    配置setName()的参数时,在表达式中调用方法
    配置setAxe()的参数时,在表达式中创建对象
    配置调用setBooks()的参数时,在表达式中访问其他Bean的属性 -->
    <bean id="author" class="org.crazyit.app.service.impl.Author"
        p:name="#{T(java.lang.Math).random()}"
        p:axe="#{new org.crazyit.app.service.impl.SteelAxe()}"
        p:books="#{ {confTest.a , confTest.b} }"/>
</beans>
a=《轻量级Java EE企业应用实战》
b=《疯狂Java讲义》
a=u300au8f7bu91cfu7ea7Java EEu4f01u4e1au5e94u7528u5b9eu6218u300b
b=u300au75afu72c2Javau8bb2u4e49u300b
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
{
    String chop();
}
package org.crazyit.app.service;

import java.util.*;
/**
 * 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();
    public List<String> getBooks();
    public String getName();
}
package org.crazyit.app.service.impl;

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 SteelAxe implements Axe
{
    public String chop()
    {
        return "钢斧砍柴很快!";
    }
}
package org.crazyit.app.service.impl;

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 Author implements Person
{
    private Integer id;
    private String name;
    private List<String> books;
    private Axe axe;
    // id的setter和getter方法
    public void setId(Integer id)
    {
        this.id = id;
    }
    public Integer getId()
    {
        return this.id;
    }

    // name的setter和getter方法
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // books的setter和getter方法
    public void setBooks(List<String> books)
    {
        this.books = books;
    }
    public List<String> getBooks()
    {
        return this.books;
    }

    // axe的setter方法
    public void setAxe(Axe axe)
    {
        this.axe = axe;
    }

    public void useAxe()
    {
        System.out.println("我是"
            + name + ",正在砍柴
" + axe.chop());
    }
}
package lee;

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

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 SpELTest
{
    public static void main(String[] args)
    {
        ApplicationContext ctx = new
            ClassPathXmlApplicationContext("beans.xml");
        Person author = ctx.getBean("author" , Person.class);
        System.out.println(author.getBooks());
        author.useAxe();

    }
}
原文地址:https://www.cnblogs.com/tszr/p/12372012.html