SpringBoot.02.SpringBoot创建对象与属性注入

x33g5p2x  于2022-04-11 转载在 Spring  
字(10.7k)|赞(0)|评价(0)|浏览(311)

SpringBoot创建对象与属性注入

前言

所谓SpringBoot创建对象就是将对象交给Spring来管理。在SpringBoot中我们可以使用注解。比如我们常用的@Component@Controller@Service@Repository等。不过这种方式一次只能创建一个对象;此外我们还可以使用@Configuration + @Bean的方式一次性创建多个对象。

属性注入是指我们可以将在配置文件中配置的信息注入到java文件中来使用。这样的使用场景在实际开发中是普遍存在的。比如我们要集成高德定位需要用到搞的提供的secret,这个值不能直接写死在代码中而只能写在配置文件中。而实际使用是在java中,这就需要我们将该属性值从配置文件注入到当前的java文件中。有关属性注入分为基本属性注入对象注入

下面我们以springboot-02-initializr项目为例来演示在SpringBoot创建对象与属性注入。

创建对象

单个对象的创建

​ 在springboot中可以管理单个对象可以直接使用spring框架中注解形式创建。常用的注解如下:

  • @Component: 通用的对象创建注解
  • @Controller :用来创建控制器对象
  • @Service :用来创建业务层对象
  • @Repository:用来创建DAO层对象
    原理上以上四个注解可以互相替代,@Component注解修饰了下面3个注解。在Spring中为了区分MVC各层,不建议这几个注解混用
1.TestController.java
import com.christy.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Christy
 * @Date 2021/9/1 11:06
 **/
@RestController
@RequestMapping("test")
public class TestController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Value("${server.port}")
    private Integer port;

    /**
     * Spring官方不再建议使用该种方式进行注入,转而使用构造函数的方式
     */
    /*@Autowired
    private TestService testService;*/

    private TestService testService;
    @Autowired
    public TestController(TestService testService){
        this.testService = testService;
    }

    @RequestMapping("hello")
    public String sayHello(){
        log.info(testService.sayHello());
        return testService.sayHello() + "current port is " + port;
    }
}
2.TestService
/**
 * @Author Christy
 * @Date 2021/9/1 14:25
 **/
public interface TestService {
    String sayHello();
}

import com.christy.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @Author Christy
 * @Date 2021/9/1 14:26
 * @Service 该注解标识当前对象为一个业务处理对象,并将当前对象交由Spring管理,默认在Spring工厂的名字是类名首字母小写
 **/
@Service
public class TestServiceImpl  implements TestService {

    private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class);

    @Override
    public String sayHello() {
        log.info("Hello SpringBoot!");
        return "Hello SpringBoot!";
    }
}
3.测试

启动项目,在浏览器中访问http://localhost:8802/test/hello,结果如下图所示:

由结果我们可以看到TestService在Spring中成功创建,并且在TestController中成功注入了。

多个对象的创建

​ 如何在springboot中像spring框架一样通过xml创建多个对象?SpringBoot也提供了如**@Configuration + @Bean**注解进行创建

  • @Configuration :代表这是一个spring的配置类,相当于Spring.xml配置文件
  • @Bean :用来在工厂中创建这个@Bean注解标识的对象
    @Bean将标识方法的返回值交由Spring工厂管理,在Spring中创建该对象。一般情况下我们将该方法名命名为返回值首字母小写
1.BeansConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Calendar;

/**
 * @Author Christy
 * @Date 2021/9/1 14:57
 *
 * @Configuration 标注在类上,作用:配置Spring容器(应用上下文),被它修饰的类表示可以使用Spring IoC容器作为bean定义的来源。
 *              相当于把该类作为Spring的xml配置文件中的<beans>元素(并且包含命名空间)
 *              简单的理解,被该注解标识的类就相当于SSM框架中的Spring.xml
 * @Bean 标注在方法上,作用:注册bean对象,被标记的方法的返回值会作为bean被加入到Spring IoC容器之中,bean的名称默认为方法名。
 *              相当于把该方法的返回值作为 xml 配置文件中<beans>的子标签<bean>
 **/
@Configuration
public class BeansConfig {

    @Bean
    public Calendar calendar(){
        return Calendar.getInstance();
    }
}
2.TestController.java
import com.christy.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;

/**
 * @Author Christy
 * @Date 2021/9/1 11:06
 **/
@RestController
@RequestMapping("test")
public class TestController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Value("${server.port}")
    private Integer port;

    /**
     * Spring官方不再建议使用该种方式进行注入,转而使用构造函数的方式
     */
    /*@Autowired
    private TestService testService;*/

    private TestService testService;
    private Calendar calendar;
    @Autowired
    public TestController(TestService testService, Calendar calendar){
        this.testService = testService;
        this.calendar = calendar;
    }

    @RequestMapping("hello")
    public String sayHello(){
        log.info(testService.sayHello());
        return testService.sayHello() + "current time is " + calendar.getTime();
    }
}
3.测试

属性注入

基本属性注入

基本属性注入又称单个属性注入,使用注解@Value可以注入八大基本类型String日期ListArrayMap。下面我们来举例说明

1.application-dev.yml
# 开发环境端口号是8802
server:
  port: 8802

# 基本类型
username: christy
age: 18
salary: 1800
gender: true
birthday: 2003/10/01 #日期类型必须是yyyy/MM/dd这种斜线类型的

# array、list与map
hobbya: money,belle,right
hobbyl: 抽烟,喝酒,烫头
hobbym: "{'username':'christy','realname':'tide'}"
2.TestController.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Christy
 * @Date 2021/9/1 11:06
 **/
@RestController
@RequestMapping("test")
public class TestController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    // 注入基本数据类型、String、Date
    @Value("${username}")
    private String username;
    @Value("${age}")
    private Integer age;
    @Value("${gender}")
    private Boolean gender;
    @Value("${salary}")
    private Double salary;
    @Value("${birthday}")
    private Date birthday;

    // 注入数组
    @Value("${hobbya}")
    private String[] hobbya;

    // 注入list
    @Value("${hobbyl}")
    private List<String> hobbyl;
    // 注入map
    @Value("#{${hobbym}}")
    private Map<String,String> hobbym;

    /**
     * Spring官方不再建议使用该种方式进行注入,转而使用构造函数的方式
     */
    /*@Autowired
    private TestService testService;*/

    private TestService testService;
    private Calendar calendar;
    @Autowired
    public TestController(TestService testService, Calendar calendar){
        this.testService = testService;
        this.calendar = calendar;
    }

    @RequestMapping("hello")
    public String sayHello(){
        log.info(testService.sayHello());

        System.out.println("姓名:" + username + ",年龄:" + age + ",性别:" + gender + ",生日:" + birthday + ",薪资:" + salary);

        System.out.println("生平三大爱好:");
        for (String hobby : hobbya) {
            System.out.print(hobby + "、");
        }

        System.out.println("生平三大爱好:");
        hobbyl.forEach(hobby-> System.out.println(hobby + "、"));

        System.out.println("map遍历");
        hobbym.forEach((key,value)-> System.out.println("key = " + key+" value = "+value));

        return testService.sayHello() + "current time is " + calendar.getTime();
    }
}
3.测试

启动项目,在浏览器中访问http://localhost:8802/test/hello,观察控制台。如下图所示:

对象注入

1.application-dev.yml
# 开发环境端口号是8802
server:
  port: 8802

# 基本类型
username: christy
age: 18
salary: 1800
gender: true
birthday: 2003/10/01 #日期类型必须是yyyy/MM/dd这种斜线类型的

# array、list与map
hobbya: money,belle,right
hobbyl: 抽烟,喝酒,烫头
hobbym: "{'username':'christy','realname':'tide'}"

# 注入对象
user:
  name: christy
  age: 18
  bir: 2003/10/01
2.User.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author Christy
 * @Date 2021/9/1 16:21
 * @ConfigurationProperties 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 *                          prefix = "xxx":配置文件中哪个下面的所有属性进行一一映射
 *                          使用该注解记得要写getter与setter方法
 **/
@Component
@ConfigurationProperties(prefix = "user")
public class User {
    private String username;
    private Integer age;
    private Date birthday;

    public User() {
    }

    public User(String username, Integer age, Date birthday) {
        this.username = username;
        this.age = age;
        this.birthday = birthday;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}
3.TestController.java
import com.christy.entity.User;
import com.christy.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Christy
 * @Date 2021/9/1 11:06
 **/
@RestController
@RequestMapping("test")
public class TestController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    // 注入基本数据类型、String、Date
    @Value("${username}")
    private String username;
    @Value("${age}")
    private Integer age;
    @Value("${gender}")
    private Boolean gender;
    @Value("${salary}")
    private Double salary;
    @Value("${birthday}")
    private Date birthday;

    // 注入数组
    @Value("${hobbya}")
    private String[] hobbya;

    // 注入list
    @Value("${hobbyl}")
    private List<String> hobbyl;
    // 注入map
    @Value("#{${hobbym}}")
    private Map<String,String> hobbym;

    /**
     * Spring官方不再建议使用该种方式进行注入,转而使用构造函数的方式
     */
    /*@Autowired
    private TestService testService;*/

    private TestService testService;
    private Calendar calendar;
    private User user;
    @Autowired
    public TestController(TestService testService, Calendar calendar, User user){
        this.testService = testService;
        this.calendar = calendar;
        this.user = user;
    }

    @RequestMapping("hello")
    public String sayHello(){
        log.info(testService.sayHello());

        System.out.println("姓名:" + username + ",年龄:" + age + ",性别:" + gender + ",生日:" + birthday + ",薪资:" + salary);

        System.out.println("生平三大爱好:");
        for (String hobby : hobbya) {
            System.out.print(hobby + "、");
            System.out.println();
        }

        System.out.println("生平三大爱好:");
        hobbyl.forEach(hobby-> System.out.println(hobby + "、"));

        System.out.println("map遍历");
        hobbym.forEach((key,value)-> System.out.println("key = " + key+" value = "+value));

        System.out.println(user.toString());

        return testService.sayHello() + "current time is " + calendar.getTime();
    }
}
4.测试

启动项目,在浏览器中访问http://localhost:8802/test/hello,观察控制台。如下图所示:

5.警告

在使用注解@ConfigurationProperties的时候在User.java文件的上方有一个警告Spring Boot Configuration Annotation Processor not Configured。当然了他不影响我们程序的执行,但它是什么意思呢?

问题分析

它的意思是"Spring Boot的配置注解执行器没有配置",配置注解执行器的好处是什么?

配置注解执行器配置完成后,当执行类中已经定义了对象和该对象的字段后,在配置文件中对该类赋值时,便会非常方便的弹出提示信息。

解决方案

我们在pom.xml文件中加入以下依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <!-- 为true时表示该依赖不会传递 -->
  <optional>true</optional>
</dependency>

当加入该依赖后点击重新加载Maven依赖,原先的警告会消失,进而提示我们Re-run Spring Boot Configuration Annotation Processor to update generated metedata。如下图所示:

我们重启项目,然后回到配置文件,在配置文件中键入user,可以发现会自动提示User.java实体类中定义的属性

相关文章