package model;

 

import javax.annotation.Resource;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

 

@Component("demoBeanAn01")

//上面這一行等同於<bean id="demoBeanAn01" class="modle.DemoBean">

public class DemoBean {

    @Resource(name="date2")

    private java.util.Date date;

    public void setDate(java.util.Date date) {

       this.date = date;

    }

 

    @Override

    public String toString() {

       return "DemoBean ["+ date+ "]";

    }

}



package model;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

 

@Component("demoDAOAn01")

public class DemoDAO {

    @Autowired //自己會去new生出一個DemoBean物件來

    private DemoBean demoBean;

    public void setDemoBean(DemoBean demoBean) {

       this.demoBean = demoBean;

    }

 

    @Override

    public String toString() {

       return "DemoDAO ["+ demoBean+ "]";

    }

}



package model;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

 

@Component("demoServiceAn01")

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

public class DemoService {

    @Autowired

    private DemoDAO demoDAO;

    public void setDemoDAO(DemoDAO demoDAO) {

       this.demoDAO = demoDAO;

    }

 

    @Override

    public String toString() {

       return "DemoService ["+demoDAO+"]";

    }

}



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

<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.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config></context:annotation-config>

<context:component-scan base-package="model"></context:component-scan>

 

 

<!--   <bean id="demoService1" class="model.DemoService"> -->

<!--       <property name="demoDAO"> --><!-- 也就是setDemoDAO -->

<!--          <bean class="model.DemoDAO"> --><!-- 因為DAO也還沒被new,所以得自己在裡面new一個DAO物件 -->

<!--              <property name="demoBean"> --><!-- DAO中的setDemoBean() -->

<!--                 <bean class="model.DemoBean"> -->

<!--                     <property name="date"> --><!-- setDate()方法 -->

<!--                         <bean class="java.util.Date"></bean> --><!-- 終於new一個Date出來了 -->

<!--                     </property> -->

<!--                 </bean> -->

<!--              </property> -->

<!--          </bean> -->

<!--       </property>   -->

<!--   </bean> -->

 

    <bean id="date" class="java.util.Date"></bean>

    <bean id="date1" class="java.util.Date"></bean>

    <bean id="date2" class="java.util.Date">

<!--   有兩個java.util.Date時,若DemoBean中有一個@Autowired會不知道要找誰而當機,於是給他們自己的id(name,二則一)

再放上@Resource(name="date2") -->

    <property name="time" value="0"></property><!-- Date中的方法setDate -->

    </bean>

    <bean id="demoBean" class="model.DemoBean">

       <property name="date" ref="date"></property>

    </bean>

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

       <property name="demoBean" ref="demoBean"></property>

    </bean>

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

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

    </bean>

 

</beans>



package test;

 

import model.DemoBean;

import model.DemoDAO;

import model.DemoService;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class TestAnnotation {

 

    public static void main(String[] args) {

       ApplicationContext context =

              new ClassPathXmlApplicationContext("beans.config.xml");

      

       DemoBean demoBeanAn01=(DemoBean) context.getBean("demoBeanAn01");

       System.out.println("DemoBean="+demoBeanAn01);

      

       DemoDAO demoDAOAn01=(DemoDAO) context.getBean("demoDAOAn01");

       System.out.println("DemoBean="+demoDAOAn01);

      

       DemoService demoServiceAn01=(DemoService) context.getBean("demoServiceAn01");

       System.out.println("DemoBean="+demoServiceAn01);

      

       ((ConfigurableApplicationContext) context).close();

       //務必記得關掉!!!

    }

 

}

 

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