Spring study notes new annotations and integration of Junit

@Configuration

作用:用于指定当前类是一个配置类,当创建容器时会从该类上加载注解。相当于bean.xml文件。
属性:value用于指定配置类的字节码
//示例代码:
@Configuration//添加该注解后该类就是spring的配置类,相当于bean.xml
public class SpringConfiguration {
    
    
}

@ComponentScan

作用:用于指定 spring 在初始化容器时要扫描的包。作用和在 spring 的 xml 配置文件中的:
<context:component-scan base-package="com.itheima"/>是一样的。
属性:basePackages用于指定要扫描的包,和@Configuration注解中的 value 属性作用一样。
//示例代码:
@Configuration
@ComponentScan("com.zy.account")//指定要扫描的包
public class SpringConfiguration {
    
    
}

@Bean

作用:该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器。
属性:
name:给当前@Bean 注解方法创建的对象指定一个名称(即 bean 的 id)。如果不写默认是方法名称。
//示例代码:
public class JdbcConfig {
    
    
    @Bean(name = "dataSources")
    public DataSource createDataSources(){
    
    
        try {
    
    
            ComboPooledDataSource dataSources = new ComboPooledDataSource();
            dataSources.setDriverClass("com.mysql.jdbc.Driver");
            dataSources.setJdbcUrl("jdbc:mysql://localhost:3306/stu");
            dataSources.setUser("root");
            dataSources.setPassword("123456");
            return dataSources;
        }catch (Exception e){
    
    
            throw new RuntimeException();
        }
    }
}

@Import

作用:用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解。当然,写上也没问
题。
属性:value[]:用于指定其他配置类的字节码。
//示例代码:
/**
 * 主配置类,相当于bean.xml文件
 * */
@Configuration
@ComponentScan("com.zy.account")//包扫描
@Import(value = JdbcConfig.class)//导入子配置类
public class SpringConfiguration {
    
    
}
/**
*子配置类
*/
public class JdbcConfig {
    
    
    @Bean(name = "queryRunner")
    public QueryRunner createQueryRunner(DataSource dataSource){
    
    
        return new QueryRunner(dataSource);
    }
    @Bean(name = "dataSources")
    public DataSource createDataSources(){
    
    
        try {
    
    
            ComboPooledDataSource dataSources = new ComboPooledDataSource();
            dataSources.setDriverClass("com.mysql.jdbc.Driver");
            dataSources.setJdbcUrl("jdbc:mysql://localhost:3306/stu");
            dataSources.setUser("root");
            dataSources.setPassword("123456");
            return dataSources;
        }catch (Exception e){
    
    
            throw new RuntimeException();
        }
    }
}

@PropertySource

/*作用:用于加载.properties 文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到
properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。
属性:value[]用于指定 properties 文件位置。如果是在类路径下,需要写上 classpath:
jdbc.properties文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stu
jdbc.username=root
jdbc.password=123456*/
//示例代码:
//配置类
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;

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

    @Bean(name = "dataSources")
    public DataSource createDataSources(){
    
    
        try {
    
    
            ComboPooledDataSource dataSources = new ComboPooledDataSource();
            dataSources.setDriverClass(driver);
            dataSources.setJdbcUrl(url);
            dataSources.setUser(username);
            dataSources.setPassword(password);
            return dataSources;
        }catch (Exception e){
    
    
            throw new RuntimeException();
        }
    }
}
/**
 * 主配置类,相当于bean.xml文件
 * */
@Configuration
@ComponentScan("com.zy.account")//包扫描
@Import(value = {
    
    JdbcConfig.class})//导入子配置类
@PropertySource(value = "classpath:jdbc.properties")//加载properties配置文件
public class SpringConfiguration {
    
    
}

Spring integrates Junit

Import the integrated jar package.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>

Test code (configuration file method), this method is developed by us using xml, and there is a spring configuration file:

Explain the two usages of @ContextConfiguration annotation:
locations attribute: used to specify the location of the configuration file. If it is under the classpath, it needs to be represented under the classpath with classpath.
classes attribute: used to specify the annotation class. When the xml configuration is not used, that is, the annotation development is used, and this attribute needs to be used to specify the location of the annotation class.

@RunWith(SpringJUnit4ClassRunner.class)//把Junit的main方法替换成Spring提供的
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
    
    
    
    @Autowired
    private AccountServie accountServie;
    
    @Test
    public void testSelectAll(){
    
    
        /*ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        AccountServie accountServie = (AccountServie) context.getBean("accountServie");*/
        List<Account> list = accountServie.selectAll();
        for (Account accounts:list){
    
    
            System.out.println(accounts);
        }
    }
 }

Test code (pure annotation method):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
    
    

    @Autowired
    private AccountServie accountServie;

    @Test
    public void testSelectAll(){
    
    
        /*ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        AccountServie accountServie = (AccountServie) context.getBean("accountServie");*/
        List<Account> list = accountServie.selectAll();
        for (Account accounts:list){
    
    
            System.out.println(accounts);
        }
    }
}

operation result:
Insert picture description here

Come on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/113107299