2015-10-07 76 views
3

自動裝配的問題,我有一個類象下面這樣:與SpringBoot

package com.company.data.render.model 
@RestController 
public class ControllerClass { 

@Autowired 
ApplicationPropertiesServiceImpl services; 

@RequestMapping(value = "/node1", method = RequestMethod.GET) 
@ResponseBody 
public ParentNode getNode1() 
{ 


    Child node = new Child(); 
    List<Map<String, Object>> properties properties = services.getData("A",xxx); 
    node.addtree(); 
    node.setProperties(properties); 
    return node; 
} 
} ------------------------------------------------------------------------------- 

package com.company.data.service; 
@Component 
public List<Map<String, Object>> getData(String type,String name) 
{ 
     if(type.equalsIgnoreCase("A")) 
     { 
      String sql = "select * from data.data_properties(?)"; 
      List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql,host); 
      return rows; 
     }else if(properties.equalsIgnoreCase("B")) 
     { 
      String sql = "select * from data.application_properties(?)"; 
      List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql,host); 
      return rows; 
     } 

} 

------------------------------------------------------------------------------- 
package com.company.data.render.model; 

@Component 
public class Child { 

@Autowired 
ApplicationPropertiesServiceImpl services; 


public void addtree() 
{ 
List<Map<String, Object>> properties=services.getData("B", "xxy"); 

} 
} 

我如何可以訪問的GetData()功能兒童 class.I我得到空指針異常的服務對象雖然我已經自動裝配ApplicationPropertiesServiceImpl

+1

錯誤拼寫應該是'@ Autowired'註釋。但是,將控制器連接到另一個組件是個不錯的主意。如果你將公共代碼提取到一個單獨的'@Service' bean中,並從控制器和''Child'類引用這個bean,那將會更好。當然,您還必須正確設置應用程序上下文並對其進行初始化。 – hotzst

+0

你最好分開邏輯,目前你的控制器做的東西太多了。我建議你做2個類:一個服務和一個DAO(倉庫)。存儲庫會給你數據庫的結果。該服務將具有DAO對象+一些業務。兩個控制器都將具有自動裝配的服務。 – korogui

+0

@korogue --evenif我創建了一個單獨的類..我將如何在類中獲得相同的服務對象? – Aman

回答

0

好像你將有2個控制器。

顯然你的控制器做得太多了。將控制器注入另一個控制器並不是一個好主意。

我建議你做一個服務和庫:

該模型接收到來自控制器的數據量,所以我建議創建一個類,使之更加明確,因爲返回地圖過於抽象,使代碼難以閱讀。

public class CarProperties { 
    private Integer id; 
    private String name; 
    private Integer age; 
    private String color; 
    //setters and gettters 
    .... 
} 

服務:

public interface CarPropertiesService { 
    public List<CarProperties> findAll(String type); 
} 

@Service("CarPropertiesService") 
public class CarPropertiesServiceImpl implements CarPropertiesService { 
    @Autowired 
    private CarPropertiesDAO carPropertiesDAO; 

    public List<CarProperties> findAll(String type) { 
     List<CarProperties> result = new ArrayList<>(); 
     if ("XXX".equalsIgnoreCase(type)) { 
     List<Map<String, Object>> carPropertiesList = carPropertiesDAO.findAll(); 
     for(Map<String, Object> carProperties : carPropertiesList) { 
      result.add(getCarPropertiesInstance(carProperties)); 
     } 

     } 
     return result; 
    } 

    private CarProperties getCarPropertiesInstance(Map<String, Object> properties) { 
     CarProperties instance = new CarProperties(); 
     instance.setId(properties.get("id")); 
     instance.setName(properties.get("name")); 
     ... 
     return instance; 
    } 
} 

DAO:

public interface CarPropertiesDAO { 
    public List<Map<String, Object>> findAll(); 
} 

@Repository("CarPropertiesDAO") 
public class CarPropertiesDAOImpl implements CarPropertiesDAO { 
    ... 
    public List<Map<String, Object>> findAll() { 
     String sql = "select * from data.car_properties(?)"; 
     return jdbcTemplate.queryForList(sql,host); 
    } 
} 

,最後你的控制器就可以使服務的使用:

@RestController 
public class ControllerClass { 
    @Autowired 
    private CarPropertiesService carPropertiesService; 

    @RequestMapping(value = "/node1", method = RequestMethod.GET) 
    @ResponseBody 
    public ParentNode getNode1() 
    { 
     List<CarProperties> properties = carPropertiesService.findAllByType("XXX"); 
     return properties; 

    } 

    @RequestMapping(value = "/prop/{type}/{name}", method = RequestMethod.GET) 
    @ResponseBody 
    public List<CarProperties> getData(@PathVariable String type,@PathVariable String name) 
    { 
     List<CarProperties> rows = carPropertiesService.findAllByType(type);  
     ... 
    } 
} 

@Controller 
public class Controller2 { 
    @Autowired 
    CarPropertiesService carPropertiesService; 


    public void addtree(){ 
     List<CarProperties> rows = carPropertiesService.findAllByType("XXX"); 
    } 
} 

恢復:控制器不應該擔心生意只返回數據。服務應該是業務所在的地方,比如計算,數據交換等......你可以清楚地看到它的DAO類用於數據庫操作。同時請記住,DAO對象的訪問權限應該在Services/Facedes中,在Controller推薦的Controller中使用它。因此,使用這種結構,您的代碼將變得更加可重用且易於維護。

+0

但是需要兩個dao和impl類的必要條件...它不是令人困惑的littilbit – Aman

+0

不是真的,一個是接口,另一個是類。您可以重複使用不同類別的界面。 – korogui

+0

這種解決方案是不會適合我的問題..反正謝謝Becoz這不是一個表我查詢..它是我通過查詢執行的功能.. – Aman

0

請試試這個: -

@Component 
@ComponentScan(basePackages="<provide your base pkg where it will scan for the component ControllerClass>") 
@ConditionalOnBean(ControllerClass.class) 
public class Child { 
    @Autowired(required=true) 
    private ControllerClass controllerClass; 

    public void addtree(){ 
    controllerClass.getData("XXX",xxx) 
    } 
} 
+0

號。 。這是行不通的... – Aman

+0

請檢查我上面的更新建議,以包括註釋@ComponentScan。 – Avis

+0

No ..同名問題 – Aman