不使用配置文件实现注解

SpringConfig

package cn.qut.config;

import org.springframework.context.annotation.*;
/**
        * 该类是一个配置类,它的作用和bean.xml是一样的
        * spring中的新注解
        * Configuration
        *     作用:指定当前类是一个配置类
        *     细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
        * ComponentScan
        *      作用:用于通过注解指定spring在创建容器时要扫描的包
        *      属性:
        *          value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
        *                 我们使用此注解就等同于在xml中配置了:
        *                      <context:component-scan base-package="com.itheima"></context:component-scan>
        *  Bean
        *      作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
        *      属性:
        *          name:用于指定bean的id。当不写时,默认值是当前方法的名称
        *      细节:
        *          当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
        *          查找的方式和Autowired注解的作用是一样的
        *  Import
        *      作用:用于导入其他的配置类
        *      属性:
        *          value:用于指定其他配置类的字节码。
        *                  当我们使用Import的注解之后,有Import注解的类就父配置类,而导入的都是子配置类
        *  PropertySource
        *      作用:用于指定properties文件的位置
        *      属性:
        *          value:指定文件的名称和路径。
        *                  关键字:classpath,表示类路径下
        */
//@Configuration
@Configuration
@ComponentScan("cn.qut")
@Import(jdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfig {



}


public class jdbcConfig {

    //Spring的EL表达式
    @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对象,并添加到IOC容器中
     * @param dataSource
     * @return
     */
    @Bean(name="runner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(@Qualifier("ds") DataSource dataSource){
        return new QueryRunner(dataSource);
    }

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

测试类

package cn.qut.dao;

import cn.qut.config.SpringConfig;
import cn.qut.enity.Account;
import cn.qut.service.IAccountService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class IAccountDaoTest {
   //直接使用了ApplicationContext的实现子类,要不然只能使用父类中定义过的方法
   private   AnnotationConfigApplicationContext as;
    private IAccountService accountService;
    @Before
    public  void  init(){
     //这句可以省略配置类中的@Configuration
     as = new AnnotationConfigApplicationContext(SpringConfig.class);
     accountService = (IAccountService) as.getBean("accountService");
    }
    @Test
    public void findAllAccount() {
        List<Account> accounts = accountService.findAllAccount();
        for (Account a : accounts
        ) {
            System.out.println(a);
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值