我怎样才能在下面的课上使用弹簧?

时间:2018-04-06 01:23:48

标签: java spring

我正在学习如何使用冲刺,说实话,我认为当你知道要注射什么时它会很有用。我在下面的课上陷入两难境地:

package Edamam;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class EdamamApi {
   private String ApplicationIDRecipes;
   private String ApplicationKeyRecipes;
   private String ApplicationIDIngredients;
   private String ApplicationKeyIngredients;

   public EdamamApi(){
       Properties prop = new Properties();
       InputStream input = null;
       try{
           input = new FileInputStream("src/main/java/Edamam/Edamam.properties");
           prop.load(input);
           this.ApplicationIDRecipes = prop.getProperty("Application_ID_Recipes");
           this.ApplicationKeyRecipes = prop.getProperty("Application_Keys_Recipes");
           this.ApplicationIDIngredients = prop.getProperty("Application_ID_Ingredients");
           this.ApplicationKeyIngredients = prop.getProperty("Application_Keys_Ingredients");
       }
       catch(IOException ex){
           ex.printStackTrace();
       }finally{
           if(input != null){
               try{
                   input.close();
               }
               catch(IOException e){
                   e.printStackTrace();
               }
           }
       }
   }

   private String makeUrlForRecipes(ArrayList<String> ingredients){
       boolean isFirst = true;
       String url = "https://api.edamam.com/search?q=";
       for(String ingredient : ingredients){
           if(!isFirst)
               url = url + "%20";
           isFirst = false;
           url = url + ingredient;
       }
       url = url + "&app_id=" + this.ApplicationIDRecipes + "&app_key=" + this.ApplicationKeyRecipes;
       return url;
   }

   private String makeUrlForIngredients(String ingredient){
       String url = "https://api.edamam.com/api/nutrition-data?app_id="+this.ApplicationIDIngredients+
                    "&app_key="+this.ApplicationKeyIngredients+"&ingr=1%20large%20"+ingredient;
       return url;
   }

   private ArrayList<String> toArrayList(JSONArray arr){
       ArrayList<String> StringList = new ArrayList<String>();
       for(int i = 0; i < arr.length(); i++)
           StringList.add(arr.getString(i));
       return StringList;
   }

   public ArrayList<RecipePojo> getRecipes(ArrayList<String> ingredients){
       String url = makeUrlForRecipes(ingredients);
       ArrayList<RecipePojo> recipes = new ArrayList<RecipePojo>();
       try {
        JSONObject response = JsonReader.readJsonFromUrl(url);
        JSONArray jsonArray = response.getJSONArray("hits");
        int NumberOfRecipes = 20;
        int jsonIndex = 0;
        while(jsonIndex < jsonArray.length() && NumberOfRecipes > 0){
            JSONObject objectInArray = jsonArray.getJSONObject(jsonIndex);
            String recipe = objectInArray.getJSONObject("recipe").getString("label");
            String ImgURL = objectInArray.getJSONObject("recipe").getString("image");
            ArrayList<String> IngredientLines = toArrayList(objectInArray.getJSONObject("recipe").
                                                 getJSONArray("ingredientLines"));
            RecipePojo newRecipe = new RecipePojo(recipe, ImgURL, IngredientLines);
            recipes.add(newRecipe);
            jsonIndex++;
            NumberOfRecipes--;
        }
       } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      return recipes;
   }

   public NutritionFactsIngredient getNutritionFacts(String ingredient){
       String url = makeUrlForIngredients(ingredient);
       System.out.println("the url is: " + url);
       NutritionFactsIngredient facts = null;
       try{
          JSONObject response = JsonReader.readJsonFromUrl(url);
          int Calories = response.getInt("calories");
          int TotalWeight = response.getInt("totalWeight");
          JSONArray DietLabelsJson = response.getJSONArray("dietLabels");
          JSONArray HealthLabelsJson = response.getJSONArray("healthLabels");
          JSONArray CautionsJson = response.getJSONArray("cautions");
          ArrayList<String> DietLabels = this.toArrayList(DietLabelsJson);
          ArrayList<String> HealthLabels = this.toArrayList(HealthLabelsJson);
          ArrayList<String> Cautions = this.toArrayList(CautionsJson);
          facts = new NutritionFactsIngredient(Calories,TotalWeight,
                                               DietLabels,HealthLabels,Cautions, ingredient);
       }catch (JSONException e){
           e.printStackTrace();
       }catch (IOException e){
           e.printStackTrace();
       }

       return facts;
   }
}

我不知道是否应该让spring处理我们应用程序中每个对象的生命周期。我刚刚在我的课堂上添加了@component注释来体验Spring,但我认为这还不够。我仍在使用我的方法中的new关键字实例化对象。我应该制作RecipePojo和NutritionFactsIngredients类的组件吗?这太令人困惑了,因为这些类的实例并不是唯一的。它们取决于用户输入。我怎样才能在这堂课上使用Spring?

编辑:所以人们告诉我,而不是这样做:

@Component
public class EdamamApi{
  public EdmamApi(){}
  public Recipe getRecipe(String recipe ){
     Recipe rep = Recipe(recipe);
     return rep;
  }
}

我应该这样做

@Component
public class EdamamApi{
  public EdmamApi(){}
  public void makeRecipe(String recipe,  Recipe rep){
     rep.setRecipeName(recipe);
  }
}

通过这种方式,该课程不受食谱限制,我只能使用豆食谱。

---&gt;这不是正确的但我正在尝试做什么&lt; ------

@Component
public class EdamamApi{
  public EdmamApi(){}
  public Recipe getRecipe(String name){
      @Autowired 
         Recipe rep;
       rep.setName(name);
      return rep;
  }
}

0 个答案:

没有答案