基于Java的配置
组件注册
@Configuration
java 类文件标注@Configuration后即为Spring的配置文件
public class AnnotationTest {
@Configuration
static class Config{}
@Test
public testContainnerCreateWithJavaConfig() {
ApplicationContext container =
new AnnotationConfigApplicationContext(Config.class);
assertThat(container).isNotNull();
}
}
@Bean 注册一个bean
public class AnnotationTest {
@Configuration
static class Config{
@Bean
public Person person () {
return new Person("Tom",5);
}
}
@Data
@ AllArgsConstructor
static class Person {
private String name;
private int age;
}
@Test
public testBean() {
ApplicationContext container =
new AnnotationConfigApplicationContext(Config.class);
assertThat(container.getBean("person")).isNotNull();
}
}
Import
Import({类})
快速注册组件,不用在config类文件内用@Bean标注直接用@Import标注在类文件上,并指明要注册到容器的组件类
public class AnnotationTest {
@Configuration
@Import({Person.class})
static class Config{}
@Test
void testImport(){
// Import 注册的组件的默认name属性为类的全路径名称
assertThat(container.containsBean("xxx.xxx.Person")).isTrue()
}
}
ImportSelector
ImportSelector是一个接口,可以将实现了它的一个类赋值给@Import的value属性
public class AnnotationTest {
@Configuration
@Import({Person.class,MyImportSelector.class})
static class Config{}
static class MyImportSelector implements ImportSelector{
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{"springlearning.annotation.bean.Student"};
}
}
@Test
void testImportSelector(){
// Import 注册的组件的默认name属性为类的全路径名称
assertThat(container.containsBean("springlearning.annotation.bean.Student")).isTrue()
}
}
ImportBeanDefinitionRegistrar
//实现ImportBeanDefinitionRegistrar
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(Student.class);
registry.registerBeanDefinition("importedStudent",beanDefinition );
}
}
//在配置文件中用@Import({MyImportBeanDefinitionRegistrar}的方式向容器注册bean,bean的name为` registry.registerBeanDefinition("importedStudent",beanDefinition );`中指定的beaName:importedStudent
//config类
@Configuration
@Import({MyImportBeanDefinitionRegistrar.class})
public class JavaConfig {}
//在测试类中的测试方法
@Test
void testImportBeanDefinitionRegistrar(){
assertThat(container.containsBean("importedStudent")).isTrue();
}
FactoryBean
public class AnnotationTest {
@Configuration
static class Config{
@Bean
public Person person () {
return new Person("Tom",5);
}
@Bean
public MyFactoryBeam myFactoryBean() {
return new MyFactoryBean ();
}
}
@Data
@ AllArgsConstructor
static class Person {
private String name;
private int age;
}
public MyFactoryBeam implements FactoryBean<Person> {
@OVerride
public Person getObject(){
return new Person
}
@Override
public Class getType() {
}
@Override
public boolean isSingleton() {
return false;
}
}
@Test
public testFactoryBean() {
ApplicationContext container =
new AnnotationConfigApplicationContext(Config.class);
Object bean = container.getBean("myFactoryBean");
//FactoryBean 定义的bean,类型是FacoryBean.getObject()返回的类型,尽管在config类中,@Bean标注的方法是返回一个FactoryBean的实现。
assertThat(container.getBean("myFactoryBean")
.getClass()).isEqualTo(Person.class);
}
}