Kela's Blog

            前面的路很坎坷,但毕竟是条路.也许走过这一段就会发现,走过去就是梦想中的地方.因此坚持成为此刻唯一能做且必须去做的事情.
posts - 9, comments - 27, trackbacks - 0, articles - 15

kela的笔记 应用程序框架 ---- spring(3)

Posted on 2006-08-22 10:41 Kela 阅读(181) 评论(0)  编辑  收藏 所属分类: 我的笔记(Spring)

摘要:属性参考至对象。一个对象的属性参考另一个对象的实例,如,学生(StudentBean)对象有两个属性一个是name一个是addressBeanname是一个String型,而addressBeanAddressBean对象(地址对象)的一个实例。

     StrudentBean.java

 

package com.kela.spring.ioc;

 

public class StudentBean {

   

    private String name;

    private AddressBean addressBean;

   

    public AddressBean getAddressBean() {

        return addressBean;

    }

    public void setAddressBean(AddressBean addressBean) {

        this.addressBean = addressBean;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

}

     AddressBean.java

 

package com.kela.spring.ioc;

 

public class AddressBean {

   

    private String addressName;

 

    public String getAddressName() {

        return addressName;

    }

 

    public void setAddressName(String addressName) {

        this.addressName = addressName;

    }

}

     beans-config_2.xml

 

<?xml version= "1.0" encoding= "GB2312" ?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

 

<beans>

    <bean id= "addressBean" class= "com.kela.spring.ioc.AddressBean" >

       <property name= "addressName" >

           <value> 团结大街向阳路 123 </value>

       </property>

    </bean>

   

    <bean id= "studentBean" class= "com.kela.spring.ioc.StudentBean" >

       <property name= "name" >

           <value> KangFeng </value>

       </property>

       <property name= "addressBean" >

           <!-- 注意这里已不是 value 了,改为 ref -->

           <ref bean= "addressBean" ></ref>

       </property>

    </bean>

</beans>

     TestClass.java

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

 

public class TestClass {

   

    public void method_2() {

        try {

            ApplicationContext context = new FileSystemXmlApplicationContext("bin\\com\\kela\\spring\\ioc\\beans-config_2.xml");

            StudentBean studentBean = (StudentBean)context.getBean("studentBean");

            System.out.println(" 姓名:" + studentBean.getName() + ";地址:" + studentBean.getAddressBean().getAddressName());

        } catch (Exception e) {

            System.out.println("[ERROR]" + e.getMessage());

        }

    }

   

    public static void main(String[] args) {

        TestClass testClass = new TestClass();

        testClass.method_2();

    }

}

     学习小结

 

Don’t Call MeIll Call You”。

 


只有注册用户登录后才能发表评论。


网站导航:
 
分享到: