注:之前的一篇博客【JDBC】查询Student表是用纯JSP实现的,现在将Spring引入其中。Eclipse、MySQL、Spring4。
简介
Spring是分层的轻量级开源框架,以IoC(Inverse of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)为内核,使用基本的JavaBean来完成以前只能有EJB来完成的工作,开发模式更加高效。在实际开发中,通常服务器端采用三层体系架构,分别为表示层(web)、业务逻辑层(service)、持久层(dao)。Spring对每一层都提供了技术支持,在表示层提供了与Struts2框架的整合,在业务逻辑层可以管理事务,记录日志等,在持久层可以整合Hibernate、JdbcTemplate等技术,从设计上看,给予了Java程序员许多的自由度,对业界的常见问题也提供了良好的解决方案。
实例
1、引入Jar包
新建一个Web项目SpringTest,引入所需Jar包。本实例用的是Spring4,实例所需Jar包可单击下载。
2、编写代码
项目的目录结构如图1-1所示,其中Test类用于测试。所有类文件和配置文件的代码也在下面列出。
图1-1 项目目录结构
public interface StudentDao {
//查询出Student表中所有数据
public List<Student> selectAll();
}
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Student> selectAll() {
// 定义SQL语句
String sql = "select * from student";
// 将结果集通过Java的反射机制映射到Java对象中
RowMapper<Student> rowMapper = ParameterizedBeanPropertyRowMapper
.newInstance(Student.class);
return this.jdbcTemplate.query(sql, rowMapper);
}
}
public interface StudentService {
//查询所有数据
public List<Student> queryAll();
}
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Resource(name="studentDao")
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public List<Student> queryAll() {
return this.studentDao.selectAll();
}
}
public class Test {
public static void main(String[] args){
//获得Spring容器
String xmlPath = "applicationContext.xml";
//加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);
//取得StudentServiceImpl实例
StudentService studentService =(StudentService)ac.getBean("studentService");
//通过实例取得查询结果,遍历输出
List<Student> students=studentService.queryAll();
for(Student student:students){
System.out.println(student);
}
}
}
applicationContext.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: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/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">
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:c3p0-db.properties" />
<!-- 1.配置数据源,读取properties文件 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.配置JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 使用context命名空间,通知spring扫描指定目录 -->
<context:component-scan base-package="cn.jujianfei"></context:component-scan>
</beans>
c3p0-db.properties文件
jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.jdbcUrl = jdbc:mysql:///dbname
jdbc.user=root
jdbc.password=jujianfei
3、输出结果
总结
IoC:是指在程序设计中,实例不再由调用者来创建,而是由Spring容器来创建。Spring容器会负责控制程序之间的关系,而不是由程序代码直接控制,这样控制权由应用代码转移到了外部容器,控制权发生了反转,即IoC思想。
DI(Dependency Injection,依赖注入):与控制反转含义相同,只不过这两个称呼是从两个角度描述的同一概念。它的作用在于使用Spring框架创建对象时,动态地将其所依赖的对象注入到Bean组件中。
Annotation(注解):在Spring中,尽管使用XML配置文件可以实现Bean的装配工作,但如果应用中有很多Bean时,会导致XML配置文件过于臃肿,给后续的维护和升级工作带来一定的困难。为此,Java从JDK1.5以后,提供了Annotation功能。
JDBCTemplate:Spring框架在数据库开发中的应用主要使用的是JDBCTemplate类,该类作为Spring对JDBC支持的核心,提供了所有对数据库操作功能的支持。
AOP:为了将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,Spring提供了面向切面的编程方式,也称Spring AOP,这有效地减少了系统间的重复代码,达到了模块间的松耦合目的。AOP采取横向抽取机制,取代了传统纵向继承体系提取重复性代码的模式,主要体现在事务处理、日志管理、权限控制、异常处理等方面,使开发人员在编写业务逻辑可以专心于核心业务,提高了代码的可维护性。
优点:方便解耦、简化开发;AOP编程的支持;声明式事务的支持;方便程序测试;方便集成各种优秀的框架;对一些较为难用的API进行了封装(JavaMail,JDBC等),降低了JavaEE API的使用难度。