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

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


一、Spring简介

什么是Spring?

Spring是一个开源的轻量级Java开发框架,以IOC和AOP为内核,提供SpringMVC(展现层)、Spring JDBC(持久层)、Spring Security(权限控制)、Spring AOP(面向切面编程)等多种应用技术,简化了应用程序的开发。

Spring的优势

(1)、方便解耦,简化开发:通过IOC容器,将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合。
(2)、AOP编程的支持:许多不容易用传统OOP实现的功能可以通过AOP轻松应付。
(3)、声明式事务的支持。
(4)、方便程序的测试。
(5)、方便集成各种优秀框架:Struts、Hibernate、MyBatis、Hession。
(6)、Spring的源码设计很精妙!

二、程序的耦合和解耦

  • 耦合:程序间的依赖关系。
    (1)、类之间的依赖。
    (2)、方法间的依赖。
  • 解耦:降低程序间的依赖关系。
  • 实际开发中,应该做到:
    编译期不依赖,运行时才依赖。
  • 解耦思路:
    第一步:使用反射来创建对象,而避免使用new关键字。
    第二步:通过读取配置文件来获取要创建的对象全限定类名。
  • 以下解耦实例从最开始的反射,到工厂模式,再到IOC模式。通过这种演变的模式来讲解Spring的IOC容器。

解耦实例1:JDBC驱动注册

JDBC操作中注册驱动时,我们不使用DriverManager的registerDriver方法去new一个com.mysql.jdbc.Driver(),而是通过反射加载驱动类。
Class.forName(“com.mysql.jdbc.Driver”)。

package com.itheima.jdbc;

import java.sql.*;
public class JdbcDemo1 {
    public static void main(String args[]) throws Exception {
        //1、注册驱动
        //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        //利用反射机制得到类,而避免直接调用类或方法。
        Class.forName("com.mysql.cj.jdbc.Driver");
        .......
    }
}

缺点:太死板,一旦把数据库改为其他类型数据库,比如Oracle或SqlServer该链接就不适用了。
于是常用另一个方法:通过读取配置文件来获取要创建的对象全限定类名。

解耦实例2:工厂模式解耦

在Web项目中,UI层,Service层,Dao层之间有着前后调用的关系。

public class MyServiceImpl implements IMyService {

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

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

解耦思路:工厂模式解耦

  • 何为工厂模式:
    在实际开发中可以把三层的对象的全类名都使用配置文件保存起来,当启动服务器应用加载的时候,创建这些对象的实例并保存在容器中。在获取对象时,不使用new的方式,而是直接从容器中获取,这就是工厂设计模式。
  • 一个创建Bean对象的工厂:抽象概念:工厂模式——>实际实现:配置文件。
    这个例子的工厂类是自己写,实际上spring自己有这个类,可以自己使用。

Beab:在计算机英语中,有可重用组件的含义
javaBean:用java语言编写的可重用组件。javabean>实体类
它就是创建我们的service和dao对象的。

  1. 项目架构:

在这里插入图片描述

  1. 需要将下面使用new去调用其它对象的写法改为bean的方式。

AccountServiceImpl.java

package com.itheima.service.impl;

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();
    //改用bean得到对象,减少new
    private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean("accountDao");
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

Client.java

package com.itheima.ui;

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

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

    public static void main(String args[]){
        //IAccountService as = new AccountServiceImpl();
        //改用bean获取对象
        IAccountService as = (IAccountService) BeanFactory.getBean("accountServiceBean");
        as.saveAccount();
    }
}

  1. 步骤
    第一步:需要一个配置文件来配置我们的service和dao配置的内容:唯一标识=全限定类名(key = value)

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

第二步:创建一个工厂类。
这个例子的工厂类是自己写的,实际上spring自己有这个类,下面springIOC的例子会介绍。

package com.itheima.factory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class BeanFactory {
    //定义一个Properties对象
    private static Properties props;
    //使用静态代码块为Properties对象赋值
    static {
       try {
            //实例化对象
            props = new Properties();
            //获取properties文件的流对象  getClassLoader():取得该Class对象的类装载器
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            props.load(in);
        } catch (IOException e) {
            throw new ExceptionInInitializerError("初始化properties失败!");
        }
    }
    /*
    * 根据Bean的名称获取bean对象
    * */
    public static Object getBean(String beanName){
        Object bean = null;
        try {
            //传进去一个bean的id,得到一个beanPath
            String beanPath = props.getProperty(beanName);
            //System.out.println(beanName);
            bean = Class.forName(beanPath).newInstance();//每次都会调用默认构造函数创建对象
        }catch (Exception e){
            e.printStackTrace();
        }
        return bean;
    }
}

第三步:通过读取配置文件中配置的内容,反射创建对象
配置文件可以是xml也可以是properties。


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

三、使用springIOC解决程序耦合

简单实例

与上面工厂模式例子所不同的是,不需要去创建bean工厂类,这里直接使用springIOC控制反转机制。
Beans也是spring框架的主要内容之一:
在这里插入图片描述
参考https://www.cnblogs.com/fg-fd/p/10003211.html。或搜索springIOC内容。。。

  1. 准备工作:创建maven项目,并准备三层接口类和实现类。
    创建maven项目,配置其pom.xml如下:
<?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>com.itheima</groupId>
    <artifactId>day01_test03</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--引入spring框架依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
           <version>5.3.0</version>
        </dependency>
    </dependencies>
</project>

创建三层接口类和实现类的结构如下,模拟一个保存账户的服务。
在这里插入图片描述
AccountDaoImpl

package com.itheima.dao.impl;

import com.itheima.dao.IAccountDao;

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


    public void saveAccount() {
        System.out.println("保存了账户");
    }
}

IAccountDao

package com.itheima.dao;
/*
* 账户的持久层接口
* */
public interface IAccountDao {
    /*
    * 模拟保存账户
    * */
    void saveAccount();
}

AccountServiceImpl

package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.dao.impl.AccountDaoImpl;
import com.itheima.service.IAccountService;
//账户的业务层实现类
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao = new AccountDaoImpl();
    //改用bean得到对象,减少new
    //private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean("accountDao");
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

IAccountService

package com.itheima.service;
/*
账户业务的接口
* */
public interface IAccountService {
    //模拟保存账户
    void saveAccount();
}

Client

package com.itheima.ui;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
* 模拟一个表现层,用于调用业务层
* */
public class Client {
/*
获取spring的Ioc核心容器,并根据id获取对象

ApplicationContext的三个常用实现类
    ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。
    FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
    AnnotationConfigApplicationContext:它是用于读取注解创建容器的。
核心容器的两个接口引发出的问题:
    ApplicationContext: 单例对象适用
        它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只有一读取完配置文件马上就创建配置文件中配置的对象。
    BeanFactory:    多例对象适用
        它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
* */
    public static void main(String args[]){
        //IAccountService as = new AccountServiceImpl();
        //1、获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取Bean对象
        IAccountService as = (IAccountService)ac.getBean("accountService");
        IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
        System.out.println(as);
        System.out.println(adao);
        as.saveAccount();
    }
}

  1. 配置bean: 在类的根路径下的resource目录下创建bean.xml文件,把对象的创建交给spring来管理.
    每个标签对应一个类,其class属性为该类的全类名,id属性为该类的id,在spring配置中,通过id获取类的对象.
<?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>
  1. 在表现层文件Client.java中通过容器创建对象.通过核心容器的getBean()方法获取具体对象.
package com.itheima.ui;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
* 模拟一个表现层,用于调用业务层
* */
public class Client {
/*
获取spring的Ioc核心容器,并根据id获取对象
* */
    public static void main(String args[]){
        //IAccountService as = new AccountServiceImpl();
        //1、获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取Bean对象
        IAccountService as = (IAccountService)ac.getBean("accountService");
        IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
        System.out.println(as);
        System.out.println(adao);
        as.saveAccount();
    }
}

ApplicationContext的三个常用实现类

  1. ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。
  2. FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
  3. AnnotationConfigApplicationContext:它是用于读取注解创建容器的。

核心容器的两个接口引发出的问题:

  1. ApplicationContext: 单例对象适用
    它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只有一读取完配置文件马上就创建配置文件中配置的对象。
  2. BeanFactory: 多例对象适用
    它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
    在这里插入图片描述

使用XML配置文件实现IOC

使用配置文件实现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的作用范围和生命周期

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

实例化Bean的三种方式

  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>

依赖注入

依赖注入的概念

依赖注入(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>

<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方法的成员. 其主要属性可大致分为两类:

  1. 指定要调用set方法赋值的成员字段
    1. name:要调用set方法赋值的成员字段
  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 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>

注入集合字段

集合字段及其对应的标签按照集合的结构分为两类: 相同结构的集合标签之间可以互相替换.

  1. 只有键的结构:

    1. 数组字段: 标签表示集合,标签表示集合内的成员.
    2. List字段: 标签表示集合,标签表示集合内的成员.
    3. Set字段: 标签表示集合,标签表示集合内的成员.
      其中,,标签之间可以互相替换使用.
  2. 键值对的结构:

    1. Map字段: 标签表示集合,标签表示集合内的键值对,其key属性表示键,value属性表示值.
    2. Properties字段: 标签表示集合,标签表示键值对,其key属性表示键,标签内的内容表示值.
      其中,标签之间,,标签之间可以互相替换使用.

下面使用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,要将注解写在类的定义中

常见注解

用于创建对象的注解

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

  1. @Component: 把当前类对象存入spring容器中,其属性如下:
    1. value: 用于指定当前类的id. 不写时默认值是当前类名,且首字母改小写
  2. @Controller: 将当前表现层对象存入spring容器中
  3. @Service: 将当前业务层对象存入spring容器中
  4. @Repository: 将当前持久层对象存入spring容器中
@Controller,@Service,@Repository注解的作用和属性与@Component是一模一样的,可以相互替代,它们的作用是使三层对象的分别更加清晰.
用于注入数据的注解

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

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

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

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

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

1. @PostConstruct: 用于指定初始化方法
2. @PreDestroy: 用于指定销毁方法

spring的半注解配置和纯注解配置

spring的注解配置可以与xml配置并存,也可以只使用注解配置。

半注解配置

在半注解配置下,spring容器仍然使用ClassPathXmlApplicationContext类从xml文件中读取IOC配置,同时在xml文件中告知spring创建容器时要扫描的包.

例如,使用半注解模式时,上述简单实例中的beans.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在创建容器时要扫描的包,配置此项所需标签不在beans的约束中,而在一个名为context的名称空间和约束中-->
    <context:component-scan base-package="cn.maoritian"></context:component-scan>
</beans>

然后将spring注解加在类的定义中.

纯注解配置

在纯注解配置下,我们用配置类替代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) {
        // 功能实现...
    }
}

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存放配置类,其中配置类代码如下:
    其中SpringConfiguration类为主配置类,内容如下:
@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);
        }
    }
}

参考:https://blog.csdn.net/ncepu_Chen/article/details/91903396?spm=1001.2014.3001.5502#t12

总结

提示:这里对文章进行总结:

  • 程序的耦合和解耦

参考

https://blog.csdn.net/ncepu_Chen/article/details/91903396?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7Edefault-15.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7Edefault-15.control

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Senye_ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值