美文网首页
Spring 注解@Configuration,@Bean,@v

Spring 注解@Configuration,@Bean,@v

作者: Tinyspot | 来源:发表于2022-12-23 10:01 被阅读0次

1. @Configuration

  • @Configuration 代表当前类是一个配置类
  • 配置类也是组件,默认是单实例的
  • @Bean 构建一个实例,放入 Spring 容器中
@Configuration
public class UserConfig {
    /**
      * 方法名 user() 相当于 id="user"
      * 返回值 User 相当于 class="com.xxx.User"
      */
    @Bean
    public User user() {
        return new User("tinyspot", 20);
    }
}

等价

<beans>
    <bean id="user" class="com.xxx.User">
      <property name="name" value="tinyspot" />
      <property name="age" value="20" />
    </bean>
</beans>

1.1 proxyBeanMethods 配置

  • Full 模式(proxyBeanMethods = true), 代理对象调用方法,保证单实例
  • Lite 模式(proxyBeanMethods = false)
  • 组件之间无依赖关系,用 Lite 模式加速容器启动,减少判断
  • 组件之间有依赖关系,需要获取之前的单实例,用 Full 模式

2. Bean 配置

  • Java-based Container Configuration
  • @Configuration equivalent beans.xml
  • @Bean equivalent <bean />
  • @Bean 作用于方法上,产生一个bean 并交给 Spring 管理
  • 引用第三方库中的类,需要装配到 Spring 容器时,只能通过 @Bean 来实现
@Configuration
public class UserConfig {
    @Bean("user")
    @Scope("singleton|prototype")
    public User user() {
        return new User("tinyspot", 12);
    }
}

@Bean("user") @Bean作用于方法; 若不指定对象名,默认方法名是 id

ApplicationContext context = new AnnotationConfigApplicationContext(UserConfig.class);
User user = (User) context.getBean("queryUser");

3. @value

  • a-guide-to-value-in-spring-boot/
  • So far every example used a ${…} pattern in the @Value annotations. These are called SpEL(Spring Expression Language) expressions.

application.yml

# 自定义属性
author:
  name: Tinyspot
  age: 20
@RestController
public class NginxController {

    @Value("${author.name}")
    private String authorName;

    @Value("${author.age}")
    private Integer authorAge;

    @GetMapping("/author")
    public String getAuthor() {
        return authorName + ", " + authorAge;
    }
}

如果属性很多,每个都要配置 @Value

3.1 配置绑定 @ConfigurationProperties

@RestController
public class NginxController {
    @Resource
    private AuthorProperties authorProperties;

    @GetMapping("/author")
    public String getAuthor() {
        return JSON.toJSONString(authorProperties);
    }
}
@ConfigurationProperties(prefix = "author")
@Component
@Data
public class AuthorProperties {
    private String name;
    private Integer age;
}

3.2 绑定配置提示

配置类 AuthorProperties.java 会提示 Spring Boog Configuration Annotation Processor not found in classpath
可在 pom.xml 里加上(不加也没影响)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

<!-- 打包时排除 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

在 application.yml 增加属性信息时会有提示

相关文章

网友评论

      本文标题:Spring 注解@Configuration,@Bean,@v

      本文链接:https://www.haomeiwen.com/subject/wtrbxdtx.html