【钉钉开发】关于@value和@autowired注解的使用和配置

resources文件夹下的application.properties

dingtalk.app_key=XXXXX
dingtalk.app_secret=XXXXX
dingtalk.agent_id=XXXXX
dingtalk.corp_id=XXXXX

com.dingtalk.bpm.config包下的AppConfig类

package com.dingtalk.bpm.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
//@Component
public class AppConfig {
    @Value("${dingtalk.app_key}")
    private String appKey;

    @Value("${dingtalk.app_secret}")
    private String appSecret;

    @Value("${dingtalk.agent_id}")
    private String agentId;

    @Value("${dingtalk.corp_id}")
    private String corpId;

    //省略他们的get、set函数
}

com.dingtalk.bpm.service包下的AccessTokenService类

package com.dingtalk.bpm.service;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.bpm.config.AppConfig;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static com.dingtalk.bpm.config.UrlConstant.URL_GET_TOKEN;
//即https://oapi.dingtalk.com/gettoken

public class AccessTokenService {
    private static final Logger log = LoggerFactory.getLogger(AccessTokenService.class);
    @Autowired
    private AppConfig appConfig;

    public String getAccessToken() {
        DefaultDingTalkClient client = new DefaultDingTalkClient(URL_GET_TOKEN);
        OapiGettokenRequest request = new OapiGettokenRequest();
//        System.out.println("appkey:" + appConfig.getAppKey());

        request.setAppkey(appConfig.getAppKey());
        request.setAppsecret(appConfig.getAppSecret());
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
//            System.out.println(response.getBody());
        } catch (ApiException e) {
            log.error("getAccessToken failed", e);
        }

        return response.getAccessToken();
    }

【遇到的问题】@value能获取到application.properties中的数据,但无法给各个成员变量赋值,只获取到null

错误1 不能正确获取使用了@AutoWired的对象

刚开始写main方法测试时,直接new了一个accessTokenService对象,查阅资料后得知@autowired不能这样做,但他喵的没人写应该怎么做!!!正确方法如下,即使用ApplicationContext和ClassPathXmlApplicationContext获取

public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        AccessTokenService accessTokenService = (AccessTokenService) applicationContext.getBean("accessTokenService");
        String accessToken = accessTokenService.getAccessToken();
        System.out.println("accessToken: " + accessToken);
}

注意引入的包:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

不要引入错误的包名,idea开始自动导入了其他的包导致第一句飘红

错误2 spring.xml配置文件不知道怎么写

使用了Spring注解当然要配置了,但在百度“@value获取失败”、“@AutoWired使用”等问题时,资料中并没有提到配置xml的问题,以下是spring.xml的配置内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder ignore-unresolvable="true" location="classpath:application.properties" />
<!--        <context:annotation-config/>-->
        <context:component-scan base-package="com.dingtalk.bpm.config"/>
        <context:component-scan base-package="com.dingtalk.bpm.service"/>
        <bean id="accessTokenService" class="com.dingtalk.bpm.service.AccessTokenService"/>

</beans>

其中与@value获取数据相关的配置信息为:

<context:property-placeholder ignore-unresolvable="true" location="classpath:application.properties" />

与@autowired有关的配置信息为:

<bean id="accessTokenService" class="com.dingtalk.bpm.service.AccessTokenService"/>

这样就可以成功获取到accessToken啦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章