-
相关maven依赖
<span style="font-size:14px;"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.hive</groupId>
<artifactId>mars</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Plato</name>
<url>http://maven.apache.org</url>
<properties>
<!--配置局部JDK版本-->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--依赖版本-->
<spring-version>4.1.0.RELEASE</spring-version>
<junit-version>4.11</junit-version>
<log4j-version>1.2.17</log4j-version>
<j2ee-version>7.0</j2ee-version>
<hibernate-version>4.3.6.Final</hibernate-version>
<MySql-version>5.1.15</MySql-version>
</properties>
<!--依赖坐标-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<!--使用aop注解aop编程必须导入-->
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
<exclusions>
<exclusion>
<artifactId>jboss-logging-annotations</artifactId>
<groupId>org.jboss.logging</groupId>
</exclusion>
<exclusion>
<artifactId>jboss-logging-annotations</artifactId>
<groupId>org.jboss.logging</groupId>
</exclusion>
<exclusion>
<artifactId>jboss-logging-annotations</artifactId>
<groupId>org.jboss.logging</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- mysql driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${MySql-version}</version>
</dependency>
<!-- c3p0 datasource -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-web-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>${j2ee-version}</version>
</dependency>
<!-- slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
</project></span>
即使是持久层 跟spring整合 依然导入了 j2ee 的包 是因为 在整合过程中发现了一个问题
如果J2EE 的包是6.0的任何版本 都会发生 ClassFormatError的 错误 原因是 6.0版本仅仅是做了规范 并没有实现 在实际运用中要避免引用
-
工程目录结构如下
这里仅仅贴一个 简单dao层的代码段 service 层 可以后面通过依赖注入实现
- 接口
<span style="font-size:12px;">package org.hive.dao;
import org.hive.bean.Admin;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* Author: Dax
* Date: 2016/10/10
* Time: 17:54
*/
<span style="color:#33CC00;">/* 通过注解来声明事务则该接口下的方法都会实现事务 如果不需要事务可在相关实现类下来实现 */</span>
@Transactional
public interface AdminDao {
/**
* Select all list.
*
* 查询全部用户 返回清单
*
* @return the list
*/
List<Admin> selectAll();
/**
* Add.
*
* 添加一个用户
*
* @param admin the admin
*/
void add(Admin admin);
/**
* Remove.
*
* 移除一个用户
*
* @param admin the admin
*/
void remove(Admin admin);
/**
* Update.
*
* 修改用户信息
*
* @param admin the admin
*/
void update(Admin admin);
/**
* Select one admin.
*
*通过用户的id来查询用户
*
* @param id the id
* @return the admin
*/
Admin selectOne(Integer id);
}</span>
- 实现类
仅仅实现了简单的查询 其他不再一 一实现
<span style="font-size:12px;">package org.hive.dao.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hive.bean.Admin;
import org.hive.dao.AdminDao;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* Author: Dax
* Date: 2016/10/10
* Time: 16:17
*/
</span><pre name="code" class="java"><span style="font-size:12px;"><span style="color:#33CC00;">/* 通过注解注入IOC容器 */</span></span>
@Repositorypublic class AdminDaoImpl implements AdminDao { @Resource(name = "sessionFactory") private SessionFactory factory; public SessionFactory getFactory() { return factory; } public void setFactory(SessionFactory factory) { this.factory
= factory; } @Override public List<Admin> selectAll() { Session session = factory.openSession(); return session.createQuery("FROM Admin ").list(); } @Override public void add(Admin admin) { } @Override public void remove(Admin admin) { } @Override public void
update(Admin admin) { } @Override public Admin selectOne(Integer id) { Session session = factory.openSession(); return (Admin) session.get(Admin.class, id); }}
- 实体类
package org.hive.bean;
import org.springframework.stereotype.Repository;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.sql.Timestamp;
/**
* Created with IntelliJ IDEA.
* Author: Dax
* Date: 2016/10/10
* Time: 17:51
*/
<pre name="code" class="java"><span style="font-size:12px;"></span><pre name="code" class="java"><span style="font-size:12px;"><span style="color:#33CC00;">/* 通过注解注入IOC容器 */</span></span>
@Repository<span style="font-size:12px;"></span><pre name="code" class="java"><span style="font-size:12px;"><span style="color:#33CC00;">/* hibernate 相关的注解配置 */</span></span>
@Entitypublic class Admin { private Integer adminId; private String adminName; private String adminPassword; private Timestamp loginTime; @Id @Column(name = "ADMINID") public Integer getAdminId() { return adminId; } public void setAdminId(Integer adminId) {
this.adminId = adminId; } @Basic @Column(name = "ADMINNAME") public String getAdminName() { return adminName; } public void setAdminName(String adminName) { this.adminName = adminName; } @Basic @Column(name = "ADMINPASSWORD") public String getAdminPassword()
{ return adminPassword; } public void setAdminPassword(String adminPassword) { this.adminPassword = adminPassword; } @Basic @Column(name = "LOGINTIME") public Timestamp getLoginTime() { return loginTime; } public void setLoginTime(Timestamp loginTime) { this.loginTime
= loginTime; }}
- spring配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<pre name="code" class="html"><span style="color:#33CC00;"><!--开启注解扫描--></span>
<context:component-scan base-package="org.hive"><span style="color:#33CC00;"><!--配置这个排除一些过滤器 相关用法在下面有资料介绍--></span>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan><pre name="code" class="html"><span style="color:#33CC00;"><!--开启AOP注解 并强制使用cglib代理--></span>
<aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/><pre name="code" class="html"><span style="color:#33CC00;"><!--导入属性配置文件--></span>
<context:property-placeholder file-encoding="utf-8" location="hh.properties"/> <!--配置c3p0数据源 你也可以启用你自己的数据源--> <bean id="c3p0_dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property
name="driverClass" value="${driver}"/> <property name="jdbcUrl" value="${url}"/> <property name="user" value="${user}"/> <property name="password" value="${password}"/> <property name="maxPoolSize" value="${max}"/> <property name="minPoolSize" value="${min}"/>
<property name="initialPoolSize" value="${initSize}"/> </bean> <!--配置session工厂--> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!--为工厂注入数据源依赖-->
<property name="dataSource" ref="c3p0_dataSource"/><!--hibernate的一些其他的设置--> <property name="hibernateProperties"> <map> <entry key="dialect" value="${dialect}"/> <entry key="show_sql" value="${show_sql}"/> <entry key="hbm2ddl.auto"
value="${hbm2ddl.auto}"/> <entry key="format_sql" value="${format_sql}"/> </map> </property><span style="color:#FF0000;"><!--实体类的映射 类似于hibernate.cfg.xml中的mapped文件资源 不写将抛出 实体类无映射的异常 可以通过扫描包 或者 注解类等多种手段实现 可以查阅相关api--></span>
<property name="packagesToScan" value="org.hive.*"/> </bean> <!--事务管理 采用注解的方式--> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/></beans>
<strong><span style="color:#CC6600;">hh.properties</span></strong>
#数据源driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/testuser=rootpassword=adminmax=30min=5initSize=8#session工厂dialect=org.hibernate.dialect.MySQL5Dialectshow_sql=truehbm2ddl.auto=updateformat_sql=true
<context:component-scan /> 用法
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
注意:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者。另外<context:annotation-config/>还提供了两个子标签
1. <context:include-filter>
2. <context:exclude-filter>
在说明这两个子标签前,先说一下<context:component-scan>有一个use-default-filters属性,改属性默认为true,这就意味着会扫描指定包下的全部的标有@Component的类,并注册成bean.也就是@Component的子注解@Service,@Reposity等。所以如果仅仅是在配置文件中这么写
<context:component-scan base-package="tv.huan.weisp.web"/>
Use-default-filter此时为true那么会对base-package包或者子包下的所有的进行java类进行扫描,并把匹配的java类注册成bean。
可以发现这种扫描的粒度有点太大,如果你只想扫描指定包下面的Controller,该怎么办?此时子标签<context:incluce-filter>就起到了勇武之地。如下所示
<context:component-scan base-package="tv.huan.weisp.web .controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
这样就会只扫描base-package指定下的有@Controller下的java类,并注册成bean
但是因为use-dafault-filter在上面并没有指定,默认就为true,所以当把上面的配置改成如下所示的时候,就会产生与你期望相悖的结果(注意base-package包值得变化)
<context:component-scan base-package="tv.huan.weisp.web ">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
此时,spring不仅扫描了@Controller,还扫描了指定包所在的子包service包下注解@Service的java类
此时指定的include-filter没有起到作用,只要把use-default-filter设置成false就可以了。这样就可以避免在base-packeage配置多个包名这种不是很优雅的方法来解决这个问题了。
另外在我参与的项目中可以发现在base-package指定的包中有的子包是不含有注解了,所以不用扫描,此时可以指定<context:exclude-filter>来进行过滤,说明此包不需要被扫描。综合以上说明
Use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描源
转自 :http://blog.csdn.net/chunqiuwei/article/details/16115135
根据上面的结构 可以看出 通过声明依赖 然后 注入依赖 层层进行操作 对同样的业务流程 采取AOP的思想 进行切面编程 理清思路其实 还是很简单的