Spring_新注解_hehe.employment.over.34.3

34.4 spring的新注解-Configuration和ComponentScan

  • 创建SpringConfiguration.java 该类是一个配置类,它的作用和bean.xml是一样的
  • 说明两个注解:
    • @Configuration
      • 作用:指定当前类是一个配置类
      • 细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
    • @ComponentScan(“com.xww”)
      • 作用:用于通过注解指定spring在创建容器时要扫描的包
      • 属性:
        • value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
      • 我们使用此注解就等同于在xml中配置了: <context:component-scan base-package="com.itheima"></context:component-scan>

34.5 spring的新注解-Bean

  • 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中.
  • 属性:
    • name:用于指定bean的id。当不写时,默认值是当前方法的名称.
  • 细节:
    • 当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
    • 查找的方式和Autowired注解的作用是一样的
  • 示例:
    • SpringConfiguration.java
package config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

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

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

    /**
     * 创建数据源对象
     * @return
     */
    @Bean(name="ds2")
    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("1234");
            return ds;
        }catch (Exception e){
    
    
            throw new RuntimeException(e);
        }
    }

}


34.6 AnnotationConfigApplicationContext的使用

package com.itheima.test;

import config.SpringConfiguration;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 测试queryrunner是否单例
 */
public class QueryRunnerTest {
    
    

    @Test
    public  void  testQueryRunner(){
    
    
        //1.获取容易
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        //2.获取queryRunner对象
        QueryRunner runner = ac.getBean("runner",QueryRunner.class);
        QueryRunner runner1 = ac.getBean("runner",QueryRunner.class);
        System.out.println(runner == runner1);
    }
}

34.7 spring的新注解-Import

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

import org.springframework.context.annotation.*;

import javax.sql.DataSource;

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

}


34.8 spring的新注解-PropertySource

  • 作用: 用于指定properties文件的位置
  • 属性:
    • value:指定文件的名称和路径。
    • 关键字:classpath,表示类路径下。
  • 示例:
package config;

import org.springframework.context.annotation.*;

import javax.sql.DataSource;

//@Configuration
@ComponentScan("com.itheima")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
    
    

}


34.9 Qualifier注解的另一种用法

  • @Qualifier(“ds2”)的用法,可以在方法的形参中指定你要注入的数据源名称
 package config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;

/**
 * 和spring连接数据库相关的配置类
 */
public class JdbcConfig {
    
    

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

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

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

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

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

    /**
     * 创建数据源对象
     * @return
     */
    @Bean(name="ds2")
    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);
        }
    }

    @Bean(name="ds1")
    public DataSource createDataSource1(){
    
    
        try {
    
    
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass(driver);
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy02");
            ds.setUser(username);
            ds.setPassword(password);
            return ds;
        }catch (Exception e){
    
    
            throw new RuntimeException(e);
        }
    }
}

34.10 Spring整合junit的配置

  • 1、导入spring整合junit的jar(坐标)
    在这里插入图片描述
  • 2、使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的 @Runwith
  • 3、告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置
    • @ContextConfiguration
      • locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
      • classes:指定注解类所在地位置
        在这里插入图片描述
  • note:当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上

猜你喜欢

转载自blog.csdn.net/qq_44686266/article/details/114626713