package model;

 

public class DemoDAO {

    public DemoDAO() {

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

    }

}




package model;

 

public class DemoService {

    // 範例二

    private DemoDAO dao = null;

    public DemoService(DemoDAO dao) {

       // 我是建構子

       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物件,被呼叫時就會new出一個util.LogProvider的物件 -->

 

    <!-- 範例二: -->

   

    <bean id="demoDAO" class="model.DemoDAO"></bean>

    <!-- ↑這一行對應到TestDemoService中的DemoDAO dao = new DemoDAO();也就是new一個DemoDAO物件 -->

   

    <bean id="demoService" class="model.DemoService">

    <constructor-arg ref="demoDAO"></constructor-arg>

    </bean>

<!--   ↑這三行對應到TestDemoService中的DemoService service = new DemoService(dao);  -->

<!--   其中建構子內dao<constructor-arg ref="demoDAO"></constructor-arg>這一行指定 -->

   

   

</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(dao);

 

      

    }

 

}

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 乙方 的頭像
    乙方

    學習筆記專區

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