在SSH中使用注解可以减少配置XML文件,毕竟随着项目规模的扩大,配置bean将把Spring的配置文件(applicationContext.xml)变得很混乱

在Spring的配置文件中开启注解扫描

<context:component-scan base-package="cn.lynu"></context:component-scan>

注意这个base-package就是指定了要扫描的包范围,这里可以指定一个共有的包名以扫描所有类的注解

这里给一个Spring配置文件:

现在只需要配置C3P0和hibernate并开启注解扫描就可以了

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/easyui"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <!-- 配置hibernate -->
<!-- 1.先配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean> <!-- 2.开启事务管理器,用注解方式使用事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事务,记得在需要开启事务的类上 使用@Transaction-->
<tx:annotation-driven transaction-manager="transactionManager"/> <!-- 开启注解扫描 -->
<context:component-scan base-package="cn.lynu"></context:component-scan> </beans>

开始使用注解

1.先从Dao开始,在Dao类上使用@Repository注解声明自己

然后是很关键的一步,我使用了HibernateDaoSupport,需要在Dao中注入sessionFactory,而Dao中就需要调用HibernateDaoSupport中的sessionFactory

    @Resource(name="sessionFactory")
public void setSF(SessionFactory sf){
super.setSessionFactory(sf);
}

setSF这个名字不是固定的,只要是以set打头就行了

2.在Service类上使用注解@Service

然后使用@Resource注入Dao(使用注解注入属性就不需要set方法)

@Resource(name="adminDao")
private AdminDao adminDao;

3.在Action类上面使用@Controller指明自己是控制器,使用@Scope("prototype")指明为多实例,action都是多实例的

同样还要使用@Resource(J2EE规范注解,按照名称进行装配)注入Service(使用注解注入属性就不需要set方法)

@Resource(name="adminService")
private AdminService adminService;

SSH开发中的注解使用的更多相关文章

  1. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

  2. SSH开发中 使用超链接到action 其excute方法会被执行两次 actual row count: 0; expected: 1

    由于执行两次excute,所以在做删除操作的时候会出现 Batch update returned unexpected row count from update [0]; actual row c ...

  3. 廖师兄springboot微信点餐开发中相关注解使用解释

    package com.imooc.dataobject;import lombok.Data;import org.hibernate.annotations.DynamicUpdate;impor ...

  4. 【详细】总结JavaWeb开发中SSH框架开发问题(用心总结,不容错过)

    在做JavaWeb的SSH框架开发的时候,遇到过很多的细节问题,这里大概记录下 我使用的IDE是Eclipse(老版本)三大框架:Spring4.Struts2.Hibernate5 1.web.xm ...

  5. MyBatis 项目开发中是基于 XML 还是注解?

    只要你对 MyBatis 有所认识和了解,想必知道 MyBatis 有两种 SQL 语句映射模式,一种是基于注解,一种是基于XML. 基于 XML <mapper namespace=" ...

  6. 开发中常见的@NotNull,@NotBlank,@NotEmpty注解的区别

    开发中常看见@NotNull,@NotBlank,@NotEmpty三个注解,但却没有深入了解过,下面介绍一下他们的应用场景和区别 @NotNull:主要用在基本数据类型上(Int,Integer,D ...

  7. SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...

  8. 注解式项目开发!详细解析Java中各个注解的作用和使用方式

    @Target 作用: 指明了修饰的这个注解的使用范围, 即被描述的注解可以用在哪里 @Target(ElementType.Type) ElementType取值的类型: TYPE: 类,接口或者枚 ...

  9. SSH开发环境搭建

    断断续续学习hibernate也有一段时间了,在这里研究一下SSH开发环境的搭建过程,自己简单的搭建一个SSH的开发环境.采用maven搭建. 0.项目结构: 1.导包:(maven项目) pom.x ...

随机推荐

  1. poj1330lca入门题

    直接套模板,dfs的时候注意起点 #include<map> #include<set> #include<cmath> #include<queue> ...

  2. 修改当前启动菜单项的HyperVisorLaunchType

    switch-hyperv.bat @echo off "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\ ...

  3. 三十四 Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy信号详解

    信号一般使用信号分发器dispatcher.connect(),来设置信号,和信号触发函数,当捕获到信号时执行一个函数 dispatcher.connect()信号分发器,第一个参数信号触发函数,第二 ...

  4. java多线程补:充原子性和可见性

    参考:http://www.cnblogs.com/mengyan/archive/2012/08/22/2651575.html 原子性:所谓原子性就是不可分割的,比如:在我们编程中直接给变量赋值, ...

  5. LeetCode OJ :Move Zeroes (移动0)

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  6. Leetcode 1018. Binary Prefix Divisible By 5

    class Solution: def prefixesDivBy5(self, A: List[int]) -> List[bool]: ans,t = [],0 for a in A: t ...

  7. 多进程(了解):守护进程,互斥锁,信号量,进程Queue与线程queue(生产者与消费者模型)

    一.守护进程 主进程创建守护进程,守护进程的主要的特征为:①守护进程会在主进程代码执行结束时立即终止:②守护进程内无法继续再开子进程,否则会抛出异常. 实例: from multiprocessing ...

  8. 如何在公司Http代理后使用NuGet官方源

    文章转自CSDN 霍力强的专栏 有些公司上网使用的是Http代理.默认情况下,VS是无法访问外部网络的.如果要使用NuGet,通常只能在局域网里架一个自己的NuGet服务器.但这种方法不论是packa ...

  9. Thinking in Java 4th(Java编程思想第四版)文档、源码、习题答案

    Thinking in Java 4th 中.英文两版pdf文档,书中源码及课后习题答案.链接:https://pan.baidu.com/s/1BKJdtgJ3s-_rN1OB4rpLTQ 密码:2 ...

  10. VMware 10安装Mac OS X 10.11和XCode7

    上周把我的计算机当试验品,安装mac虚拟机.由于文件下载复制解压的时间花了很长,历时两天,记录下来(和我一样的新手不妨参考一下): 我机硬件:win7 64位 8G内存 没有8G以上就不要考虑了.我安 ...