Spring笔记02

1. Spring整合连接池

1.1 Spring整合C3P0

  • 在工程中导入c3p0连接池需要的包com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

  • c3p0的硬编码方式

    1. @Test //自己new对象,自己设置属性
    2. public void test() throws Exception {
    3. ComboPooledDataSource dataSource = new ComboPooledDataSource();
    4. //设置驱动
    5. dataSource.setDriverClass("com.mysql.jdbc.Driver");
    6. //设置地址
    7. dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/hibernate");
    8. //设置用户名
    9. dataSource.setUser("root");
    10. //设置密码
    11. dataSource.setPassword("2626");
    12. //获取链接池连接对象
    13. Connection con = dataSource.getConnection();
    14. System.out.println(con);
    15. //com.mchange.v2.c3p0.impl.NewProxyConnection@26ba2a48
    16. }
  • Spring整合c3p0连接池

  • 配置文件

    1. <!-- c3p0 -->
    2. <bean id="C3P0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    3. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    4. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate"></property>
    5. <property name="user" value="root"></property>
    6. <property name="password" value="2626"></property>
    7. </bean>
  • 测试

    1. @Test //Spring的IOC+DI替代以上硬编码的方式
    2. public void test2() throws SQLException {
    3. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    4. DataSource dataSource = (DataSource) context.getBean("C3P0");
    5. Connection con = dataSource.getConnection();
    6. System.out.println(con);
    7. //com.mchange.v2.c3p0.impl.NewProxyConnection@52aa2946
    8. }

1.2 Spring整合DBCP

  • 导入DBCP连接池需要的包com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar和com.springsource.org.apache.commons.pool-1.5.3.jar

  • DBCP硬编码方式

    1. @Test //DBCP的硬编码方式
    2. public void test3() throws SQLException {
    3. BasicDataSource dataSource = new BasicDataSource();
    4. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    5. dataSource.setUrl("jdbc:mysql://localhost:3306/hibernate");
    6. dataSource.setUsername("root");
    7. dataSource.setPassword("2626");
    8. Connection con = dataSource.getConnection();
    9. System.out.println(con);
    10. //jdbc:mysql://localhost:3306/hibernate, UserName=root@localhost, MySQL-AB JDBC Driver
    11. }
  • Spring整合DBCP

  • 配置文件

    1. <!-- DBCP -->
    2. <bean id="DBCP" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
    3. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    4. <property name="url" value="jdbc:mysql://localhost:3306/hibernate"></property>
    5. <property name="username" value="root"></property>
    6. <property name="password" value="2626"></property>
    7. </bean>
  • 测试

    1. @Test
    2. public void test4() throws SQLException {
    3. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    4. DataSource dataSource = (DataSource) context.getBean("DBCP");
    5. Connection con = dataSource.getConnection();
    6. System.out.println(con);
    7. //jdbc:mysql://localhost:3306/hibernate, UserName=root@localhost, MySQL-AB JDBC Driver
    8. }

1.3 最终版

  • 最终版使用propertie配置文件,Spring加载properties文件

  • Spring提供了一个标签可以加载外部的properties文件内容

  • 导入context的名称空间和约束后,xml文件中才会有提示,这个约束在/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html中可以找到

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
    5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
    7. </beans>
  • 导入约束后配置xml

    1. <context:property-placeholder location="classpath:jdbc.properties"/>
    2. <!-- DBCP -->
    3. <bean id="DBCP" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
    4. <property name="driverClassName" value="${jdbc.driver}"></property>
    5. <property name="url" value="${jdbc.url}"></property>
    6. <property name="username" value="${jdbc.username}"></property>
    7. <property name="password" value="${jdbc.password}"></property>
    8. </bean>
  • 测试

    1. @Test
    2. public void test4() throws SQLException {
    3. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    4. DataSource dataSource = (DataSource) context.getBean("DBCP");
    5. Connection con = dataSource.getConnection();
    6. System.out.println(con);
    7. //jdbc:mysql://localhost:3306/hibernate, UserName=root@localhost, MySQL-AB JDBC Driver
    8. }
  • jdbc.properties配置文件可以配置不同的数据库,切换方便。

2. 基于注解的IOC配置

  • 注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置形式不一样。至于是使用xml还是注解,实际的开发过程中,每家公司有不同的习惯。

2.1 导包

  • 拷贝必备包到lib目录下。基于注解的配置中,需要加入一个aop的jar包。

2.2 配置文件

  • 基于注解的配置文件,导入约束时需要多导入一个context名称空间下的约束。约束的位置可以在约束的位置在:

    ​ ..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html中找到

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans
    6. http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context
    8. http://www.springframework.org/schema/context/spring-context.xsd ">
    9. </beans>

2.3 开启注解扫描器

  • 在配置文件中开启注解扫描器

    1. <!-- 开启注解扫描器
    2. com.itzhouq:包含自己以及自己下面的所有子包
    3. -->
    4. <context:component-scan base-package="com.itzhouq"></context:component-scan>
  • 告知Spring框架,在读取配置文件,创建容器时,依据注解创建对象,并存入容器中

2.4 使用注解

  • 要创建UserDaoImpl对象,在类上使用@Component注解。只要定义在类上,那么注解扫描器只要一扫描到就会创建该类的实例对象,放入Spring容器中。

    1. package com.itzhouq.daoImpl;
    2. import org.springframework.stereotype.Component;
    3. import com.itzhouq.dao.UserDao;
    4. @Component("userDao") //<bean id="userDao" class="com.itzhouq.daoImpl.UserDaoImpl"></bean>
    5. public class UserDaoImpl implements UserDao{
    6. @Override
    7. public void save() {
    8. System.out.println("操作数据库,保存用户的数据");
    9. }
    10. }
  • 要创建的对象UserServiceImpl,在类上使用注解,在属性上使用注解

  • @value("属性值"):定义在属性字段上,针对的是基本数据类型和String类型。如果使用了这个注解,该属性的set方法可以省略不写。

  • @Autowired:定义在属性字段上,针对的是对象类型。自动按照类型注入,当使用注解注入属性时,set方法可以省略。它只能注入其他bean类型。当有多个类型匹配时,使用要注解的对象变量名作为bean的id,在Spring容器查找,找到了也可以注入成功,找不到就报错。

  • @Qualifier("对象属性id"):定义在属性字段上。在自动按照类型注入的基础上,再按照Bean的id注入。他在给字段注入时,不能独立使用,必须和@Autowired一起使用。但是给方法参数注入时,可以独立使用。

    1. package com.itzhouq.serviceImpl;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.stereotype.Component;
    5. import com.itzhouq.dao.UserDao;
    6. import com.itzhouq.daoImpl.UserDaoImpl;
    7. import com.itzhouq.service.UserService;
    8. @Component("userService") //<bean id="UserService" class="com.itzhouq.serviceImpl.UserServiceImpl">
    9. public class UserServiceImpl implements UserService {
    10. @Value("要开始访问dao了") //<property name="name" value="要开始访问dao了"></property>
    11. private String name; //使用注解,可以不需要set方法,相当于直接赋值
    12. @Autowired //对象类型:自动去Spring容器中找有没有该类型(UserDao)的实例对象 如果有直接赋值
    13. @Qualifier("userDao")
    14. private UserDao userDao;
    15. public void setUserDao(UserDao userDao) {
    16. this.userDao = userDao;
    17. }
    18. @Override
    19. public void save() {
    20. System.out.println(name);
    21. //调用dao
    22. userDao.save();
    23. }
    24. }
  • 测试

    1. @Test
    2. public void test() {
    3. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    4. UserService userService = (UserService) context.getBean("userService");
    5. userService.save();
    6. //要开始访问dao了
    7. //操作数据库,保存用户的数据
    8. }

2.5了解的几个注解

  • @Scope("singleton") / @Scope("prototype"):定义在类上,用于指定该类是单实例还是多实例

    • 一般action/web层为多实例,service和dao层为单实例
  • @PostConstruct:定义在方法上,用于配置初始化方法
  • @PreDestroy:定义在方法上,用于配置销毁的方法

3. Spring整合JUnit

3.1 导入包

  • spring-aop-4.2.4.RELEASE.jar
  • spring-test-4.2.4.RELEASE.jar
  • junit.jar

3.2 编写测试类

  1. package com.itzhouq.test;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. import com.itzhouq.service.UserService;
  8. //1. 告诉Spring配置文件的位置
  9. //2. 告诉Spring谁去加载配置文件
  10. @ContextConfiguration(value="classpath:applicationContext.xml")
  11. @RunWith(value=SpringJUnit4ClassRunner.class)
  12. public class SpringJunit {
  13. @Autowired
  14. private UserService userService;
  15. @Test
  16. public void test() {
  17. userService.save();
  18. // 要开始访问dao了
  19. // 操作数据库,保存用户的数据
  20. }
  21. }

3.3 注解

  • 使用@RunWith注解替换原有运行器
  • 使用@ContextConfiguration指定spring配置文件的位置
  • 使用@Autowired给测试类中的变量注入数据

Spring笔记02_注解_IOC的更多相关文章

  1. Spring笔记04_AOP注解开发_模板_事务

    目录 1. Spring基于AspectJ的注解的AOP开发 1. 1 SpringAOP的注解入门 1.2 Spring的AOP的注解通知类型 1.2.1 @Before:前置通知 1.2.2 @A ...

  2. Spring笔记13--SSH--全注解开发

    SSH全注解开发: (1) 在Action类中添加注解,实现Struts2的注解开发(@NameSpace.@ParentPackage.@Action...) package com.tongji. ...

  3. spring笔记--通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

  4. spring笔记-@Primary注解

    1.问题 当一个接口有2个不同实现时,使用@Autowired注解时会报org.springframework.beans.factory.NoUniqueBeanDefinitionExceptio ...

  5. Spring学习笔记--使用注解装配

    使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...

  6. Spring笔记(5) - 声明式事务@EnableTransactionManagement注解源码分析

    一.背景 前面详解了实现Spring事务的两种方式的不同实现:编程式事务和声明式事务,对于配置都使用到了xml配置,今天介绍Spring事务的注解开发,例如下面例子: 配置类:注册数据源.JDBC模板 ...

  7. springmvc学习笔记(常用注解)

    springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...

  8. 学习笔记_J2EE_SpringMVC_03_注解配置_@RequestMapping用法

    @RequestMappingde的用法 摘要: 主要介绍注解@RequestMapping的用法 一.@RequestMapping 简介 在Spring MVC 中使用 @RequestMappi ...

  9. Spring笔记01_下载_概述_监听器

    目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ...

随机推荐

  1. hello1和hello2代码分析

    1.hello1代码分析 hello.java package javaeetutorial.hello1; import javax.enterprise.context.RequestScoped ...

  2. PHP 5.x和PHP 7 Closure不同行为问题

    同样一段闭包代码,PHP 7 ok的,PHP 5.5.11(Windows 开发机器)上却报错,以为是PHP 5 bug,原来是用法不对,记录一下~ 原代码(自己写的框架的路由部分)最初是这样写的: ...

  3. LoadRunner(一)——性能测试基础及性能指标概述

    参考学习感谢:<精通软件性能测试与LoadRunner实战> 一.典型的性能测试场景 某个产品要发布了,需要对全市的用户做集中培训.通常在进行培训的时候,老师讲解完成一个业务以后,被培训用 ...

  4. 第一个servlet程序

    在Eclipse中新建一个Dynamic Web Project 在WebContent下面添加index.jsp <%@ page language="java" cont ...

  5. 分布式数据中间件TDDL、Amoeba、Cobar、MyCAT架构比较

    框架比较 TDDL Amoeba Cobar MyCat 点评 TDDL不同于其它几款产品,并非独立的中间件,只能算作中间层,是以Jar包方式提供给应用调用.属于JDBC Shard的思想,网上也有很 ...

  6. [Swift]LeetCode812. 最大三角形面积 | Largest Triangle Area

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  7. VMware Workstation下安装Linux

    下载VMware Workstation thunder://QUFodHRwczovL2Rvd25sb2FkMy52bXdhcmUuY29tL3NvZnR3YXJlL3drc3QvZmlsZS9WT ...

  8. 4.DOM

    定义 文档对象模型(Document Object Model)是一种用于HTML和XML文档的编程接口. 查找元素 1.直接查找 document.getElementById 根据ID获取一个标签 ...

  9. BBS论坛(二十二)

    22.1.七牛js上传轮播图图片 (1)common/zlqiniu.js 'use strict'; var zlqiniu = { 'setup': function (args) { var d ...

  10. 基于 dubbo 的分布式架构

    前言 现在越来越多的互联网公司还是将自己公司的项目进行服务化,这确实是今后项目开发的一个趋势,就这个点再凭借之前的 SSM 项目来让第一次接触的同学能快速上手. 浅谈分布式架构 分布式架构单看这个名字 ...