Spring_08_基于注解的IOC-spring的新注解,不使用bean.xml

Spring提供了一种新注解,可以不再使用bean.xml

1.创建一个SpringConfiguration配置类

package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.*;

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

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

}

2.创建一个JdbcConfig配置类

package config;

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

import javax.sql.DataSource;

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对象
    @Bean(name="runner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    //创建数据源对象
    @Bean(name="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);
        }

    }
}

3.在资源中新建一个jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3366/bjpowerbode
jdbc.username=root
jdbc.password=123

3.注意在SpringConfiguration配置类中导入子配置类和资源文件

在这里插入图片描述

4.再获取容器时,把代码改成以下,然后就可以测试了

        //1.获取容器
        ApplicationContext ac =new AnnotationConfigApplicationContext(SpringConfiguration.class);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值