入离职管理系统——使用注解实现依赖注入

来源:互联网 发布:淘宝买东西优惠 编辑:程序博客网 时间:2024/06/07 13:03

使用传统的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类中,如下

@Componentpublic class PositionVo {    private int id;    private String position;    //这里省略了setter/getter方法}

2.2## @Repository##

@Repositorypublic 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 ##

@Servicepublic 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;      }}
阅读全文
0 0
原创粉丝点击