使用Spring注解来简化ssh框架的代码编写
目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写。
首先:我们浏览一下原始的applicationContext.xml文件中的部分配置。
<bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype">
<property name="is" ref="myIndexService"></property>
</bean> <bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
<property name="id" ref="myIndexDao"></property>
</bean> <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype">
<property name="sf" ref="mySessionFactory"></property>
</bean>
注:从上面的代码段中可以看出,action控制层访问的是service业务层,而service业务层则是访问dao数据层。
每个类有什么属性要注入都显得非常明显,所以本例要做的就是把applicationContext.xml配置文件进行简化。
接着:我们把applicationContext.xml文件中代码简化成如下代码:
<bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype"></bean>
<bean id="myIndexService"class="ssh.service.IndexServiceImpl" scope="prototype"></bean>
<bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype"></bean>
注:在这里只是绑定了每个bean,并没有为其注入属性。其实是用到了Spring的@Autowired和@Qualifier这两个注解。
//属性的注解
@Autowired
@Qualifier("mySessionFactory")
private SessionFactory sf;
注:在Qualifier注解中我们申明其引用的是哪一个bean,Spring 便会自动为其注入这个实例,并且可以省略属性的set方法。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 基于ssh这个包自动扫描其中的类 ,也会自动注入解析器-->
<context:component-scan base-package="ssh"></context:component-scan> <!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <bean id="mySessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
</bean> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> </bean> </beans>
注:从上面的applicationContext.xml文件中的代码我们可以看到配置文件没有对Java bean 进行相关配置, 但多了一行代码:
<context:component-scan base—package="ssh"></context:component-scan>,通过这个节点的base-package属性可以配置Spring需要自动注入的哪个基包。
接下来:我们使用spring注解来对类进行注解: @Controller action层 、 @Service service层 、 @Repository dao层;
//action类的类注解
@Controller("myIndexAction")
@Scope("prototype")
public class IndexAction extends ActionSupport {
//使用spring内置注解@Autowired自动注入实例
@Autowired
//@Qualifier("myIndexService")
private IndexService is;
//Service类的类注解
@Service("myIndexService")
@Scope("prototype")
public class IndexServiceImpl implements IndexService {
//使用spring内置注解@Autowired自动注入实例
@Autowired
//@Qualifier("myIndexDao")
private IndexDao id;
//dao类的类注解
@Repository("myIndexDao")
@Scope("prototype")
public class IndexDaoImpl implements IndexDao {
//使用spring内置注解@Autowired自动注入实例
@Autowired
//@Qualifier("mySessionFactory")
private SessionFactory sf;
注:@Controller("myIndexAction")相当于applicationContext.xml配置文件中的bean节点 ,而括号中的值相当于bean节点中的id属性值。则注解 @Scope("prototype")
相当于applicationContext.xml文件中bean节点中scope属性,这个非单例 模式注解十分重要,主要起到线程安全,防止并发操作时出现异常的作用。
注:其实除了Spring注解,还有JDK注解@Resource配置属性;说明见如下截图:

总结:本例通过使用Spring注解的方式简化了ssh框架代码编写,对实例和类直接用注解注入。
使用Spring注解来简化ssh框架的代码编写的更多相关文章
- MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法
MVC+Spring.NET+NHibernate .NET SSH框架整合 在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...
- 如何用注解简化SSH框架
一.简化代码第一步,删除映射文件,给实体类加上注解 @Entity //声明当前类为hibernate映射到数据库中的实体类 @Table(name="news") //声明tab ...
- 简化SSH框架的整合
一.开发环境: (1) OS:Windows 7 (2) DB:MySql 5.1.6 (3) JDK:1.8.0_17 (4) Server:Apache Tomcat 8. ...
- MVC+Spring.NET+NHibernate .NET SSH框架整合
在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MVC框架以后太灵活了(相比之前的web Form),嗯,关于.NET中的MVC框架我 ...
- spring+springMvc+struts的SSH框架整合
1.建立一个web项目 2.导入SSH框架所需jar包 3.配置web.xml文件 <?xml version="1.0" encoding="UTF-8" ...
- Spring学习8-用MyEclipse搭建SSH框架 Struts Spring Hibernate
1.new一个web project. 2.右键项目,为项目添加Struts支持. 点击Finish.src目录下多了struts.xml配置文件. 3.使用MyEclipse DataBase Ex ...
- SSH框架简化(struts2+spring+hibernate)
目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91 ...
- SSH框架简化
通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91,Tom ...
- SSH框架的简化(struts2、spring4、hibernate5)
目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.本文提纲:本文通过一个新闻管理系统的实例来简化ssh框架的代码编写,功能包括查询数据库中 ...
随机推荐
- Spring环境搭建之:导入jar包、配置文件名称及放置位置
Spring环境搭建之:导入jar包.配置文件名称及放置位置 现在项目开发中spring框架应用的还是比较多的,自己用的还不太熟练,每次用的时候总配置半天,总有些配置弄错,就找个时间总结以下,方便以后 ...
- 利用Jurassic在.net下运行js函数
static void Main(string[] args) { var eng = new Jurassic.ScriptEngine(); eng.Evaluate("function ...
- TCP/IP详解系列 --- 概念总结02
TCP复位报文段(RST)的用途: 1.当客户端程序访问不存在的端口时,目标主机将给它发送一个复位报文段:收到复位报文段的一端应该关闭连接或者重新连接,而不能回应这个复位报文段. 2.当客户端程序向服 ...
- 在Sublime Text3上面更加方便愉快的写php
写php代码,elcipse体积太大,开起来太麻烦,记事本又没有什么标识,稍不留神就会出现;没加.大括号没对全的尴尬情况.所以我选择使用Sublime. 推荐几个 sublime插件:sublimeL ...
- Java常用的7大排序算法汇总
1.插入排序算法 插入排序的基本思想是在遍历数组的过程中,假设在序号 i 之前的元素即 [0..i-1] 都已经排好序,本趟需要找到 i 对应的元素 x 的正确位置 k ,并且在寻找这个位置 k 的过 ...
- 异步I/O操作
今天在看boost库的时候注意到异步I/O操作时,缓冲区有效性问题. 如何实现异步操作:以异步读操作为例async_read(buffer, handler): void handler() {} v ...
- Oracle归档模式和非归档模式
一 什么是Oracle归档模式? Oracle数据库有联机重做日志,这个日志是记录对数据库所做的修改,比如插入,删除,更新数据等,对这些操作都会记录在联机重做日志里.一般数据库至少要有2个联机重做日志 ...
- c# dotfuscator 混淆后无法使用
在实体类中忘记给字段加上 get ;set ;导致编译后程序无法使用. 下面这个(A代码)是可以正常混淆的. public class PhoneUsedStatus { ...
- Mysql连接到Visual studio注意
测试环境:mysql(mysql-connector-net-6.9.7.msi)+visual studio2012 1.mysql安装ok的情况下!在控制面板管理工具-数据源(ODBC)--用户D ...
- Android自定义View的实现方法,带你一步步深入了解View(四)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17357967 不知不觉中,带你一步步深入了解View系列的文章已经写到第四篇了,回 ...