溫故知新:spring_03基于annotation配置IOC

          相比較使用XML配置,基于annotation的IOC配置會極大的簡化配置文件的內(nèi)容,所以在beans.xml中無需再配置bean的設置,只需要開啟組件掃描即可。
           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <beans xmlns="http://www.springframework.org/schema/beans"
           3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           4     xmlns:context="http://www.springframework.org/schema/context"
           5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
           6                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           7                         http://www.springframework.org/schema/context
           8                         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
           9 
          10     <!-- 開啟注解 -->
          11     <context:annotation-config/>
          12     <!-- 指定哪些需要加入掃描 -->
          13     <context:component-scan base-package="org.duyt.*"/>
          14 
          15 </beans>
          拿出一個Dao組件的注入為例
          1 package org.duyt.dao;
          2 public interface IUserDao {
          3     public void add();
          4     public void delete();
          5     public void update();
          6     public void load();
          7 }
          Dao的實現(xiàn)
           1 package org.duyt.dao.impl;
           2 
           3 import org.duyt.dao.IUserDao;
           4 import org.springframework.context.annotation.Scope;
           5 import org.springframework.stereotype.Repository;
           6 
           7 //使用Repository("userDao")標注UserDao類相當于在配置文件中寫作
           8 //<bean id="userDao" class="org.duyt.dao.impl.UserDao"/>
           9 @Repository("userDao")
          10 //相應的,Scope("singleton")標注就相當于的配置scope="singleton"
          11 @Scope("singleton")
          12 public class UserDao implements IUserDao {
          13     public void add() {
          14         System.out.println("用戶增加方法");
          15     }
          16     public void delete() {
          17         System.out.println("用戶刪除方法");
          18     }
          19     public void update() {
          20         System.out.println("用戶更新方法");
          21     }
          22     public void load() {
          23         System.out.println("用戶查詢方法");
          24     }
          25 }
          26 
          spring提供了三種注解來對應MVC的三層,數(shù)據(jù)層的注解對應Repository,業(yè)務層對應service,表示層對應controller。還有一個公共的component,分開這么多是為了更好的標注當前這個待注入類的功能,如果不按照這些分類好的注解去注入也可以完成注入,但是還是規(guī)范的去寫。關于對象中某個屬性的注入看下面的action
           1 package org.duyt.action;
           2 
           3 import javax.annotation.Resource;
           4 
           5 import org.duyt.domain.User;
           6 import org.duyt.service.IUserService;
           7 import org.springframework.context.annotation.Scope;
           8 import org.springframework.stereotype.Controller;
           9 
          10 @Controller("userAction")
          11 @Scope("prototype")
          12 public class UserAction {
          13     @Resource
          14     private IUserService userService;
          15     public String testString;
          16     public User user;
          17     
          18     public UserAction() {
          19     }
          20     
          21     public UserAction(String testString) {
          22         super();
          23         this.testString = testString;
          24     }
          25 
          26     public void addUser(){
          27         userService.add();
          28     }
          29     //get/set略
          30 }
          31 
          對于某個要注入的屬性,Spring自帶的注解是@AutoWire,但是這種是按照類型注入的,為了避免接口多實現(xiàn)可能出現(xiàn)的問題,推薦可以使用javax.annotation提供的@resource,這種是按照屬性的名稱進行注入,可以直接在屬性名之上添加注解,這樣的話連屬性的get/set都可以省了...或者是添加對應的get/set,在set方法上添加注解。但是要注意的是,按照屬性名字進行注入,屬性的名稱需要和注解定義組件時的id一致。Action中的Iuserservice的實例名為userService,會尋找有@service("userService")的注解進行注入,否則會注入失敗。
          測試如下
           1 package org.duyt.test;
           2 import org.duyt.action.UserAction;
           3 import org.junit.Test;
           4 import org.springframework.beans.factory.BeanFactory;
           5 import org.springframework.context.support.ClassPathXmlApplicationContext;
           6 public class IocTest {
           7     private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
           8     @Test
           9     public void test(){
          10         
          11         //測試依賴注入
          12         UserAction ua = (UserAction) factory.getBean("userAction");
          13         UserAction ua2 = (UserAction) factory.getBean("userAction");
          14         System.out.println(ua == ua2);
          15         ua.addUser();
          16     }
          17 }
          18 
          結果為:
          false
          用戶增加方法

          posted on 2014-11-04 11:17 都較瘦 閱讀(97) 評論(0)  編輯  收藏 所屬分類: containerFramework

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統(tǒng)計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 右玉县| 通州区| 遂溪县| 绥芬河市| 聊城市| 平泉县| 绥宁县| 东光县| 工布江达县| 灵宝市| 肃南| 精河县| 萝北县| 嘉峪关市| 大厂| 铜川市| 南乐县| 尤溪县| 苏尼特右旗| 大城县| 石渠县| 石狮市| 竹北市| 鹤山市| 大丰市| 黔西| 宜丰县| 武威市| 青川县| 上栗县| 宜宾县| 平乡县| 峡江县| 井研县| 汽车| 东乌珠穆沁旗| 河曲县| 嘉定区| 凌源市| 抚顺县| 三明市|