Spring Boot文档阅读笔记-Spring Boot @Bean解析

利用SpringBoot的@Bean创建一个简单的Bean。

Spring的@Bean注解是放在方法上的,带上这个注解的方法会被Spring容器管理。并且这个方法要返回一个值(对象),这个值和对象会被BeanFactory注册为Bean。

下面是一个例子

程序运行截图如下:

项目结构如下:

pom.xml
src
├───main
│   ├───java
│   │   └───cn
│   │       └───it1995
│   │               DemoApplication.java
│   │               AppName.java
│   └───resources
│           application.properties
│           logback.xml
└───test
    └───java

下面是Maven代码,spring-boot-starer是核心启动包,他包含自动配置、日志、YAML。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.it1995</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

下面定义一个简单的接口,用于返回应用名

package cn.it1995.demo;

interface AppName {

    String getName();
}

application.peroperties文件包含了应用配置的设置。spring.main.banner-mode配置了Spring内部的属性,将其关闭,将spring.main.log-startup-info也关掉。新增app.name这个自定义属性

spring.main.banner-mode=off
spring.main.log-startup-info=false
app.name=SpringBootBean

下面是logbak.xml文件,用于配置日志的。设置日志等级为ERROR。这个功能是在spring-boot-starter包中的。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="ERROR"/>
    <logger name="cn.it1995" level="INFO"/>
</configuration>

下面是启动类,包含bean的创建

package cn.it1995.demo;

import org.springframework.beans.factory.annotation.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        logger.info("Application name: {}", appName.getName());
    }

    @Autowired
    private AppName appName;

    @Bean
    public AppName getAppName(@Value("${app.name}") String appName) {

        
        return () -> appName;
    }
}

@SpringBootApplication开启了自动装配和组建扫描;

@Autowired注解获将AppName的Bean注入进去了;

@Bean用于创建一个Bean,让Spring容器进行管理;

@Value获取application.properties中app.name的值。

 

源码打包下载地址:

https://github.com/fengfanchen/Java/tree/master/SpringBootBean

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页