SpringBoot容器相关注解

1.@Configuration 配置类

User bean类:

package com.ttpfx.bean;

public class User {
    private String name;
    private int age;
}

AppConfig 配置类:

@Configuration(proxyBeanMethods = false)
public class AppConfig {
    @Bean
    public User user() {
        return new User("zhangsan", 30);
    }
}

主程序类:

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);

        // 容器获取User
        User user1 = context.getBean(User.class);
        User user2 = context.getBean(User.class);

        // 调用AppConfig方法获取User
        AppConfig appConfig = context.getBean(AppConfig.class);
        User user3 = appConfig.user();

        System.out.println(user1 == user2);
        System.out.println(user1 == user3);
    }
}

proxyBeanMethods属性说明:
proxyBeanMethods默认为true,为true时调用配置类的方法得到的User类是容器中的组件类,即user1 == user3 为true
proxyBeanMethods为false时,调用配置类的方法得到的User类是重新实例化的,即user1 == user3 为false

2. @Import

导入组件,可用于导入第三方类,导入组件名为全类名

Pet bean类:

package com.ttpfx.bean;

public class Pet {
    private String name;
}

AppConfig 配置类:

@Configuration()
@Import({Pet.class})
public class AppConfig {
}

主程序类:

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
        System.out.println(context.containsBean("com.ttpfx.bean.Pet"));
    }
}

3. @Conditional系列

当符合某种条件时,配置类才会生效:

@Configuration
//@ConditionalOnBean(Pet.class)
@ConditionalOnMissingBean(Pet.class)
public class AppConfig {
    @Bean
    public User user() {
        return new User("zhangsan", 30);
    }
}

4. @ImportResource

引入xml配置文件

配置类:

@Configuration
@ImportResource("beans.xml")
public class AppConfig {
}

xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="zhangsan" class="com.ttpfx.bean.User">
        <property name="name" value="张三"></property>
        <property name="age" value="20"></property>
    </bean>

    <bean id="lisi" class="com.ttpfx.bean.User">
        <property name="name" value="李四"></property>
        <property name="age" value="20"></property>
    </bean>
</beans>

5. ConfigurationProperties 配置绑定

application.properties:

user.wangwu.name=wangwu
user.wangwu.age=30

用法1:@ConfigurationProperties + @Component

User:

@Component
@ConfigurationProperties(prefix = "user.wangwu")
public class User {
    private String name;
    private int age;
}

用法2:@ConfigurationProperties + @EnableConfigurationProperties

User:

@ConfigurationProperties(prefix = "user.wangwu")
public class User {
    private String name;
    private int age;
}

配置类:

@Configuration
@EnableConfigurationProperties({User.class})
public class AppConfig {
}
上一篇:Django3.2 新特性


下一篇:通过DOI号来获取相关文献并下载PDF