SpringBoot学习笔记44——SpringBoot的使用小技巧

今天来记录一些一定会用到但总忘记怎么写的代码

获取request

public static HttpServletRequest getHttpServletRequest() {
        HttpServletRequest request = null;
        try {
            request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        } catch (Exception ignored) {}
        return request;
    }

获取某个Service

这里说的是不通过@Autowired进行注入

1.创建一个上下文
package com.youyou.sso.test;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware {
    
    private static ApplicationContext applicationContext;
    
    public static Object getBean(String name) {
        
        return SpringContextHolder.applicationContext.getBean(name);
    }
    
    public static <T> T getBean(Class<T> clazz) {
        
        return SpringContextHolder.applicationContext.getBean(clazz);
    }
    
    public static String getValue(String key) {
        
        return SpringContextHolder.applicationContext.getEnvironment().getProperty(key);
    }
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        SpringContextHolder.applicationContext = applicationContext;
    }
    
}

2.测试代码,测试成功

 /**
     * 登录返回token
     * @param userName
     * @param password
     * @return
     */
    @ApiOperation(value = "|登录返回token|")
    @GetMapping("/loginByUserInfo")
    public String login(String userName , String password){

        ISsoService bean = SpringContextHolder.getBean(ISsoService.class);
        return bean.login(userName , password);
    }

获取配置文件中的值

配置文件

test.user.username = 123
test.user.pass=456
test.number.list[0] = 1
test.number.list[1] = 2
test.number.list[2] = 3
test.number.list[3] = 4

配置文件映射类

package com.youyou.sso.test;

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

import java.util.ArrayList;
import java.util.List;

@Data
@Component("testProperty")
@ConfigurationProperties(prefix = "test")
public class TestProperty {
    
    private ListProperty number = new ListProperty();
    
    private UserProperty user = new UserProperty();

    
    @Data
    public static class ListProperty {
        
        private List<String> list = new ArrayList<>();
        
    }


    @Data
    public static class UserProperty {

        private String username;

        private String pass;
    }
    
}
    /**
     * 登录返回token
     * @param userName
     * @param password
     * @return
     */
    @ApiOperation(value = "|登录返回token|")
    @GetMapping("/loginByUserInfo")
    public String login(String userName , String password){
        return testProperty.getNumber().toString();

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值