找到上次我们搭建的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"
//<context:component-scan base—package="ssh"></context:component-scan>,通过这个节点的base-package属性可以配置Spring需要自动注入的哪个基包
<!-- 基于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>

使用spring注解来对类进行注解: @Controller action层 、  @Service service层 、  @Repository dao层;

 //action类的类注解
@Controller("myIndexAction")
@Scope("prototype")
public class IndexAction extends ActionSupport {
//属性的注解
@Autowired
//@Qualifier("myIndexService")
private IndexService is;
 //Service类的类注解
@Service("myIndexService")
@Scope("prototype")
public class IndexServiceImpl implements IndexService { //属性的注解
@Autowired
//@Qualifier("myIndexDao")
private IndexDao id;
 //dao类的类注解
@Repository("myIndexDao")
@Scope("prototype")
public class IndexDaoImpl implements IndexDao {
//属性的注解
@Autowired
//@Qualifier("mySessionFactory")
private SessionFactory sf;

  (注).了解一些我们常用的注解

    1.@Controller注解--定义控制器(类级别上的注解)action)

  2.@service注解--定义业务处理类(service)

  3.@repository注解--定义底层数据库处理接口(dao)

  4.@Resource注解--实现注入(jdk)

  5.@Scope("prototype")--//非单例

最新文章

  1. Linux下编译安装MariaDB
  2. geoserver使用curl发布 imagemosaic
  3. MMDrawerController的使用
  4. input实时监控和获取焦点的问题,oninput,ononfocus
  5. varnish4.0简介
  6. javascript实现继承的几种方式
  7. prmopt 提示框接收字符串,输入后按确定弹出警告框,警告内容为逆序的字符串
  8. ubuntu sublime安装及配置
  9. LeetCode_Rotate Image
  10. Linux Ubuntu 内核升级
  11. k8s踩坑记 - kubeadm join 之 token 失效
  12. BZOJ3613: [Heoi2014]南园满地堆轻絮
  13. 安卓动态分析工具【Android】3D布局分析工具
  14. JS 原型与原型链
  15. ES6 export
  16. linux python3 selenuim firefox
  17. 【排序算法】选择排序(Selection sort)
  18. Oracle表字段类型更改的一个经验
  19. js打开、关闭页面和运行代码那些事
  20. 6 python 继承与派生

热门文章

  1. PHP开发工具
  2. JavaScript分离代码理解
  3. 启用https协议的方法
  4. connections
  5. HelloWorld[Java]
  6. 数据库服务器CPU持续百分之百、部分Session一直处于执行状态---等待事件为:asynch descriptor resize(Oracle Bug )
  7. 网络请求三方库——OkHttp
  8. nodemon配置文件简单整理
  9. .NET操作Xml类
  10. [PHP] - PDO事务操作