入离职管理系统——使用注解实现依赖注入
2017-08-05 10:53
591 查看
使用传统的xml方式配置Java Bean,会使applicationContext.xml文件越来越大,不利于维护。所以现在使用注解来实现依赖注入。
其中,base-package属性指定了需要扫描哪些包。
需要注意的是,这里使用了context命名空间,所以需要在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">[/code]二、如何在类中使用注解?
目前我使用到的注解如下所示:
@Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Repository:用于标注数据访问组件,即DAO组件。
@Service:用于标注业务层组件。
@Controller:用于标注控制层组件(如struts中的action)。
@Resource:用于注入bean,标注在属性上。
@Scope:用于表明bean的生成模式,标注在类名上,其默认值为singleton,即单例模式。
@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的顺序如下:
如果同时指定了name和type属性,则根据name和type匹配bean,如果匹配不到,则抛出异常;
如果只指定了name属性,则根据name匹配bean,如果匹配不到,则抛出异常;
如果只指定了type属性,则根据type匹配bean,如果匹配不到或者匹配到多个,则抛出异常;
如果既未指定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; } }
相关文章推荐
- 使用otter实现MySQL跨机房双向同步 工作流程 环境 目标 名词解释 安装 配置 配置mysql 导入 manager 系统表 导入 node 同步算法依赖的系统表 管理配置 添加zookee
- 使用spring注解方式实现组件的声明,并且用注解注入方式解决依赖关系
- spring 框架中的依赖注入(IOC--设值注入)--使用注解--的具体实例的简单实现
- 依赖注入 - 如何使用反射和注解实现简单的依赖注入
- android使用篇(四) 注解依赖注入IOC实现绑定控件
- Asp.Net Core 项目实战之权限管理系统(4) 依赖注入、仓储、服务的多项目分层实现
- 【Annotation】使用自定义注解实现依赖注入
- 2014-05-08android使用篇(四) 注解依赖注入IOC实现绑定控件
- android使用篇(四) 注解依赖注入IOC实现绑定控件
- spring 注解方式实现依赖注入
- 使用Spring的注解方式注入Struts所管理的action
- 使用Autofac在ASP.NET Web API上实现依赖注入
- .NET 插件系统框架设计(二) 使用对象序列化实现自定义配置文件管理
- ASP.NET MVC3使用Unity2.0实现依赖注入
- 【ASP.Net MVC3 】使用Unity 实现依赖注入
- 使用Autofac在ASP.NET Web API上实现依赖注入
- MVC3使用Unity实现依赖注入接口与于实现类自动注册
- 使用C#调用系统API实现内存注入
- C_综合使用数组实现简单的学生成绩管理系统
- 使用spring的aop实现权限拦截后出现依赖注入为空的问题