SSH框架联合开发(Annotation精简Spring的配置文件)

我的上一篇博文详细介绍了SSH框架的联合开发模式,这篇文章着重讲述了如何运用Annotation精简Spring当中的配置文件。所以看本篇文章有疑问时,可以参考上一篇。

配置信息的改变

在Spring3.0及以上版本中,可以使用Annotation替代一部分XML配置,从而简化一些配置代码。

使用此功能前,需要先在头信息中加入允许使用context支持的配置。(这里头信息是在初始创建的applicationContext中,我这里是在application-hibernate,xml中)

<?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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

xsi:schemaLocation="http://www.springframework.org/schema/bean

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

原来的配置信息:

<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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

怎样确定头信息更改正确呢?(在配置文件中写入一下内容)

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

base-package="org.liky.ssh.dao.impl,org.liky.ssh.back.service.impl,org.liky.ssh.back.action"></context:component-scan>


即在写入context:会出现一系列的提示;上述配置结束后,就可将之前的DAO层,Service层,Action的配置信息去除了。只需在相应的代码区域加入annotation配置就可以。当然Spring的AOp部分保留。

DAO层

这里既然删除了NewsDAOImpl NewsTypeDAOImpl的配置,那么怎样才能让HibernateTemplate注入到实现层呢?
@Component
public class NewsDAOImpl extends HibernateDaoSupport implements INewsDAO {

@Autowired
public NewsDAOImpl(HibernateTemplate hibernateTemplate) {
super.setHibernateTemplate(hibernateTemplate);
}

分别在 NewsDAOImpl NewsTypeDAOImpl 中加入上面一段代码,就可以让 hibernateTemplate 注入了。

Service层

这里@Service也表示是一个bean文件
@Service
public class NewsServiceImpl implements INewsService {

•••••
@Resource(name="newsDAOImpl")
public void setNewsdao(INewsDAO newsdao) {
this.newsdao = newsdao;
}

@Resource(name = "newsTypeDAOImpl")
public void setTypedao(INewsTypeDAO typedao) {
this.typedao = typedao;
}

Action配置

这里的Controller也是一个bean,在setService注入的时,resource引用newsService,很好理解的


    
    
@Controller public class NewsAction extends ActionSupport {

     
     
@Resource(name="newsServiceImpl")

     
     
public void setService(INewsService service) {

     
     
this.service = service;

     
     
}

总结:Spring的Annotation确实精简了配置文件,它不像hibernate的annotation只是在映射文件体现,
试想下在多个XXXDAOImpl,XXXAction的情况下,配置文件的注入也是很多的,除非配置通用的














  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值