Spring学习(二)之IOC的注解使用

一、Spring基于注解的IOC

1、常用的IOC注解

曾经XML的配置:

<bean id="accountService" class="com.itheima.serviceimpl.AccountServiceImpl" scope="" init-method="" destroy-method="">
    <property name="" value=""/ref=""></property>
</bean>

注解按作用分类:
(1) 用于创建对象的
他们的作用就和在XML配置文件中编写一个bean标签实现的功能是一样的

  • @Component:

作用:用于把当前对象存入spring容器中
属性:
  value:用于指定bean的id,当我们不写时,它的默认值是当前类名,且首字母改小写。
写法:
  @Component(value=“accountService”) 当只有一个属性时,value可以省略
  即:@Component(“accountService”)

Demo:

@Component
public class AccountServiceImpl implements IAccountService {
     
    private IAccountDao accountDao;
    public AccountServiceImpl(){
     
        System.out.println("项目创建了");
    }
    @Override
    public void saveAccount() {
     
        accountDao.saveAccount();
    }
}

添加上面的注解@Component,就会自动加入spring容器
但在此前,需要在配置文件添加context约束,因为需要使用到context标签,且这时需要的jar是aop的jar包。
添加context约束,依旧去spring官方文档,按Ctrl + F,搜索xmlns:context
Spring学习(二)之IOC的注解使用_第1张图片
复制这段beans标签的配置覆盖之前的,就行(PS:记得添加结束标签)
配置文件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在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中-->
    <context:component-scan base-package="com.itheima"></context:component-scan>
</beans>
@Component
public class AccountServiceImpl implements IAccountService {
     
    private IAccountDao accountDao;

    public AccountServiceImpl(){
     
        System.out.println("项目创建了");
    }

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

主函数:
public static void main(String[] args) {
     
        //1、获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取bean对象
        IAccountService as = (IAccountService) ac.getBean("accountServiceImpl");  //因为没主动赋值bean对象的id值,所以默认id值为类名首字母小写
        System.out.println(as);
    }

运行结果:
Spring学习(二)之IOC的注解使用_第2张图片
衍生的注解

  • @Controller:一般用在表现层
  • @Service:一般用在业务层
  • @Repository:一般用在持久层

以上三个注解他们的作用和属性与Compent是一模一样。
他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰,如果某个类不属于三层,则可以用Component

(2) 用于注入数据的
他们的作用就和在XML配置文件中的bean标签中写一个property标签的作用是一样的

  • @Autowired:

作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类匹配,就可以注入成功。
出现位置:
     可以是变量上,也可以是方法上
细节:
   在使用注解注入时,set方法就不是必须的了。

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
     
    private IAccountDao accountDao;

    public AccountServiceImpl(){
     
        System.out.println("项目创建了");
    }
    @Override
    public void saveAccount() {
     
        accountDao.saveAccount();
    }
}

accountDao变量为null,调用saveAccount()方法,一运行就会报空指针异常,不过使用@Autowired,自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类匹配,就可以注入成功。

原理分析

Spring学习(二)之IOC的注解使用_第3张图片
如果IOC容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
Spring学习(二)之IOC的注解使用_第4张图片
至少要有一个符合要求的bean。

如果容器中有多个类型匹配时,先匹配类型,其次按变量名当作bean的id进行筛选,匹配成功则注入成功,反之则报错。
在这里插入图片描述
Spring学习(二)之IOC的注解使用_第5张图片
所以当容器中出现多个类型匹配时,只能通过改变变量名称去匹配,这样就很麻烦。

  • @Qualifier

作用:在按照类中注入的基础之上再按照名称注入。它在给类成员注入时不能单独使用。但是在给方法参数注入时可以。
属性:
   value:用于指定注入bean的id。

Demo:
AccountDaoImpl类:

@Repository("accountDao1")
public class AccountDaoImpl implements IAccountDao {
     
    @Override
    public void saveAccount() {
     
        System.out.println("保存了账户11111111");
    }
}

AccountDaoImpl2类:

@Repository("accountDao2")
public class AccountDaoImpl2 implements IAccountDao {
     
    @Override
    public void saveAccount() {
     
        System.out.println("保存了账户2222222");
    }
}

AccountServiceImpl类:

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
     

    @Autowired
    @Qualifier("accountDao1")
    private IAccountDao accountDao;
    public AccountServiceImpl(){
     
        System.out.println("项目创建了");
    }
    @Override
    public void saveAccount() {
     
        accountDao.saveAccount();
    }
}

运行结果:
Spring学习(二)之IOC的注解使用_第6张图片
如果为@Qualifier(“accountDao2”),则运行结果为 :
Spring学习(二)之IOC的注解使用_第7张图片
但是如果没有@Autowired(将其注释掉),运行结果为:
在这里插入图片描述
为什么空指针异常? 原因是没有注入进去,换句话说@Qualifier不能单独使用!

由于上面两个存在着限制,所以引入下面的注解

  • @Resource:

作用:直接按照bean的id注入。它可以独立使用
属性:
   name:用于指定bean的id。

如:

@Resource(name = "accountDao1")
private IAccountDao accountDao;

注意:
这里的属性不是value,而是name,且不可省略,跟上述例子一样,否则如下:
在这里插入图片描述
一般省略,都是value属性才可以。

以上三个注入都只能注入其他bean类型的数据,而基本数据类型和String类型的注入无法使用上述注解实现。
另外,集合类型的注入只能通过XML来实现。

而基本数据类型和String类型的注入可以通过下面这个注解实现:

  • @Value:

作用:用于注入基本类型和String类型的数据
属性:
   value:用于指定数据的值。它可以使用spring中SpEL(也就是Spring的EL表达式)
  SpEL的写法:${表达式}

(3) 用于改变作用范围的
他们的作用就和在bean标签中使用scope属性实现的功能是一样的
@Scope:

作用:用于指定bean的作用范围
属性:
   value:指定范围的取值。常用取值:singleton、prototype

默认值为singleton,@Scope(“singleton”)可以不写。
Demo:多例

@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
     
    @Resource(name = "accountDao1")
    private IAccountDao accountDao;
    public AccountServiceImpl(){
     
        System.out.println("项目创建了");
    }
    @Override
    public void saveAccount() {
     
        accountDao.saveAccount();
    }
}

IAccountService as = (IAccountService) ac.getBean("accountService");
IAccountService as2 = (IAccountService) ac.getBean("accountService");
System.out.println(as == as2);   //false

(4) 和生命周期相关(了解)
他们的作用就和在bean标签中使用init-method和destroy-method的作用是一样的

PreDestroy
  作用:用于指定销毁方法
PostConstruct
  作用:用于指定初始化方法

Demo:

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
     
    @Resource(name = "accountDao1")
    private IAccountDao accountDao;
    public AccountServiceImpl(){
     
        System.out.println("项目创建了");
    }
    @PostConstruct
    public void Init() {
     
        System.out.println("项目初始化了。。。");
    }
    @PreDestroy
    public void Destroy() {
     
        System.out.println("项目销毁了。。。");
    }
    @Override
    public void saveAccount() {
     
        accountDao.saveAccount();
    }
}
public class Client {
     
    public static void main(String[] args) {
     
        //1、获取核心容器对象
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取bean对象
        IAccountService as = (IAccountService) ac.getBean("accountService");
        as.saveAccount();
        //关闭容器
        ac.close();
    }
}

运行结果:
Spring学习(二)之IOC的注解使用_第8张图片

2、Spring的新注解(不使用xml的注解IOC容器)

以上的那些注解都是只针对我们自己写的类,而那些jar包里面的类,我们无法对其加注解,只能通过xml配置去注入,这就是我们现在遇到的问题
Spring学习(二)之IOC的注解使用_第9张图片
我们要怎么做,才能不使用xml,就可以将jar包的类加入spring容器,并注入数据?
我们将使用下面的新注解,来实现上图中xml配置的功能,从而代替xml。
(1)@Configuration:指定当前类是一个配置类

细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。(下面有讲解)

(2)@ComponentScan:用于通过注解指定spring在创建容器时要扫描的包

属性:
  value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。

我们使用此注解就等同于在xml中配置了:

<context:component-scan base-package="com.itheima"></context:component-scan>

@ComponentScans是一个@ComponentScan数组。
例:

@ComponentScans(value = {
     @ComponentScan("com.itheima"),@ComponentScan("com.itheima.dao")})
@ComponentScan("com.itheima")

Demo:下面这个类就是配置类了

@Configuration
@ComponentScan("com.itheima")
public class SpringConfiguration {
     
	//。。。
}

(3)@Bean:用于把当前方法的返回值作为bean对象存入spring的ioc容器中

属性:
   name:用于指定bean的id。当不写时,默认值时当前方法的名称
细节:
   当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
   查找的方式和@Autowired注解的作用是一样的

Spring学习(二)之IOC的注解使用_第10张图片
但是对第一个使用注解@Bean之后,他们效果就一样了。

Demo:

@Configuration
@ComponentScan("com.itheima")
public class SpringConfiguration {
     

    /**
     * 用于创建一个QueryRunner对象
     * @param dataSource
     * @return
     */
    @Bean(name = "runner")
    public QueryRunner createQueryRunner(DataSource dataSource){
     
        return new QueryRunner(dataSource);
    }

    /**
     * 创建数据源对象
     * @return
     */
    @Bean("dataSource")
    public DataSource createDataSource(){
     
        try {
     
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass("com.mysql.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy");
            ds.setUser("root");
            ds.setPassword("123456");
            return ds;
        }catch (Exception e){
     
            throw new RuntimeException(e);
        }
    }
}

当把配置文件bean.xml删掉之后
Spring学习(二)之IOC的注解使用_第11张图片
之前Spring学习(一)说过ApplicationContext的三个常用实现类

  • ClassPathXmlApplicationContext
  • FileSystemApplicationContext
  • AnnotationConfigApplicationContext

前两个需要用到配置文件,而第三个刚好符合,是解决我们现在的问题的方法,不过它提供的构造方法中,有个需要被@Configuration注解过的配置类的字节码当作参数。
而且该配置类如果被AnnotationConfigApplicationContext作为对象创建的参数时,@Configuration可以不写。

public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
     ...}

所以我们只要将

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//换成下面这个AnnotationConfigApplicationContext,就可以获取容器
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);`

测试一下:

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

运行结果:
Spring学习(二)之IOC的注解使用_第12张图片
还存在一个问题,就是我们在配置文件中,QueryRunner对象应该是多例的。
因为有很多Dao层的对象,如果是QueryRunner是单例的话,那么每次访问数据库,都是同一个QueryRunner对象。
假设dao1对象还没操作完,dao2对象要访问数据库,这时可能造成数据出现脏读等现象。
所以这里要在QueryRunner配置那里加个@Scope注解,将其改成多例对象。

@Bean(name = "runner")
@Scope("prototype")
public QueryRunner createQueryRunner(DataSource dataSource){
     
	return new QueryRunner(dataSource);
}

测试:

/**
 * 测试queryrunner是否单例
 */
public class QueryRunnerTest {
     
    @Test
    public void testQueryRunner(){
     
        //获取容器
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        //获取QueryRunner对象
        QueryRunner runner = ac.getBean("runner",QueryRunner.class);
        QueryRunner runner1 = ac.getBean("runner",QueryRunner.class);
        System.out.println(runner == runner1);
    }
}

运行结果:
Spring学习(二)之IOC的注解使用_第13张图片
虽然当配置类作为AnnotationConfigApplicationContext对象创建的参数时,一定会去扫描类中里面的东西,所以@Configuration可以不写。

但如果配置类不是作为参数的话呢?

Demo:
SpringConfiguration 类作为AnnotationConfigApplicationContext的参数,假设它只用来配置一些公共的。

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
package config;
@ComponentScan({
     "com.itheima"})
public class SpringConfiguration {
     ...}

这时在config包下,新建一个JdbcConfig类,是用来配置和spring连接数据库相关的配置类。

public class JdbcConfig {
     
    /**
     * 用于创建一个QueryRunner对象
     * @param dataSource
     * @return
     */
    @Bean(name = "runner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(DataSource dataSource){
     
        return new QueryRunner(dataSource);
    }

    /**
     * 创建数据源对象
     * @return
     */
    @Bean("dataSource")
    public DataSource createDataSource(){
     
        try {
     
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass("com.mysql.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy");
            ds.setUser("root");
            ds.setPassword("123456");
            return ds;
        }catch (Exception e){
     
            throw new RuntimeException(e);
        }
    }
}

测试:
Spring学习(二)之IOC的注解使用_第14张图片
还是报错,spring容器找不到类型为QueryRunner的bean对象。

第一种方式
难道是因为@ComponentScan没有添加config包的原因?

@ComponentScan({
     "com.itheima","config"})

但加完还是报这样的错!!!
因为spring扫描该类时,前提是要认定该类是个配置类,这样才会去扫描该类里面的注解。
所以要在JdbcConfig类上加@Configuration,而且不能省略。

注意:如果要扫描的包和配置类是同一个包的话,直接写包名就行,不要写包名的路径,不要还是会报错,如:写config就行,不要写。com.config

第二种方式
如果不想加@Configuration的话,可以在AnnotationConfigApplicationContext的参数加上JdbcConfig的字节码,这时扫描包的时候,也不用加config包了,因为AnnotationConfigApplicationContext会去扫描JdbcConfig类里面的东西。

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class,JdbcConfig.class);

但是如果这样的话,SpringConfiguration类 和 JdbcConfig类 就成了并列关系,不存在所谓的哪个是大类,哪个是小类了。

第三种方式
如果想要说SpringConfiguration类作为一个综合的配置类,里面包含着若干个像JdbcConfig类这样的小配置类,不想在AnnotationConfigApplicationContext的构造函数中写太多class参数的话,就要采用第一种方式。
如果两种方式都不想使用的话,可以使用@Import注解。
(4)@Import:用于导入其他的配置类

属性:
   value:用于指定其他配置类的字节码。
当我们使用Import的注解之后,有Import注解的类就父配置类,而导入的都是子配置类。

package config;
@ComponentScan("com.itheima")
@Import(JdbcConfig.class)
public class SpringConfiguration {
     ...}

JdbcConfig类中还存在的问题
Spring学习(二)之IOC的注解使用_第15张图片
这里写死了,要怎么做,才能将他们摘出来?
创建配置文件将这些字符串存起来,通过读取配置文件,将值赋给各变量
配置文件:jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy
jdbc.username=root
jdbc.password=123456

然后创建变量,通过@Value注解注入

    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private  String username;

    @Value("${jdbc.password}")
    private String password;
    
    @Bean("dataSource")
    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);
        }
    }

${jdbc.driver}中的jdbc.driver是properties中的key值

但现在的问题是如何读取properties里的东西?

(4)@PropertySource:指定文件的名称和路径

属性:
   value:指定文件的名称和路径。
        关键字:classpath:表示类路径下(即:后面的是类路径)

@PropertySource("classpath:jdbcConfig.properties")
//有包就写包,没有就算了
@PropertySource("classpath:com/resource/jdbcConfig.properties")

3、Spring整合Junit

分析问题:
Spring学习(二)之IOC的注解使用_第16张图片
这些重复的代码,该怎么减少重复呢?

	private ApplicationContext ac;
    private IAccountService as;

    @Before
    public void init(){
     
        //1、获取容器
        ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        //2、得到业务层对象
        as = ac.getBean("accountService",IAccountService.class);
    }

如果这样处理,对开发人员来说的确会这样写,但是对于测试人员来说,他的关注点在于@Test后面的测试方法,测试功能是否符合要求,而不是关注于如何获取spring容器,如何解耦之类的问题,有可能他不懂spring,就写不出这样的代码。
所以对于测试人员来说上面那段@Before以下的代码是不需要的。

如果通过@Autowired去自动注入的话,是否可行?

@Autowired
private IAccountService as;

运行结果如下:
在这里插入图片描述
还是报空指针异常???继续分析

1、应用程序的入口
		 	main方法
2、junit单元测试中,没有main方法也能执行
			junit集成了一个main方法
			该方法就会判断当前测试类中哪些方法有@Test注解
			junit就让有Test注解的方法执行
3、junit不会管我们是否采用spring框架
			即:在执行测试方法时,junit根本不知道我们是不是使用了spring框架
			所以也就不会为我们读取配置文件/配置类创建spring核心容器
4、由以上三点可知
			当测试方法执行时,没有IOC容器,就算写了Autowired注解,也无法实现注入。

所以为什么会出现空指针异常,是因为as初始值就是为null,@Autowired发挥不了作用。
由于junit的jar都是class文件,所以改不了,那么我们就改变思路,将junit中不能加载容器的main方法换成可以加载的main方法。

Spring整合junit的配置

  1. 导入spring整合junit的jar(坐标)
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.2.RELEASE</version>
    <scope>test</scope>
</dependency>
  1. 使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的
    (1) @RunWith
    属性是一个value,Class value()
    需要继承Runner的类的字节码

    @RunWith(SpringJUnit4ClassRunner.class) //SpringJUnit4ClassRunner就是spring-test中继承Runner的类
    public class AccountServiceTest {
           ...}
    
  2. 告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置
    (2) @ContextConfiguration
    localtions:指定xml文件的位置,加上classpath关键字,表示在类路径下
    classes:指定注解类所在地位置

    //配置类
    @ContextConfiguration(classes = SpringConfiguration.class)
    //xml
    @ContextConfiguration(locations = "classpath:bean.xml")
    

当我们使用JUnit版本为4.11或以下版本时,运行后会报错。
Spring学习(二)之IOC的注解使用_第17张图片
因为有个细节:当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上

你可能感兴趣的:(Spring,spring)