Spring新注解Configuration、ComponentScan、Bean 、Import 、PropertySource

8 篇文章 0 订阅

Spring新注解

@Configuration

表明当前类是一个配置类。他的作用和bean,xml一样
细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写

@ComponentScan/ComponentScans

-------- 作用:用于Spring 在创建容器时要扫描的包
--------属性value/basePackages:两者的功能都是一样的 <context:component-scan base-package=“com.itheima”></context:component-scan>

@Bean

*Bean

  • 作用:用于把当前方法的返回值作为bean对象存入Spring的IOC容器中(Map结构:<key ,value> key => id ,value=>要创建的对象)
  • 属性:
  •       name:用于指定bean的id 默认值:不写时默认值当前方法的名称
    
  •       细节:当我们使用注解配置方法时。如果方法有参数,spring框架会去容器查找
    
  •       有没有可以使用的bean对象,查找的方式和Autowired的方法是一样的
    

在进行注解开发的时候,还是有bean,xml文件的 存在,为了让Spring IOC纯注解开发,所有Spring还有其他的注解。
如下面的context:component-scan标签,配置QueryRunner的bean标签,配置数据源的标签.
如何用注解达到去除bean…xml。

在这里插入图片描述

创建配置类:
SpringConfiguration.java

package com.itheima.config;

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

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/*
* 该类是一个配置类,他的 作用是和bean.xml一样的
* Spring中的新注解
* Configuration
*           作用:当前类是一个配置类
*ComponentScan
*           作用:用于Spring 在创建容器时要扫描的包
*           属性value/basePackages:两者的功能都是一样的 <context:component-scan base-package="com.itheima"></context:component-scan>
*Bean
*   作用:用于把当前方法的返回值作为bean对象存入Spring的IOC容器中(Map结构:<key ,value>  key => id ,value=>要创建的对象)
*   属性:
*           name:用于指定bean的id 默认值:不写时默认值当前方法的名称
*           细节:当我们使用注解配置方法时。如果方法有参数,spring框架会去容器查找
*           又名没有可以使用的bean对象,查找的方式和Autowired的方法是一样的
*           */
@Configuration
@ComponentScan(basePackages = "com.itheima")
public class SpringConfiguration {
    //创建一个QueryRunner对象
    @Bean(name = "runner")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    @Bean(name ="dataSource")
    public DataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass("com.mysql.cj.jdbc.Driver");
        ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy?serverTimezone=UTC&useSSL=false&useUnicode=true &characterEncoding=UTF-8");
        ds.setUser("root");
        ds.setPassword("123456");
        return ds;
    }
}

除了创建配置类之外,还需要在获取核心容器对象的地方更改为通过注解获取核心容器对象
AnnotationConfigApplicationContext(配置类的类名.class);

//1.获取核心容器对象
        //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

在这里插入图片描述

@Import注解

除了主配置类之外还有其他的小的配置的类:当小的配置类既不是AnnotationConfigApplicationContext的参数 ,也没有在小配置类表明要扫描的包同时也不写Configuration注解表面他是个配置类,那么就需要在主配置类上面使用@Import注解。

Import的作用:用于导入其他的配置类
value:用于指定其他配置类的字节码,当我们使用@Import注解之后,有@Import注解的类为主配置类,而导入的都是子配置类。
在这里插入图片描述

在这里插入图片描述

@PropertySource

作用:用于指定properties文件的位置
属性
value:指定文件的名称和路径
关键字classpath:表示类路径下
在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值