springboot中非Controller调用service报空指针异常

package com.justsy.utils;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.justsy.entity.Patrol;
import com.justsy.service.Potrol_Service;

@Component //将工具类声明为spring组件
public class TestUtils {
	@Autowired
	private Potrol_Service pService;
	//静态初使化当前类
	public static TestUtils testUtils;
	@PostConstruct
	public void init() {
		testUtils = this;
		testUtils.pService = this.pService;
	}
	//utils工具类中使用service或mapper接口的方法例子
	public static void test(Patrol patral) {
		testUtils.pService.addPatrol(patral);
	}
}

非controller调用

private void saveData() {
        //LOGGER.info("{}条数据,开始存储数据库!", list.size());
        for (Patrol patral : list) {
			TestUtils.test(patral);
		}
        LOGGER.info("{}条数据存储成功!",list.size());
    }

成功调用:

2019-10-15 16:58:47.090 DEBUG 92676 --- [io-8080-exec-14] c.j.mapper.Patrol_Mapper.insertPatrol    : ==>  Preparing: INSERT INTO justsafe_patrol ( project_id, net_info_id, patrol_time, cpu_usage_rate, ram_usage_rate, disk_Usage_Rate, disk_Home_Usage_Rate, nginx_http, nginx_https, nginx_Download, push ) values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) 
2019-10-15 16:58:47.093 DEBUG 92676 --- [io-8080-exec-14] c.j.mapper.Patrol_Mapper.insertPatrol    : ==> Parameters: 1(Integer), 1000002(Integer), 2019-03-06 00:00:00.0(Timestamp), 11.0(Double), 22.0(Double), 33.0(Double), 44.0(Double), 55(Integer), 66(Integer), 77(Integer), 88(Integer)
2019-10-15 16:58:47.093 DEBUG 92676 --- [io-8080-exec-14] c.j.mapper.Patrol_Mapper.insertPatrol    : <==    Updates: 1
2019-10-15 16:58:47.093  INFO 92676 --- [io-8080-exec-14] c.j.utils.easyexcel.DemoDataListener     : 1条数据存储成功!
2019-10-15 16:58:47.094  INFO 92676 --- [io-8080-exec-14] c.j.utils.easyexcel.DemoDataListener     : 数据解析与存储操作已完成

此方法必须要启动项目,方法测试不行,不然还是不能注入,依然会报臭名昭著的空指针异常.如不能解决,评论区留言,看到后第一时间回复!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在其他服务中非controller调用API网关中的服务,可以使用RestTemplate或Feign等工具来发起HTTP请求。 如果使用RestTemplate,可以在其他服务中创建一个RestTemplate实例,并使用该实例来发起HTTP请求。需要注意的是,由于API网关中可能会进行一些过滤、路由等操作,因此需要使用API网关中的服务地址来发起请求,而不是直接使用后端服务的地址。 下面是一个使用RestTemplate调用API网关服务的示例代码: ```java RestTemplate restTemplate = new RestTemplate(); String apiUrl = "http://api-gateway-service:8080/api/service"; ResponseEntity<String> response = restTemplate.getForEntity(apiUrl, String.class); String responseBody = response.getBody(); ``` 在上述代码中,我们创建了一个名为restTemplate的RestTemplate实例,并使用该实例来发起GET请求。我们使用了API网关中的服务地址"http://api-gateway-service:8080/api/service"来发起请求,而不是直接使用后端服务的地址。 如果使用Feign,可以在其他服务中定义一个Feign接口,并使用该接口来发起HTTP请求。需要注意的是,需要使用@FeignClient注解来指定API网关中的服务名,并使用@RequestMapping注解来指定API网关中服务的请求路径。 下面是一个使用Feign调用API网关服务的示例代码: ```java @FeignClient(name = "api-gateway-service") public interface ApiService { @RequestMapping(method = RequestMethod.GET, value = "/api/service") String getService(); } ApiService apiService = Feign.builder() .encoder(new JacksonEncoder()) .decoder(new StringDecoder()) .target(ApiService.class, "http://api-gateway-service:8080"); String responseBody = apiService.getService(); ``` 在上述代码中,我们定义了一个名为ApiService的Feign接口,并使用@FeignClient注解指定了API网关中的服务名。我们使用@RequestMapping注解来指定API网关中服务的请求路径。最后,我们使用Feign.builder()方法创建一个Feign客户端实例,并使用该实例来发起请求。我们同样使用了API网关中的服务地址"http://api-gateway-service:8080"来发起请求,而不是直接使用后端服务的地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值