Spring Cloud微服务实战 打造企业级优惠券系统 3-5 SpringBoot 配置注入的方式

0    课程地址

https://coding.imooc.com/lesson/380.html#mid=28273 

1    浓缩精华
1.1  浓缩精华

4.2,4.3,3.1

2    个人关注
2.1  个人关注

下载安装postman,用来测试接口

3.1

3    课程内容
3.1  lombok使用
  • 安装lombok插件

https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html

a  安装lombok插件     file--》settings(Ctrl alt S)--》Plugins--》--》Browse repositories--》搜索lombok并安装

b  配置lombok注解处理器  file--》settings(Ctrl alt S)--》Build,Execution,Deployment--》Compiler--》选中Annotation Processors--》勾选Enable annotation processing

c  重启idea

  • 引入lombok依赖
<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
  • 使用参见4.2log.info
3.2  postman 使用

https://blog.csdn.net/liu__xi__/article/details/103786789

3.3  @RequestMapping,@GetMapping、@postMapping的区别

https://www.cnblogs.com/yeyuting/p/14120428.html

3.4  @Repository、@Controller、@Service、@Component区别

https://www.cnblogs.com/clwydjgs/p/9255083.html

       1、@Service用于标注业务层组件 
       2、@Controller用于标注控制层组件(如struts中的action) 
       3、@Repository用于标注数据访问组件,即DAO组件. 
       4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。   

4    代码演练
4.1  @Value配置注入
  • 配置文件
    spring:
     # profiles:
     #  active: prod
     #注意:这里不能有profiles active: dev,否则 按application-dev配置文件的端口号
     #   active: dev
      application:
        name: imooc_springboot_study
    
    server:
    #端口号
      port: 8081
      servlet:
        context-path: /imooc
    
    com:
      imooc:
        userid: 110
        username: aaa
  • UserController
    package com.imooc.springboot.application.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * UserController
     *
     * @author 魏豆豆
     * @date 2021/3/13
     */
    @RequestMapping("/userController")  //注解可以将HTTP请求映射给controller来处理,包括返回视图页面的controller和Rest服务的controller。
    @ResponseBody   //@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。
    @Controller//@Controller标识的类,该类代表控制器类(控制层/表现层)。这里控制层里面的每个方法,都可以去调用@Service标识的类(业务逻辑层),@Service标识的类中的方法可以继续调用@Resposity标识的接口实现类(Dao层/持久层)。
    public class UserController {
        @Value("${com.imooc.userid}")
        private String userid;
        @Value("${com.imooc.username}")
        private String username;
    
        /**
         *  访问地址:127.0.0.1:8081/imooc/userController/testInjectOne
         */
        @GetMapping("/testInjectOne")
        public void testConfigInjectOne(){
            System.out.println("userid="+this.userid);
            System.out.println("username="+this.username);
        }
    
    
    }
  • 重启服务,postman访问地址
  • 打印日志:
    2021-03-13 10:30:17.275  INFO 5476 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/imooc]  : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2021-03-13 10:30:17.275  INFO 5476 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2021-03-13 10:30:17.289  INFO 5476 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 14 ms
    2021-03-13 10:30:17.355  WARN 5476 --- [nio-8081-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
    userid=110
    username=aaa
4.2  @Value配置注入 引入lombok打印日志
  • 配置文件:同上
  • UserController
    package com.imooc.springboot.application.controller;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * UserController
     *
     * @author 魏豆豆
     * @date 2021/3/13
     */
    @RequestMapping("/userController")  //注解可以将HTTP请求映射给controller来处理,包括返回视图页面的controller和Rest服务的controller。
    @ResponseBody   //@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。
    @Controller//@Controller标识的类,该类代表控制器类(控制层/表现层)。这里控制层里面的每个方法,都可以去调用@Service标识的类(业务逻辑层),@Service标识的类中的方法可以继续调用@Resposity标识的接口实现类(Dao层/持久层)。
    @Slf4j//@lombok注解,该注解引入lombok日志
    
    
    //@Controller标识的类,该类代表控制器类(控制层/表现层)。这里控制层里面的每个方法,都可以去调用@Service标识的类(业务逻辑层),@Service标识的类中的方法可以继续调用@Resposity标识的接口实现类(Dao层/持久层)。
    public class UserController {
    @Value("${com.imooc.userid}")
    private String userid;
    @Value("${com.imooc.username}")
    private String username;

    /**
    * 访问地址:127.0.0.1:8081/imooc/userController/testInjectOne
    */
    @GetMapping("/testInjectOne")
    public void testConfigInjectOne(){
    /* System.out.println("userid="+this.userid);
    System.out.println("username="+this.username);*/
    log.info("userid="+this.userid);
    log.info("username="+this.
    username);

    }


    }
     
  • 重启服务postman访问地址
  • 打印日志
    2021-03-13 14:16:03.621  INFO 4672 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/imooc]  : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2021-03-13 14:16:03.621  INFO 4672 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2021-03-13 14:16:03.630  INFO 4672 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms
    2021-03-13 14:16:03.689  INFO 4672 --- [nio-8081-exec-1] c.i.s.a.controller.UserController        : userid=110
    2021-03-13 14:16:03.689  INFO 4672 --- [nio-8081-exec-1] c.i.s.a.controller.UserController        : username=aaa
4.3  @ConfigurationProperties配置注入
  • application.yml
    spring:
     # profiles:
     #  active: prod
     #注意:这里不能有profiles active: dev,否则 按application-dev配置文件的端口号
     #   active: dev
      application:
        name: imooc_springboot_study
    
    server:
    #端口号
      port: 8081
      servlet:
        context-path: /imooc
    
    com:
      imooc:
        userid: 110
        username: aaa
  • Component 组件类
package com.imooc.springboot.application.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * SpringBootConfig
 *
 * @author 魏豆豆
 * @date 2021/3/13
 */
@Component//把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>
@Data//lombok注解,进行属性的getSet操作
@ConfigurationProperties(prefix = "com.imooc")//Spring注解,配置前缀自动注入属性
public class SpringBootConfig {
    String userid;
    String username;

}
  • Controller类
package com.imooc.springboot.application.controller;

import com.imooc.springboot.application.config.SpringBootConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * UserController
 *
 * @author 魏豆豆
 * @date 2021/3/13
 */
@RequestMapping("/userController")  //注解可以将HTTP请求映射给controller来处理,包括返回视图页面的controller和Rest服务的controller。
@ResponseBody   //@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。
@Controller//@Controller标识的类,该类代表控制器类(控制层/表现层)。这里控制层里面的每个方法,都可以去调用@Service标识的类(业务逻辑层),@Service标识的类中的方法可以继续调用@Resposity标识的接口实现类(Dao层/持久层)。
@Slf4j
public class UserController {
    @Value("${com.imooc.userid}")
    private String userid;
    @Value("${com.imooc.username}")
    private String username;

    @Autowired//自动注入Compenent组件
    private SpringBootConfig springBootConfig;
    /**
     *  访问地址:127.0.0.1:8081/imooc/userController/testInjectOne
     */
    @GetMapping("/testInjectOne")
    public void testConfigInjectOne(){
/*        System.out.println("userid="+this.userid);
        System.out.println("username="+this.username);*/
        log.info("userid="+this.userid);
        log.info("username="+this.username);

    }
    /**
     *  访问地址:127.0.0.1:8081/imooc/userController/testInjectTwo
     */
    @GetMapping("/testInjectTwo")
    public void testConfigInjectTwo(){
        log.info("userid="+springBootConfig.getUserid());
        log.info("username="+springBootConfig.getUsername());

    }

}
  • postman访问
  • 打印日志
    2021-03-13 18:11:43.858  INFO 6368 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/imooc]  : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2021-03-13 18:11:43.858  INFO 6368 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2021-03-13 18:11:43.873  INFO 6368 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 14 ms
    2021-03-13 18:11:43.938  INFO 6368 --- [nio-8081-exec-1] c.i.s.a.controller.UserController        : userid=110
    2021-03-13 18:11:43.938  INFO 6368 --- [nio-8081-exec-1] c.i.s.a.controller.UserController        : username=aaa
诸葛
原文地址:https://www.cnblogs.com/1446358788-qq/p/14296097.html