Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaspringboot。Cron问题。变量未实例化_Java_Spring_Cron_Scheduled Tasks - Fatal编程技术网

javaspringboot。Cron问题。变量未实例化

javaspringboot。Cron问题。变量未实例化,java,spring,cron,scheduled-tasks,Java,Spring,Cron,Scheduled Tasks,我的Spring Boot应用程序无法正常工作。主要的想法是用户选择他想知道的城市的温度。温度值以城市名称作为文件名保存到txt文件中。然后我想安排Cron,这样,每小时相同城市的新温度值都会保存到相同的文件中。目前,除了CronManager类之外,其他一切都很正常。我不知道如何把城市的名字传给它。以下是我的代码部分: 应用程序加载程序 package com.boris2barak.samplemvc.app; import org.springframework.boot.SpringAp

我的Spring Boot应用程序无法正常工作。主要的想法是用户选择他想知道的城市的温度。温度值以城市名称作为文件名保存到txt文件中。然后我想安排Cron,这样,每小时相同城市的新温度值都会保存到相同的文件中。目前,除了CronManager类之外,其他一切都很正常。我不知道如何把城市的名字传给它。以下是我的代码部分:

应用程序加载程序

package com.boris2barak.samplemvc.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication

public class ApplicationLoader extends SpringBootServletInitializer {


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ApplicationLoader.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(ApplicationLoader.class, args);
}
}
package com.boris2barak.samplemvc.app;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;

@Controller
public class myFirstController {

@RequestMapping("/hello")
public String hello(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    model.addAttribute("name", name);
    return "hello";
}

@RequestMapping("/temperature")
public String whatIsTheTemperature(Model model, @RequestParam(value = "city", required = false, defaultValue = "World") String city) throws IOException {
    model.addAttribute("city", city);
    WeatherApp data = new WeatherApp();
    return data.getTemperatureForCity(city);
}
}
package com.boris2barak.samplemvc.app;
import org.apache.commons.io.IOUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.apache.commons.io.IOUtils.toInputStream;

public class FileManager {

public void saveTheFile(String theTemperature, String cityName) throws IOException {
    InputStream streamIn = toInputStream(theTemperature, "UTF-8");
    OutputStream streamOut = new FileOutputStream(cityName + ".txt", true);
    try {
        IOUtils.copy(streamIn, streamOut);
    } finally {
        IOUtils.closeQuietly(streamIn);
        IOUtils.closeQuietly(streamOut);
    }

}
}
package com.boris2barak.samplemvc.app;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component
public class CronManager {

@Scheduled(cron = "0 * * ? * *")
public void cronTask() throws IOException {

WeatherApp data = new WeatherApp();
data.getTemperatureForCity(city <<<< how to instantiate this??);


}
}
控制器

package com.boris2barak.samplemvc.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication

public class ApplicationLoader extends SpringBootServletInitializer {


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ApplicationLoader.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(ApplicationLoader.class, args);
}
}
package com.boris2barak.samplemvc.app;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;

@Controller
public class myFirstController {

@RequestMapping("/hello")
public String hello(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    model.addAttribute("name", name);
    return "hello";
}

@RequestMapping("/temperature")
public String whatIsTheTemperature(Model model, @RequestParam(value = "city", required = false, defaultValue = "World") String city) throws IOException {
    model.addAttribute("city", city);
    WeatherApp data = new WeatherApp();
    return data.getTemperatureForCity(city);
}
}
package com.boris2barak.samplemvc.app;
import org.apache.commons.io.IOUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.apache.commons.io.IOUtils.toInputStream;

public class FileManager {

public void saveTheFile(String theTemperature, String cityName) throws IOException {
    InputStream streamIn = toInputStream(theTemperature, "UTF-8");
    OutputStream streamOut = new FileOutputStream(cityName + ".txt", true);
    try {
        IOUtils.copy(streamIn, streamOut);
    } finally {
        IOUtils.closeQuietly(streamIn);
        IOUtils.closeQuietly(streamOut);
    }

}
}
package com.boris2barak.samplemvc.app;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component
public class CronManager {

@Scheduled(cron = "0 * * ? * *")
public void cronTask() throws IOException {

WeatherApp data = new WeatherApp();
data.getTemperatureForCity(city <<<< how to instantiate this??);


}
}
WeatherApp

package com.boris2barak.samplemvc.app;
import com.google.gson.Gson;
import org.springframework.web.client.RestTemplate;
import java.io.*;

public class WeatherApp {

public String getTemperatureForCity(String city) throws IOException {
    String URL = "http://api.openweathermap.org/data/2.5/weather?q="+city+"&APPID=9fff4e627587b84fca1ed835321da768";
    RestTemplate restTemplate = new RestTemplate();
    String json = restTemplate.getForObject(URL, String.class);
    WeatherData weatherData = new Gson().fromJson(json, WeatherData.class);

    ////......... (here i have the code to get the other data like coordinates of the city, humidity, etc which is not relevant to my problem)

    String theTemperature = weatherData.getTemperature();
    FileManager myFile = new FileManager();
    myFile.saveTheFile(theTemperature, city);
    return theTemperature;
}
}
文件管理器

package com.boris2barak.samplemvc.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication

public class ApplicationLoader extends SpringBootServletInitializer {


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ApplicationLoader.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(ApplicationLoader.class, args);
}
}
package com.boris2barak.samplemvc.app;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;

@Controller
public class myFirstController {

@RequestMapping("/hello")
public String hello(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    model.addAttribute("name", name);
    return "hello";
}

@RequestMapping("/temperature")
public String whatIsTheTemperature(Model model, @RequestParam(value = "city", required = false, defaultValue = "World") String city) throws IOException {
    model.addAttribute("city", city);
    WeatherApp data = new WeatherApp();
    return data.getTemperatureForCity(city);
}
}
package com.boris2barak.samplemvc.app;
import org.apache.commons.io.IOUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.apache.commons.io.IOUtils.toInputStream;

public class FileManager {

public void saveTheFile(String theTemperature, String cityName) throws IOException {
    InputStream streamIn = toInputStream(theTemperature, "UTF-8");
    OutputStream streamOut = new FileOutputStream(cityName + ".txt", true);
    try {
        IOUtils.copy(streamIn, streamOut);
    } finally {
        IOUtils.closeQuietly(streamIn);
        IOUtils.closeQuietly(streamOut);
    }

}
}
package com.boris2barak.samplemvc.app;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component
public class CronManager {

@Scheduled(cron = "0 * * ? * *")
public void cronTask() throws IOException {

WeatherApp data = new WeatherApp();
data.getTemperatureForCity(city <<<< how to instantiate this??);


}
}
CronManager

package com.boris2barak.samplemvc.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication

public class ApplicationLoader extends SpringBootServletInitializer {


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ApplicationLoader.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(ApplicationLoader.class, args);
}
}
package com.boris2barak.samplemvc.app;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;

@Controller
public class myFirstController {

@RequestMapping("/hello")
public String hello(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    model.addAttribute("name", name);
    return "hello";
}

@RequestMapping("/temperature")
public String whatIsTheTemperature(Model model, @RequestParam(value = "city", required = false, defaultValue = "World") String city) throws IOException {
    model.addAttribute("city", city);
    WeatherApp data = new WeatherApp();
    return data.getTemperatureForCity(city);
}
}
package com.boris2barak.samplemvc.app;
import org.apache.commons.io.IOUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.apache.commons.io.IOUtils.toInputStream;

public class FileManager {

public void saveTheFile(String theTemperature, String cityName) throws IOException {
    InputStream streamIn = toInputStream(theTemperature, "UTF-8");
    OutputStream streamOut = new FileOutputStream(cityName + ".txt", true);
    try {
        IOUtils.copy(streamIn, streamOut);
    } finally {
        IOUtils.closeQuietly(streamIn);
        IOUtils.closeQuietly(streamOut);
    }

}
}
package com.boris2barak.samplemvc.app;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component
public class CronManager {

@Scheduled(cron = "0 * * ? * *")
public void cronTask() throws IOException {

WeatherApp data = new WeatherApp();
data.getTemperatureForCity(city <<<< how to instantiate this??);


}
}
package com.boris2barak.samplemvc.app;
导入org.springframework.scheduling.annotation.Scheduled;
导入org.springframework.stereotype.Component;
导入java.io.IOException;
@组成部分
公共类CronManager{
@已计划(cron=“0**?**”)
public void cronTask()引发IOException{
WeatherApp数据=新的WeatherApp();

data.getTemperatureForCity(城市你有城市列表吗?不,我没有,因为它可能是用户想要的任何城市。目前我使用ARC模拟用户的请求。例如:。谢谢!你的想法很好!我已经像你一样将所有内容都放到了CronManager类中。现在城市被添加到HashEet中,cronTask被执行,但它认为HashSet是空的。因此,最后我不要为一个城市获取很少的温度值。我是这样做的,我看到它从未进入循环:我通过控制器类添加:正如您所看到的,我在CronTask内的循环之前添加了if语句。因此,我始终执行else条件。您可以删除“data=null;”因为它不会影响情况。是的,它现在起作用了!非常感谢!对不起,也许我昨天晚上不够专心:-)因为我认为在调用方法时使用Autowired和创建新的CronManager之间没有区别。对不起,我自己学习Java编程才两个月,这就是为什么我的问题看起来很愚蠢的原因。据我所知,Autowired注释让我们有可能创建一个新的CronManager?所以,现在CronManager是在应用程序启动时创建的,只是等待城市被实例化…类似的,对吗?在您的案例中,CronManager是单例的,您使用新的CronManager()时,通过自动连线将此实例注入控制器-您创建了未连接到spring上下文的实例,每次调用都会创建新的单独实例,-在这种情况下,因为它是SpringBean,所以您不会为此应用@Scheduled。