jerome66 2018-05-23 06:49 采纳率: 46.7%
浏览 6555
已采纳

Springboot 工具类中注入Value失败

在工具类中,通过@Value的形式,获取配置失败,但是同样的写法在Controller中却成功了,而且在这个工具类的单元测试文件中也能获取到配置。
尝试在这个工具类上加入@Controller注解,@Service注解,@Component注解均无法获取。

补充说明:版本为springboot1.5.12,配置文件为yml文件

问题找到了,在调用AuthUtil的类中,没有使用Spring容器管理,而是用了new创建的,所以导致出现这个问题。





import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;


@Component
public class AuthUtil {

    private String timestamp;

    private String httpBody;

    @Value("${bindInfo.bind.secret}")
    private String secret;

    @Value("normal")
    private String normal; // 注入普通字符串

    /*
  * 计算1970-01-01到当期时间的时间戳
  */
    public String getTimestamp() {
        long startPaintLogoTime=System.currentTimeMillis();
        return String.valueOf(startPaintLogoTime / 1000);
    }

    public String getHttpMD5(String timestamp, String httpBody){
        this.timestamp = timestamp;
        this.httpBody = httpBody;
        System.out.println(">>>>>>>>>>>>>>>>>>"+timestamp + secret +httpBody);
        System.out.println(normal);
        return  DigestUtils.md5Hex( timestamp + secret +httpBody ).toUpperCase();
    }

    public static void main(String[] args) {
        AuthUtil authUtil = new AuthUtil();
        authUtil.getHttpMD5(authUtil.getTimestamp(),"ksksksks");
    }
}

 @RunWith(SpringRunner.class)
@SpringBootTest
public class AuthUtilTest {

    @Value("${bindInfo.bind.secret}")
    private String secret;

    @Test
    public void getTimestamp() {
        System.out.println(">>>>>>>>>>>>>>>>.."+secret);
    }


}
  • 写回答

3条回答 默认 最新

  • SuperLGX 2018-05-23 07:22
    关注

    不能再main方法中运行,你既然使用spring的东西就要,将对象的管理交予spring容器
    需要加载spring容器通过容器管理对象。

    pom.xml

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    

    在项目中src下创建test/目录用于存放test累
    ApplicationTest.java

     import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class UserRoleMapperTest {
    
    @Autoware
    private AuthUtil authUtil;
        @Test
        public void test1() throws IOException, WorkbookDataException {
            authUtil.getHttpMD5(authUtil.getTimestamp(),"ksksksks");
            }
        }
    }
    

    这不是最佳的答案,还有一种方法我见过但是不会,但是首先还是要在容器中运行的,只在main方法中是无法加载容器相关的东西的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档