Spring学习01:控制反转(IOC)与依赖注入(DI)

视频讲解:https://www.bilibili.com/video/av47952931?p=3

首先来说明什么是控制反转,为什么要提出IOC和DI

  • 耦合: 程序间的依赖关系.在开发中,应该做到解决编译期依赖,即编译期不依赖,运行时才依赖.
  • 解耦的思路: 使用反射来创建对象,而避免使用new关键字,并通过读取配置文件来获取要创建的对象全限定类名.

解耦实例1: JDBC驱动注册

JDBC操作中注册驱动时,我们不使用DriverManager的register方法,而采用Class.forName(“驱动类全类名”)的方式.

public static void main(String[] args) throws SQLException, ClassNotFoundException {
	//注册驱动的两种方式
	// 1. 创建驱动类的实例 
	//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
	// 2. 通过反射加载驱动类
	Class.forName("com.mysql.jdbc.Driver");		// 实际开发中此类名从properties文件中读取
	
	//...后续操作

即使驱动类不存在,在编译时也不会报错,解决了编译器依赖.顶多运行报错

解耦实例2: UI层,Service层,Dao层的调用(web开发常会遇到)

public class MyServiceImpl implements IMyService {

    private IMyDao myDao = new MyDaoImpl();	// 业务层要调用持久层的接口和实现类

    public void myService(){
        myDao.serviceProcess();
    }
}

业务层依赖持久层的接口和实现类,若编译时不存在没有持久层实现类,则编译将不能通过,这构成了编译期依赖

解决方案:在实际开发中可以把三层的对象的全类名都使用配置文件保存起来,当启动服务器应用加载的时候,创建这些对象的实例并保存在容器中. 在获取对象时,不使用new的方式,而是直接从容器中获取,这就是工厂设计模式.

①创建BeanFactory.java(用于生产对象)

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 一个创建Bean对象的工厂
 *
 * Bean:在计算机英语中,有可重用组件的含义。
 * JavaBean:用java语言编写的可重用组件。
 *      javabean >  实体类
 *
 *   它就是创建我们的service和dao对象的。
 *
 *   第一个:需要一个配置文件来配置我们的service和dao
 *           配置的内容:唯一标识=全限定类名(key=value)
 *   第二个:通过读取配置文件中配置的内容,反射创建对象
 *
 *   我的配置文件可以是xml也可以是properties
 */
public class BeanFactory {
    //定义一个Properties对象
    private static Properties props;

    //定义一个Map,用于存放我们要创建的对象。我们把它称之为容器
    private static Map<String,Object> beans;

    //使用静态代码块为Properties对象赋值
    static {
        try {
            //实例化对象
            props = new Properties();
            //获取properties文件的流对象
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            props.load(in);
            //实例化容器
            beans = new HashMap<String,Object>();
            //取出配置文件中所有的Key
            Enumeration keys = props.keys();
            //遍历枚举
            while (keys.hasMoreElements()){
                //取出每个Key
                String key = keys.nextElement().toString();
                //根据key获取value
                String beanPath = props.getProperty(key);
                //反射创建对象
                Object value = Class.forName(beanPath).newInstance();
                //把key和value存入容器中
                beans.put(key,value);
            }
        }catch(Exception e){
            throw new ExceptionInInitializerError("初始化properties失败!");
        }
    }

    /**
     * 根据bean的名称获取对象
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName){
        return beans.get(beanName);
    }

    /**
     * 根据Bean的名称获取bean对象
     * @param beanName
     * @return

    public static Object getBean(String beanName){
        Object bean = null;
        try {
            String beanPath = props.getProperty(beanName);
//            System.out.println(beanPath);
            bean = Class.forName(beanPath).newInstance();//每次都会调用默认构造函数创建对象
        }catch (Exception e){
            e.printStackTrace();
        }
        return bean;
    }*/
}

②创建配置文件bean.properties

accountService=com.itheima.service.impl.AccountServiceImpl
accountDao=com.itheima.dao.impl.AccountDaoImpl

③写一个程序入口,模拟表现层

import com.itheima.factory.BeanFactory;
import com.itheima.service.IAccountService;

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {

    public static void main(String[] args) {
        //IAccountService as = new AccountServiceImpl();
        for(int i=0;i<5;i++) {
            IAccountService as = (IAccountService) BeanFactory.getBean("accountService");
            System.out.println(as);
            as.saveAccount();
        }

    }
}

④service层

import com.itheima.dao.IAccountDao;
import com.itheima.factory.BeanFactory;
import com.itheima.service.IAccountService;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

//    private IAccountDao accountDao = new AccountDaoImpl();

    private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean("accountDao");

//    private int i = 1;

    public void  saveAccount(){
        int i = 1;
        accountDao.saveAccount();
        System.out.println(i);
        i++;
    }
}

④dao层

import com.itheima.dao.IAccountDao;

/**
 * 账户的持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    public  void saveAccount(){

        System.out.println("保存了账户");
    }
}

通过上面的实例可以看到,我们不再是手动的进行new对象,而是在工厂中去拿,spring的控制反转和属性注入帮我们做的就是生产对象的事情,也就是充当一个容器以工厂模式帮我们生产对象

使用springIOC解决程序耦合(充当工厂)

1 准备工作: 创建MAVEN项目,并准备三层接口类和实现类
创建maven项目,配置其pom.xml如下(引入spring):

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.maoritian</groupId>
    <artifactId>learnspring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    	<!-- 引入-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

创建三层接口类和实现类的结构如下,模拟一个保存账户的服务.(Client充当表现层)
在这里插入图片描述
2 配置bean: 在类的根路径下的resource目录下创建bean.xml文件,把对象的创建交给spring来管理.
每个标签对应一个类,其class属性为该类的全类名,id属性为该类的id,在spring配置中,通过id获取类的对象(相当于上面的bean.properties)

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">
 
     <!--把对象的创建交给spring来管理-->
     <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
     <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
 </beans>

3 在表现层文件Client.java中通过容器创建对象.通过核心容器的getBean()方法获取具体对象.

public class Client {
     public static void main(String[] args) {
         // 获取核心容器对象
         ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
         // 根据id获取Bean对象
         IAccountService as  = (IAccountService)ac.getBean("accountService");
         
         // 执行as的具体方法
         // ...
     }
 }

我们常用的容器有三种: ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,AnnotationConfigApplicationContext.

  • ClassPathXmlApplicationContext: 它是从类的根路径下加载配置文件
  • FileSystemXmlApplicationContext: 它是从磁盘路径上加载配置文件
  • AnnotationConfigApplicationContext: 读取注解创建容器

上面是一个大致的流程,那么我们下面具体讲解如何使用配置文件实现IOC,也就是如上所示的要将托管给spring的类写进bean.xml配置文件中.

bean标签
  • 作用: 配置托管给spring的对象,默认情况下调用类的无参构造函数,若果没有无参构造函数则不能创建成功
  • 属性:
  1. id: 指定对象在容器中的标识,将其作为参数传入getBean()方法可以获取获取对应对象.
  2. class: 指定类的全类名,默认情况下调用无参构造函数
  3. scope: 指定对象的作用范围,可选值如下
    1. singleton: 单例对象,默认值
    2. prototype: 多例对象
    3. request: 将对象存入到web项目的request域中
    4. session: 将对象存入到web项目的session域中
    5. global session: 将对象存入到web项目集群的session域中,若不存在集群,则global session相当于session
  4. init-method:指定类中的初始化方法名称,在对象创建成功之后执行
  5. destroy-method:指定类中销毁方法名称,对prototype多例对象没有作用,因为多例对象的销毁时机不受容器控制

实例化 Bean 的三种方式(spring是如何通过xml配置文件创建对象的)

  1. 使用默认无参构造函数创建对象: 默认情况下会根据默认无参构造函数来创建类对象,若Bean类中没有默认无参构造函数,将会创建失败.
<bean id="accountService" 
	class="cn.maoritian.service.impl.AccountServiceImpl"></bean>

  1. 使用指定的静态工厂的方法创建对象:
    创建静态工厂如下:
// 静态工厂,其静态方法用于创建对象
public class StaticFactory {
	public static IAccountService createAccountService(){
		return new AccountServiceImpl();
	}
}

使用StaticFactory类中的静态方法createAccountService创建对象,涉及到标签的属性:

  1. id属性: 指定对象在容器中的标识,用于从容器中获取对象
  2. class属性: 指定静态工厂的全类名
  3. factory-method属性: 指定生产对象的静态方法
<bean id="accountService"
	class="cn.maoritian.factory.StaticFactory"
	factory-method="createAccountService"></bean>

  1. 使用实例工厂的方法创建对象
    创建实例工厂如下:
public class InstanceFactory {
	public IAccountService createAccountService(){
		return new AccountServiceImpl();
	}
}

先创建实例工厂对象instanceFactory,通过调用其createAccountService()方法创建对象,涉及到标签的属性:

  1. factory-bean属性: 指定实例工厂的id
  2. factory-method属性: 指定实例工厂中生产对象的方法
<bean id="instancFactory" class="cn.maoritian.factory.InstanceFactory"></bean>
<bean id="accountService"
	factory-bean="instancFactory"
	factory-method="createAccountService"></bean>

bean的作用范围和生命周期

  1. 单例对象: scope=“singleton”
    1. 作用范围: 每个应用只有一个该对象的实例,它的作用范围就是整个应用
    2. 生命周期: 单例对象的创建与销毁 和 容器的创建与销毁时机一致
      1. 对象出生: 当应用加载,创建容器时,对象就被创建
      2. 对象活着: 只要容器存在,对象一直活着
      3. 对象死亡: 当应用卸载,销毁容器时,对象就被销毁
  2. 多例对象: scope=“prototype”
    1. 作用范围: 每次访问对象时,都会重新创建对象实例.
    2. 生命周期: 多例对象的创建与销毁时机不受容器控制
      1. 对象出生: 当使用对象时,创建新的对象实例
      2. 对象活着: 只要对象在使用中,就一直活着
      3. 对象死亡: 当对象长时间不用时,被 java 的垃圾回收器回收了

依赖注入

依赖注入的概念

依赖注入(Dependency Injection)是spring框架核心ioc的具体实现.

通过控制反转,我们把创建对象托管给了spring,但是代码中不可能消除所有依赖,例如:业务层仍然会调用持久层的方法,因此业务层类中应包含持久化层的实现类对象.
我们等待框架通过配置的方式将持久层对象传入业务层,而不是直接在代码中new某个具体的持久化层实现类,这种方式称为依赖注入.

依赖注入的方法

因为我们是通过反射的方式来创建属性对象的,而不是使用new关键字,因此我们要指定创建出对象各字段的取值.

使用构造函数注入

通过类默认的构造函数来给创建类的字段赋值,相当于调用类的有参构造方法.(这个时候无参构造方法不是必须的了)
涉及的标签: 用来定义构造函数的参数,其属性可大致分为两类:

  1. 寻找要赋值给的字段
    1. index: 指定参数在构造函数参数列表的索引位置
    2. type: 指定参数在构造函数中的数据类型
    3. name: 指定参数在构造函数中的变量名,最常用的属性
  2. 指定赋给字段的值
    1. value: 给基本数据类型和String类型赋值
    2. ref: 给其它Bean类型的字段赋值,ref属性的值应为配置文件中配置的Bean的id
public class AccountServiceImpl implements IAccountService {

    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;

    public AccountServiceImpl(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public void saveAccount() {
		System.out.println(name+","+age+","+birthday);
    }
}

<!-- 使用Date类的无参构造函数创建Date对象 -->
<bean id="now" class="java.util.Date" scope="prototype"></bean>
<!-- 构造方法有几个参数那么你就必须写几个name属性,不然就会报错,所以我们一般采用set的方式进行属性注入,而不采用构造方法,set方法注入更加的灵活-->
<bean id="accountService" class="cn.maoritian.service.impl.AccountServiceImpl">
	<constructor-arg name="name" value="myname"></constructor-arg>
	<constructor-arg name="age" value="18"></constructor-arg>
	<!-- birthday字段为已经注册的bean对象,其id为now -->
	<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>

使用set方法注入(更常用)

在类中提供需要注入成员属性的set方法,创建对象只调用要赋值属性的set方法.

涉及的标签: ,用来定义要调用set方法的成员. 其主要属性可大致分为两类:
在这里插入图片描述

public class AccountServiceImpl implements IAccountService {

	private String name;
	private Integer age;
	private Date birthday;

	public void setName(String name) {
		this.name = name;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);
	}
}

<!-- 使用Date类的无参构造函数创建Date对象 -->
<bean id="now" class="java.util.Date" scope="prototype"></bean>

<bean id="accountService" class="cn.maoritian.service.impl.AccountServiceImpl">
	<property name="name" value="myname"></property>
	<property name="age" value="21"></property>
	<!-- birthday字段为已经注册的bean对象,其id为now -->
	<property name="birthday" ref="now"></property>
</bean>

注入集合字段

在这里插入图片描述
下面使用set方法注入各种集合字段

public class AccountServiceImpl implements IAccountService {
	// 集合字段
	private String[] myArray;
	private List<String> myList;
	private Set<String> mySet;
	private Map<String,String> myMap;
	private Properties myProps;

	// 集合字段的set方法
	public void setMyStrs(String[] myArray) {
		this.myArray = myArray;
	}
	public void setMyList(List<String> myList) {
		this.myList = myList;
	}
	public void setMySet(Set<String> mySet) {
		this.mySet = mySet;
	}
	public void setMyMap(Map<String, String> myMap) {
		this.myMap = myMap;
	}
	public void setMyProps(Properties myProps) {
		this.myProps = myProps;
	}
	
	@Override
	public void saveAccount() {
		System.out.println(Arrays.toString(myArray));
		System.out.println(myList);
		System.out.println(mySet);
		System.out.println(myMap);
		System.out.println(myProps);
	}
}

<bean id="accountService" class="cn.maoritian.service.impl.AccountServiceImpl3">
	<property name="myStrs">
		<array>
			<value>value1</value>
			<value>value2</value>
			<value>value3</value>
		</array>
	</property>

	<property name="myList">
		<list>
			<value>value1</value>
			<value>value2</value>
			<value>value3</value>
		</list>
	</property>

	<property name="mySet">
		<set>
			<value>value1</value>
			<value>value2</value>
			<value>value3</value>
		</set>
	</property>

	<property name="myMap">
		<map>
			<entry key="key1" value="value1"></entry>
			<entry key="key2">
				<value>value2</value>
			</entry>
			
		</map>
	</property>

	<property name="myProps">
		<props>
			<prop key="key1">value1</prop>
			<prop key="key2">value2</prop>
		</props>
	</property>
</bean>

使用注解实现IOC

使用注解实现IOC,要将注解写在类的定义中(不需要再去写那么负责的xml配置文件了)

常用注解

用于创建对象的注解

这些注解的作用相当于bean.xml中的标签
1. @Component: 把当前类对象存入spring容器中,其属性如下
2. @Controller: 将当前表现层对象存入spring容器中
3. @Service: 将当前业务层对象存入spring容器中
4. @Repository: 将当前持久层对象存入spring容器中

都有一个属性value: 用于指定当前类的id. 不写时默认值是当前类名的首字母改小写
@Controller,@Service,@Repository注解的作用和属性与@Component是一模一样的,可以相互替代,它们的作用是使三层对象的分别更加清晰.

用于注入数据的注解

这些注解的作用相当于bean.xml中的标签.

  1. @Autowired: 自动按照成员变量类型注入
    1. 注入过程
      1. 当spring容器中有且只有一个对象的类型与要注入的类型相同时,注入该对象.
      2. 当spring容器中有多个对象类型与要注入的类型相同时,使用要注入的变量名作为bean的id(id在@Qualifier注解上),在spring 容器查找,找到则注入该对象.找不到则报错.
    2. 出现位置: 既可以在变量上,也可以在方法上
    3. 细节: 使用注解注入时,set方法可以省略
  2. @Qualifier: 在自动按照类型注入的基础之上,再按照bean的id注入
    1. 出现位置: 既可以在变量上,也可以在方法上.注入变量时不能独立使用,必须和@Autowire一起使用; 注入方法时可以独立使用.
    2. 属性:
      value: 指定bean的id
  3. @Resource: 直接按照bean的id注入,它可以独立使用.独立使用时相当于同时使用@Autowired和@Qualifier两个注解.
    属性name: 指定bean的id
  4. @Value: 注入基本数据类型和String类型数据
    属性value: 用于指定数据的值,可以使用el表达式(${表达式})
用于改变作用范围的注解

这些注解的作用相当于bean.xml中的标签的scope属性.

  1. @Scope: 指定bean的作用范围
    属性value: 用于指定作用范围的取值,“singleton”,“prototype”,“request”,“session”,“globalsession”
和生命周期相关的注解

这些注解的作用相当于bean.xml中的标签的init-method和destroy-method属性

  1. @PostConstruct: 用于指定初始化方法
  2. @PreDestroy: 用于指定销毁方法
基于注解的示例(可以说是半注解,因为目前仍然会用到.xml文件,用到指定要扫描的包)
  1. bean.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"
       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">

    <!-- 告知spring在创建容器时要扫描的包 -->
    <context:component-scan base-package="com.itheima"></context:component-scan>
    <!--配置QueryRunner,由于这个类是别人已经写好的类,所以我们不可能去通过添加注解去实现IOC,只能在.xml文件中进行配置-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--注入数据源-->
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

    <!-- 配置数据源 由于这个类是别人已经写好的类,所以我们不可能去通过添加注解去实现IOC,只能在.xml文件中进行配置-->
    <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/eesy"></property>
        <property name="user" value="root"></property>
        <property name="password" value="1234"></property>
    </bean>
</beans>
  1. 测试类(程序入口)
import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * 使用Junit单元测试:测试我们的配置
 */
public class AccountServiceTest {

    @Test
    public void testFindAll() {
        //1.获取容易
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        List<Account> accounts = as.findAllAccount();
        for(Account account : accounts){
            System.out.println(account);
        }
    }

    @Test
    public void testFindOne() {
        //1.获取容易
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        Account account = as.findAccountById(1);
        System.out.println(account);
    }

    @Test
    public void testSave() {
        Account account = new Account();
        account.setName("test");
        account.setMoney(12345f);
        //1.获取容易
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        as.saveAccount(account);

    }

    @Test
    public void testUpdate() {
        //1.获取容易
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        Account account = as.findAccountById(4);
        account.setMoney(23456f);
        as.updateAccount(account);
    }

    @Test
    public void testDelete() {
        //1.获取容易
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.得到业务层对象
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        as.deleteAccount(4);
    }
}

  1. 涉及到的实体类
import java.io.Serializable;

/**
 * 账户的实体类
 */
public class Account implements Serializable {

    private Integer id;
    private String name;
    private Float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

  1. 业务层
import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 账户的业务层实现类
 */
@Service("accountService")
public class AccountServiceImpl implements IAccountService{

    @Autowired
    private IAccountDao accountDao;

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    @Override
    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccount(Integer acccountId) {
        accountDao.deleteAccount(acccountId);
    }
}

  1. dao层
import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 账户的持久层实现类
 */
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

    @Autowired
    private QueryRunner runner;



    @Override
    public List<Account> findAllAccount() {
        try{
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Account findAccountById(Integer accountId) {
        try{
            return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveAccount(Account account) {
        try{
            runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateAccount(Account account) {
        try{
            runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAccount(Integer accountId) {
        try{
            runner.update("delete from account where id=?",accountId);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

纯注解配置(不需要.xml文件来指定要扫描的包在哪,相比半注解可能会更加的繁琐,所以开发中常用注解与.xml配置文件一起用的方式)

在纯注解配置下,我们用配置类替代bean.xml,spring容器使用AnnotationApplicationContext类从spring配置类中读取IOC配置
纯注解配置下的注解:

  1. @Configuration: 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解.获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class).
  2. @ComponentScan: 指定spring在初始化容器时要扫描的包,作用和bean.xml 文件中<context:component-scan base-package=“要扫描的包名”/>是一样的. 其属性如下:
    1. basePackages: 用于指定要扫描的包,是value属性的别名
  3. @Bean: 该注解只能写在方法上,表明使用此方法创建一个对象,并放入spring容器,其属性如下
    1. name: 指定此方法创建出的bean对象的id
    2. 细节: 使用注解配置方法时,如果方法有参数,Spring框架会到容器中查找有没有可用的bean对象,查找的方式与@Autowired注解时一样的.
  4. @PropertySource: 用于加载properties配置文件中的配置.例如配置数据源时,可以把连接数据库的信息写到properties配置文件中,就可以使用此注解指定properties配置文件的位置,其属性如下:
    1. value: 用于指定properties文件位置.如果是在类路径下,需要写上"classpath:"
  5. @Import: 用于导入其他配置类.当我们使用@Import注解之后,有@Import注解的类就是父配置类,而导入的都是子配置类. 其属性如下:
    1.value: 用于指定其他配置类的字节码

实例: 使用纯注解配置实现数据库CRUD

  1. 项目结构: 其中包cn.maoritian存放业务代码,包config存放配置类. dao层选用DBUtils和c3p0.
    在这里插入图片描述
  2. 包cn.maoritian存放业务代码,其中dao层实现类和service层实现类的代码如下:
    dao层实现类:
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

    @Autowired	// 自动从spring容器中寻找QueryRunner类型对象注入给runner成员变量
    private QueryRunner runner;		// DBUtil对象,用来执行SQL语句

    public List<Account> findAllAccount() {
		// 功能实现...
    }

    public void saveAccount(Account account) {
        // 功能实现...
    }

    public void deleteAccount(Integer accountId) {
        // 功能实现...
    }
}

  1. service层实现类:
@Service("accountService")
public class AccountServiceImpl implements IAccountService{

    @Autowired	// 自动从spring容器中寻找IAccountDao类型对象注入给accountDao成员变量
    private IAccountDao accountDao;	// dao层对象,用来执行数据持久化操作

    public List<Account> findAllAccount() {
		// 功能实现...
    }

    public void saveAccount(Account account) {
        // 功能实现...
    }

    public void deleteAccount(Integer accountId) {
        // 功能实现...
    }
}

  1. 包config存放配置类,其中配置类代码如下:
    其中SpringConf类为主配置类,内容如下:
@Configuration					// 说明此类为配置类
@ComponentScan("cn.maoritian")	// 指定初始化容器时要扫描的包
@Import(JdbcConfig.class)		// 引入JDBC配置类
public class SpringConfiguration {
}

其中JDBCConfig类为JDBC配置类,内容如下:

@Configuration									// 说明此类为配置类
@PropertySource("classpath:jdbc.properties")	// 指定配置文件的路径,关键字classpath表示类路径
public class JdbcConfig {

    @Value("${jdbc.driver}")	// 为driver成员属性注入值,使用el表达式
    private String driver;

    @Value("${jdbc.url}")		// 为url成员属性注入值,使用el表达式
    private String url;

    @Value("${jdbc.username}")	// 为usernamer成员属性注入值,使用el表达式
    private String username;

    @Value("${jdbc.password}")	// 为password成员属性注入值,使用el表达式
    private String password;

    // 创建DBUtils对象
    @Bean(name="runner")	// 此将函数返回的bean对象存入spring容器中,其id为runner
    @Scope("prototype")		// 说明此bean对象的作用范围为多例模式,以便于多线程访问
    public QueryRunner createQueryRunner(@Qualifier("ds") DataSource dataSource){
		// 为函数参数datasource注入id为ds的bean对象
        return new QueryRunner(dataSource);
    }

    // 创建数据库连接池对象
    @Bean(name="ds")		// 此将函数返回的bean对象存入spring容器中,其id为ds
    public DataSource createDataSource(){
        try {
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
            return ds;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值