SpringBoot自动装配初步认识(5)

@Bean
@ConditionalOnBean({Teacher.class})
public Student student(StudentProperties studentProperties) {
    Student student = new Student();
    student.setStuName(studentProperties.getName());
    return student;
}

@Bean
@ConditionalOnMissingBean
public static Teacher teacher() {
    return new Teacher();
}

@Bean
@ConditionalOnMissingBean
public static School school() {
    return new School();
}
比如上述代码 Student是不会被创建的,如果非要@Bean和@Conditional使用,则可以借助于@Import方式实现
@Configuration
@EnableConfigurationProperties(StudentProperties.class)
@Import(TeacherAutoConfiguration.class)
public class MyTestAutoConfiguration {

@Bean
    @ConditionalOnBean(Teacher.class)
    public Student student(StudentProperties studentProperties) {
        Student student = new Student();
        student.setStuName(studentProperties.getName());
        return student;
    }

}

@Configuration
public class TeacherAutoConfiguration {

@Bean
    @ConditionalOnMissingBean
    public static Teacher teacher() {
        return new Teacher();
    }
}

二。实现简单的SpringBoot示例

1.我们先创建两个类分别为 Teacher Student,项目结构图

SpringBoot自动装配初步认识

注意图中标圈的文件:spring-configuration-metadata.json文件,我们配置这个文件后,可以在idea或者spring sts中配置application.yml得到相关智能提示

json数据如下

{
  "hints":[],
  "groups":[],
  "properties": [
    {
      "sourceType": "com.bdqn.lyrk.springboot.study.configuration.MyProperties",
      "name": "my.loginName",
      "description": "登录名",
      "type": "java.lang.String"
    }
  ]
}

2.MyObjectAutoConfiguration类代码:

package com.bdqn.lyrk.springboot.study.configuration;

import com.bdqn.lyrk.springboot.study.pojo.School;
import com.bdqn.lyrk.springboot.study.pojo.Student;
import com.bdqn.lyrk.springboot.study.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动创建对象示例,例子中我们创建Teacher与Student对象。
 * 当项目打成jar包依赖到其他Spring容器中,这些对象我们可以自动进行注入
 */
@Configuration
@EnableConfigurationProperties(MyProperties.class)
public class MyObjectAutoConfiguration {

@Configuration
    static class TeacherAutoConfiguration {

@Bean
        @ConditionalOnClass({Teacher.class, School.class})
        public static Teacher teacher() {
            return new Teacher();
        }
    }

@Configuration
    static class StudentAutoConfiguration {

@Bean
        @ConditionalOnMissingBean
        public Student student(@Autowired MyProperties myProperties) {
            return new Student(myProperties.getLoginName());
        }
    }
}

3.MyProperties代码:

package com.bdqn.lyrk.springboot.study.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 用于实现读取application.yml中的配置
 */
@ConfigurationProperties(prefix = MyProperties.PREFIX)
public class MyProperties {

public static final String PREFIX = "my";

private String loginName;

public String getLoginName() {
        return loginName;
    }

public void setLoginName(String loginName) {
        this.loginName = loginName;
    }
}

4.application.yml配置:

my:
  loginName: test
spring:
  main:
    web-environment: false

5.Application代码:

package com.bdqn.lyrk.springboot.study;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/57f305e4bd576cefaf9c845f2e667cdb.html