注解
@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的注入与数据初始化。