入離職管理系統——使用註解實現依賴注入

使用傳統的xml方式配置Java Bean,會使applicationContext.xml文件越來越大,不利於維護。所以現在使用註解來實現依賴注入。

一、如何實現使用註解進行依賴注入?

使用註解非常簡單,只需在applicationContext.xml中添加如下代碼:

<context:component-scan base-package="com.entry_exit.*"></context:component-scan>

其中,base-package屬性指定了需要掃描哪些包。
需要注意的是,這裏使用了context命名空間,所以需要在applicationContext.xml的頭文件中進行聲明:

<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

二、如何在類中使用註解?

目前我使用到的註解如下所示:

  1. @Component:泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。
  2. @Repository:用於標註數據訪問組件,即DAO組件。
  3. @Service:用於標註業務層組件。
  4. @Controller:用於標註控制層組件(如struts中的action)。
  5. @Resource:用於注入bean,標註在屬性上。
  6. @Scope:用於表明bean的生成模式,標註在類名上,其默認值爲singleton,即單例模式。
  7. @Lazy:用於表明該bean是否在bean容器初始化時創建,默認值爲false;當指定爲true時,表明該bean在使用的時候才創建,一般可以用於泛型類。

2.1## @Component ##
我將這個註解放在vo類中,如下

@Component
public class PositionVo {
    private int id;
    private String position;
    //這裏省略了setter/getter方法
}

2.2## @Repository##

@Repository
public class PositionDaoImpl extends BasicDaoImpl<PositionVo> implements PositionDao {
    @Override
    public PositionVo get(String position) {
        // TODO Auto-generated method stub
        String hql = "from PositionVo where position = :position";
        return (PositionVo) getSession().createQuery(hql).setString("position", position).uniqueResult();
    }
}

2.3## @Service ##

@Service
public class PositionServiceImpl implements PositionService {
    @Resource
    private PositionDao positionDao;
    public void setPositionDao(PositionDao positionDao) {
        this.positionDao = positionDao;
    }
    //省略了其他方法
}

2.4## @Controller ##

@Controller
@Scope("prototype")
public class PositionAction extends BasicAction<PositionVo> {
    @Resource
    private PositionService positionService;
    public void setPositionService(PositionService positionService) {
        this.positionService = positionService;
    }
    //省略了其他方法
}

2.5## @Resource ##
@Resource註解,具有兩個重要的屬性,name和type。@Resource匹配bean的順序如下:

  1. 如果同時指定了name和type屬性,則根據name和type匹配bean,如果匹配不到,則拋出異常;
  2. 如果只指定了name屬性,則根據name匹配bean,如果匹配不到,則拋出異常;
  3. 如果只指定了type屬性,則根據type匹配bean,如果匹配不到或者匹配到多個,則拋出異常;
  4. 如果既未指定name屬性,也未指定type屬性,則先按name匹配,若匹配不到,則按type匹配,如果依然匹配不到,或匹配到多個,則拋出錯誤。

    2.6## @Scope ##
    一般用於action類上,並指定其值爲prototype。因爲action對應頁面的請求,每個請求必須生成一個相對應的action實例。如下:

@Controller
@Lazy(true)
@Scope("prototype")
public class BasicAction<T> extends ActionSupport implements SessionAware, ApplicationAware, RequestAware, ModelDriven<T> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected int page;
    protected int rows;


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }


    //域對象  
    protected Map<String, Object> request;  
    protected Map<String, Object> session;  
    protected Map<String, Object> application;

    @Override
    public void setRequest(Map<String, Object> request) {
        // TODO Auto-generated method stub
        this.request = request;

    }

    @Override
    public void setApplication(Map<String, Object> application) {
        // TODO Auto-generated method stub
        this.application = application;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        // TODO Auto-generated method stub
        this.session = session;
    }


    protected T model;
    @Override
    public T getModel() {
        // TODO Auto-generated method stub
        ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();  
        Class clazz = (Class)type.getActualTypeArguments()[0];  
        try {  
            model = (T)clazz.newInstance();  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }     
        return model;  

    }

}
發佈了26 篇原創文章 · 獲贊 8 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章