SpringBoot配置类整理

注解

@Configuration: 指明当前类是一个配置类来替代之前的Spring配置文件,Spring boot的配置类,相当于Spring的配置文件。

  • Spring,通过配置文件添加组件
  • Spring boot,通过配置类的方式添加组件

@ComponentScan:作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中
@Bean:将方法的返回值添加到容器中

使用示例

TestService

public class TestService{
    public void say(String name) {
        System.out.println("****testService***" + name);
    }
}

ApplicationConfig

import com.test.springboot.service.TestService;
import org.springframework.context.annotation.*;

/**
 * @Configuration:注解告诉springboot当前类是一个配置类,是来替代之前的spring配置文件。
 * 在配置文件中用<bean></bean>标签添加组件
 */
@Configuration
@ComponentScan(basePackages = {"com.test.springboot"})
public class ApplicationConfig{

    //将方法的返回值添加到容器中,容器中这个组件默认的ID是方法名
    @Bean("testService")
    public TestService testService() {
        System.out.println("配置类@bean给容器中添加组件了");
        return new TestService();
    }
}

测试类

import com.test.springboot.bean.Person;
import com.test.springboot.service.TestService;
import config.MyAppConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * springboot单元测试
 * 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {

    @Autowired
    Person person;
    @Autowired
    ApplicationContext ioc;

    @Test
    public void testService() {
        System.out.println("****************************************");
        ApplicationContext context = new AnnotationConfigApplicationContext(MyAppConfig.class);
        TestService testService = (TestService) context.getBean("testService");
        System.out.println(helloService);
        boolean flag = context.containsBean("testService");
        System.out.println("bean是否存在:" + flag);

        helloService.say("Ricsot");
    }
}

自定义配置

import com.alibaba.druid.pool.DruidDataSource;
import com.example.demo.properties.JdbcPro;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
@Slf4j
@Configuration
//手动加载自定义配置文件
@PropertySource(value = {"classpath:jdbc.properties"}, encoding = "utf-8")
public class DataSouce1Config {
    @Value("${my.name}")
    private String name ;
    @Value("${spring.datasource.url}")
    private String dbUrl;

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

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

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;


    @Bean
    @Primary
    public DataSource dataSource(){

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(this.dbUrl);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setDriverClassName(driverClassName);
        log.info("cccccccccccccccc");
        log.info(this.name);
        return druidDataSource;
    }

}

其中${···}中的内容在resources的配置文件中添加。上述以初始化配置数据库为例

用途分析

为容器中注入我们自定义的以及SpringBoot为我们提供的Bean。在系统启动时利用Spring的注入机制向应用程序中初始化部分内容,使用在拦截器等Bean的注入与数据初始化。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值