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

来源:互联网 发布:我们应该禁止网络用语 编辑:程序博客网 时间:2024/06/03 21:27

配置信息的改变

在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,很好理解的

[java] view plaincopy
  1. <div style="text-align: justify;"><span style="font-size:14px;"><span style="color:#000099;">@Controller  
  2. public class NewsAction extends ActionSupport {  
  3. </span></span></div>  
[java] view plaincopy
  1. <div style="text-align: justify;"><span style="font-size:14px;"><span style="color:#000099;">@Resource(name="newsServiceImpl")</span></span></div>  
[java] view plaincopy
  1. <div style="text-align: justify;"><span style="font-size:14px;"><span style="color:#000099;">public void setService(INewsService service) {</span></span></div>  
[java] view plaincopy
  1. <div style="text-align: justify;"><span style="font-size:14px;"><span style="color:#000099;">this.service = service;</span></span></div>  
[java] view plaincopy
  1. <div style="text-align: justify;"><span style="font-size:14px;"><span style="color:#000099;">}</span></span></div>  
总结:Spring的Annotation确实精简了配置文件,它不像hibernate的annotation只是在映射文件体现,
试想下在多个XXXDAOImpl,XXXAction的情况下,配置文件的注入也是很多的,除非配置通用的


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  
  11. <pre></pre>  
  12. <pre></pre> 
0 0
原创粉丝点击