package model;

 

public class DemoDAO {

    public DemoDAO() {

       System.out.println("DemoDAO's constructor");

    }

}




package model;

 

public class DemoService {

    // 範例三:

     private DemoDAO dao = null;

     public void setDAO(DemoDAO dao) {

     // setter

     this.dao = dao;

     }

 

}



<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 

 

    <bean id="logProvider" class="util.LogProvider"></bean>

    <!-- 範例三: -->

    <bean id="demoDAO" class="model.DemoDAO"></bean><!-- new一個 DemoDAO物件-->

    <bean id="demoService" class="model.DemoService"><!-- new一個 DemoService物件-->

    <property name="dao" ref="demoDAO"></property>

    <!-- 呼叫setter方法(setDao((DemoDAO dao)),指定給一個demoDAO物件,也就是 service.setDAO(dao)中的dao -->

    <!-- name="dao":去掉setDao中的set,字首變小寫 -->

    </bean>

   

</beans>




package org.iiiedu.samuel.ProjSpringDemo;

 

import model.DemoDAO;

import model.DemoService;

 

public class TestDemoService {

 

    public static void main(String[] args) {

//     範例三

       DemoDAO dao = new DemoDAO();

       DemoService service = new DemoService();

            service.setDAO(dao);

 

      

    }

 

}

 

創作者介紹
創作者 乙方 的頭像
乙方

學習筆記專區

乙方 發表在 痞客邦 留言(0) 人氣()