Spring:控制反转(IOC)与依赖注入(DI)
程序的耦合和解耦
- 耦合: 程序间的依赖关系.在开发中,应该做到解决编译期依赖,即编译期不依赖,运行时才依赖.
- 解耦的思路: 使用反射来创建对象,而避免使用new关键字,并通过读取配置文件来获取要创建的对象全限定类名.
- 下面以两个例子来说明如何解耦.
解耦实例1: JDBC驱动注册
JDBC操作中注册驱动时,我们不使用DriverManager的==registerDriver(new com.mysql.jdbc.Driver())方法,而采用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文件中读取
//...后续操作
}
查看com.mysql.jdbc.Driver类的源码如下,在类加载和初始化时,会执行static代码块中的部分,也就是说加载类的时候就自动注册驱动了.
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
static {
try {
java.sql.DriverManager.registerDriver(new Driver()); // 类初始化时执行注册动作
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
public Driver() throws SQLException {
// Required for Class.forName().newInstance()
}
}
解决耦合的思路: 工厂模式解耦
在实际开发中可以把三层的对象的全类名都使用配置文件保存起来,当启动服务器应用加载的时候,创建这些对象的实例并保存在容器中. 在获取对象时,不使用new的方式,而是直接从容器中获取,这就是工厂设计模式.
使用springIOC解决程序耦合
简单实例
准备工作: 创建MAVEN项目,并准备三层接口类和实现类 创建maven项目,配置其pom.xml如下:
<?xml version=”1.0” encoding=”UTF-8”?>
4.0.0
com.itheima
day01_eesy_03spring
1.0-SNAPSHOT
jar
org.springframework
spring-context
5.0.2.RELEASE
创建三层接口类和实现类的结构如下,模拟一个保存账户的服务.
配置bean: 在类的根路径下的resource目录下创建bean.xml文件,把对象的创建交给spring来管理.
每个标签对应一个类,其class属性为该类的全类名,id属性为该类的id,在spring配置中,通过id获取类的对象. <?xml version=”1.0” encoding=”UTF-8”?>
在表现层文件Client.java中通过容器创建对象.通过核心容器的==getBean()==方法获取具体对象.
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
IAccountService as = (IAccountService)ac.getBean("accountService");
// IAccountService as = ac.getBean(“accountService”,IAccountService.class);
IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
System.out.println(as);
System.out.println(adao);
我们常用的容器有三种:
ApplicationContext的三个常用实现类:
ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用)
FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
AnnotationConfigApplicationContext:它是用于读取注解创建容器的.
核心容器的两个接口引发出的问题:
ApplicationContext: 单例对象适用 采用此接口它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
BeanFactory: 多例对象适用
它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
使用XML配置文件实现IOC
使用配置文件实现IOC,要将托管给spring的类写进bean.xml配置文件中.
实例化beng的三种方式
1 第一种方式:使用默认构造函数创建。
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
- id: 指定对象在容器中的标识,将其作为参数传入==getBean()==方法可以获取获取对应对象.
class: 指定类的全类名,默认情况下调用无参构造函数
2 第二种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
- factory-bean 属性:用于指定实例工厂 bean 的 id。
factory-method 属性:用于指定实例工厂中创建对象的方法。
3 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
factory-method 属性:指定生产对象的静态方法
bean标签
- 作用:用于配置对象让 spring 来创建的。默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。
属性
id: 给对象在容器中提供一个唯一标识。用于获取对象。
class: 指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。
scope: 指定对象的作用范围。
作用:用于指定bean的作用范围
取值: 常用的就是单例的和多例的
singleton:单例的(默认值)
prototype:多例的
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
bean 的作用范围和生命周期
单例对象: scope=“singleton”
一个应用只有一个对象的实例。它的作用范围就是整个引用。
生命周期:对象出生:当应用加载,创建容器时,对象就被创建了。
对象活着:只要容器在,对象一直活着。
对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
多例对象: scope=“prototype”
每次访问对象时,都会重新创建对象实例。
生命周期:对象出生:当使用对象时,创建新的对象实例。
对象活着:只要对象在使用中,就一直活着。
对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。
依赖注入
依赖注入的概念
依赖注入: Dependency Injection。 它是 spring 框架核心 ioc 的具体实现。
我们的程序在编写时, 通过控制反转, 把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。
ioc 解耦只是降低他们的依赖关系,但不会消除。 例如:我们的业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系, 在使用 spring 之后, 就让 spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
依赖注入的方法
因为我们是通过反射的方式来创建属性对象的,而不是使用new关键字,因此我们要指定创建出对象各字段的取值.
使用构造函数注入
使用的标签:< constructor-arg >
标签出现的位置:bean标签的内部
标签中的属性
1 寻找要赋值给的字段
- type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
- index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始
- name:用于指定给构造函数中指定名称的参数赋值最常用常用的
2 指定赋给字段的值
- value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的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("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);
}
}
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="泰斯特"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<!-- 配置一个日期对象 -->
<bean id="now" class="java.util.Date"></bean>
使用set方法注入(更常用)
在类中提供需要注入成员属性的set方法,创建对象只调用要赋值属性的set方法.
涉及的标签:< property >
出现的位置:bean标签的内部
标签的属性:
- name:用于指定注入时所调用的set方法名称
- value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象的id
public class AccountServiceImpl2 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;
}
public void saveAccount(){
System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);
}
}
<bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
<property name="name" value="TEST" ></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
<!-- 配置一个日期对象 -->
<bean id="now" class="java.util.Date"></bean>
注入集合字段
集合字段及其对应的标签按照集合的结构分为两类: 相同结构的集合标签之间可以互相替换.
只有键的结构:
1. 数组字段: <array>标签表示集合,<value>标签表示集合内的成员.
2. List字段: <list>标签表示集合,<value>标签表示集合内的成员.
3. Set字段: <set>标签表示集合,<value>标签表示集合内的成员.
4. 其中<array>,<list>,<set>标签之间可以互相替换使用.
键值对的结构:
- Map字段: 标签表示集合,标签表示集合内的键值对,其key属性表示键,value属性表示值.
- Properties字段: 标签表示集合,标签表示键值对,其key属性表示键,标签内的内容表示值.其中,标签之间,,标签之间可以互相替换使用.
下面使用set方法注入各种集合字段
public class AccountServiceImpl3 implements IAccountService {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
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;
}
public void saveAccount(){
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(myProps);
}
}
<bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
<property name="myStrs">
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
<property name="myList">
<array>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</array>
</property>
<property name="mySet">
<list>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</list>
</property>
<property name="myMap">
<props>
<prop key="testC">ccc</prop>
<prop key="testD">ddd</prop>
</props>
</property>
<property name="myProps">
<map>
<entry key="testA" value="aaa"></entry>
<entry key="testB">
<value>BBB</value>
</entry>
</map>
</property>
</bean>
使用注解实现IOC
使用注解实现IOC,要将注解写在类的定义中
常用注解
用于创建对象的注解
他的作用就和在bean.xml配置文件中编写一个
- @Component:用于把当前类对象存入spring容器中
属性:value:用于指定< bean >的id。当我们不写时,它的默认值是当前类名,且首字母改小写。 - @Controller:一般用在表现层
- @Service:一般用在业务层
- @Repository:一般用在持久层
- @Controller,@Service,@Repository三个注解他们的作用和属性与==@Component==是一模一样。他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰
用于注入数据的
他的作用就和在bean.xml配置文件中的
@Autowired: 自动按照成员变量类型注入
注入过程:
- 当spring容器中有且只有一个对象的类型与要注入的类型相同时,注入该对象.
- 当spring容器中有多个对象类型与要注入的类型相同时,使用要注入的变量名作为bean的id,在spring 容器查找,找到则注入该对象.找不到则报错.
- 出现位置:可以是变量上,也可以是方法上
- 细节:在使用注解注入时,set方法就不是必须的了。
@Qualifier:在按照类中注入的基础之上再按照名称注入。它在给类成员注入时不能单独使用。必须和==@Autowire==一起使用,但是在给方法参数注入时可以
- 属性: value用于指定注入bean的id。
@Resource直接按照bean的id注入。它可以独立使用
- 属性:name用于指定bean的id。
- @Autowired,@Qualifier,@Resource三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。另外,集合类型的注入只能通过XML来实现。
@Value:用于注入基本类型和String类型的数据
- 属性:value用于指定数据的值。它可以使用spring中SpEL(也就是spring的el表达式)SpEL的写法:${表达式}
用于改变作用范围的注解
他的作用就和在
@Scope: 指定bean的作用范围
- 属性:value用于指定作用范围的取
- 值:“singleton”,“prototype”,“request”,“session”,“globalsession”
用于生命周期的注解
他的作用就和在
- @PostConstruct: 用于指定初始化方法
- @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配置
纯注解配置下的注解
@Configuration:用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解.获取容器时需要使用:AnnotationApplicationContext(有@Configuration注解的类.class).
- 细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
@ComponentScan:用于通过注解指定spring在创建容器时要扫描的包
- 属性 value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
- 我们使用此注解就等同于在xml中配置了:
@Bean:该注解只能写在方法上,表明使用此方法创建一个对象,并放入spring容器,其属性如下:
- 属性 name:用于指定bean的id。当不写时,默认值是当前方法的名称
- 细节 当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。查找的方式和Autowired注解的作用是一样的
@Import:用于导入其他配置类.当我们使用@Import注解之后,有@Import注解的类就是父配置类,而导入的都是子配置类. 其属性如下:
- value: 用于指定其他配置类的字节码
@PropertySource: 用于加载properties配置文件中的配置.例如配置数据源时,可以把连接数据库的信息写到properties配置文件中,就可以使用此注解指定properties配置文件的位置,其属性如下:
- 属性 value:指定文件的名称和路径。关键字:classpath:,表示类路径下
实例: 使用纯注解配置实现数据库CRUD
- 项目结构: 其中包cn.itheima存放业务代码,包config存放配置类. dao层选用DBUtils和c3p0.
包cn.itheima存放业务代码,其中dao层实现类和service层实现类的代码如下:
@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) {
// 功能实现...
}
}
包config存放配置类,其中配置类代码如下:
其中SpringConfiguration类为主配置类,内容如下:@ComponentScan(“com.itheima”) // 指定初始化容器时要扫描的包
@Import(JdbcConfig.class) // 引入JDBC配置类
@PropertySource(“classpath:jdbcConfig.properties”) // 指定配置文件的路径,关键字classpath表示类路径
public class SpringConfiguration {
}
其中JdbcConfig类为JDBC配置类,内容如下:
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);
}
}
}
其中:jdbcConfig.properties如下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy
jdbc.username=root
jdbc.password=root
Spring整合Junit单元测试:测试我们的配置
导入spring整合junit的jar(坐标)
org.springframework
spring-test
5.0.2.RELEASE
@Runwith注解把原有的main方法替换了,替换成spring提供的:SpringJUnit4ClassRunner.class
告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置
- @ContextConfiguration:ioc创建是基于xml还是注解的
- 属性 locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
- 属性 classes:指定注解类所在地位置
Spring整合junit的配置如下
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testFindAll() {
//功能实现
}
@Test
public void testFindOne() {
//功能实现
}
@Test
public void testSave() {
//功能实现
}
}
还没有评论,来说两句吧...