facebook

Autowire – how to

  1. MyEclipse IDE
  2.  > 
  3. Spring Development
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #312462 Reply

    Greg Soulsby
    Member

    Now I am ready to include my first POJO into the app.

    I tried @Autowire in the line prior to my new attribute, in one of the autogenerated controllers, just copying the approach in the generated code.

    Get error
    org.springframework.beans.factory.BeanCreationException: Could not autowire field: …

    Can you explain how to autowire in propriatary beans? Any background on autowiring approach use by Myeclipse would be of interest.

    Regards

    Greg

    #312483 Reply

    davemeurer
    Member

    Hi Greg,

    Just to clarify – the annotation is @Autowired (with a “d” at the end). Assuming, you know this – but want to make sure.

    The POJO does need to be recognized as a Spring bean.

    When you get a moment, please post the POJO and other bean you are trying to autowire into.

    Thanks,
    Dave

    #312500 Reply

    Greg Soulsby
    Member

    Dave

    This is the POJO

    package prop;

    import java.io.FileOutputStream;
    import java.util.Calendar;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.jar.JarFile;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Date;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CreationHelper;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;

    public class Scores_to_spreadsheet {

    /**
    * @param args
    */
    private JarFile file;
    private Short dd;
    public static void main(String args[]) throws Exception {
    System.out.println(“Got in”);
    Scores_to_spreadsheet test = new Scores_to_spreadsheet();
    test.generate_scores2(args[0]);
    }
    private JarFile getFile() {
    return file;
    }

    private void setFile(JarFile file) {
    this.file = file;
    }

    public String generate_scores2(String campaign_code) throws Exception
    {
    Workbook wb = new HSSFWorkbook();
    //Workbook wb = new XSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet(“new sheet”);

    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short)0);
    // Create a cell and put a value in it.
    Cell cell = row.createCell(0);
    cell.setCellValue(1);

    // Or do it on one line.
    row.createCell(1).setCellValue(1.2);
    row.createCell(2).setCellValue(createHelper.createRichTextString(“This is a string”));
    row.createCell(3).setCellValue(true);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream(“/home/innovativ/Desktop/workbook.xls”);
    wb.write(fileOut);
    fileOut.close();
    return “result is at ” + file;
    }
    }

    This is the start of the autogenerated bean I would like to use the POJO

    package aah3.web;

    import aah3.dao.CampaignsDAO;

    import aah3.dao.HisScoresDAO;

    import aah3.domain.Campaigns;

    import aah3.domain.HisScores;

    import aah3.service.CampaignsService;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.WebDataBinder;

    import org.springframework.web.bind.annotation.InitBinder;

    import org.springframework.web.bind.annotation.ModelAttribute;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RequestParam;

    import org.springframework.web.servlet.ModelAndView;

    // XXX save the spreadsheet of scores

    import prop.Scores_to_spreadsheet;

    /**

    * Spring MVC controller that handles CRUD requests for Campaigns entities

    *

    */

    @Controller(“CampaignsController”)

    public class CampaignsController {

    // XXX autowire the save spreadsheet of scores

    // @Autowired

    private Scores_to_spreadsheet scores_to_spreadsheet;

    /**

    * DAO injected by Spring that manages Campaigns entities

    *

    */

    @Autowired

    private CampaignsDAO campaignsDAO;

    /**

    * DAO injected by Spring that manages HisScores entities

    *

    */

    @Autowired

    private HisScoresDAO hisScoresDAO;

    /**

    * Service injected by Spring that provides CRUD operations for Campaigns entities

    *

    */

    @Autowired

    private CampaignsService campaignsService;

    /**

    * Create a new Campaigns entity

    *

    */

    @RequestMapping(“/newCampaigns”)

    public ModelAndView newCampaigns() {

    ModelAndView mav = new ModelAndView();

    mav.addObject(“campaigns”, new Campaigns());

    mav.addObject(“newFlag”, true);

    mav.setViewName(“campaigns/editCampaigns.jsp”);

    return mav;

    }

    /**

    * Delete an existing Campaigns entity

    *

    */

    @RequestMapping(“/deleteCampaigns”)

    public String deleteCampaigns(@RequestParam Integer campaignIdKey) {

    Campaigns campaigns = campaignsDAO.findCampaignsByPrimaryKey(campaignIdKey);

    campaignsService.deleteCampaigns(campaigns);

    return “forward:/indexCampaigns”;

    }

    /**

    * Entry point to show all Campaigns entities

    *

    */

    public String indexCampaigns() {

    return “redirect:/indexCampaigns”;

    }

    /**

    * Register custom, context-specific property editors

    *

    */

    @InitBinder

    public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.

    binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());

    binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());

    binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));

    binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));

    binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));

    binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));

    binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());

    }

    /**

    * Edit an existing Campaigns entity

    *

    */

    @RequestMapping(“/editCampaigns”)

    public ModelAndView editCampaigns(@RequestParam Integer campaignIdKey) {

    ModelAndView mav = new ModelAndView();

    mav.addObject(“campaigns”, campaignsDAO.findCampaignsByPrimaryKey(campaignIdKey));

    mav.setViewName(“campaigns/editCampaigns.jsp”);

    return mav;

    }

    #312509 Reply

    davemeurer
    Member

    Thanks for posting the classes.

    So, the Scores_to_Spreadsheet POJO is not recognized by the Spring container. To get it recognized, you’ll need to annotate it, and then add it’s package to the component scan in the spring context files.

    What I did to get this deploy is:
    1. Annotated the Scores_to_Spreadsheet with @Service
    2. Added the following to resources/[projectname]-generated-service-context.xml:

    <context:component-scan base-package="prop" scoped-proxy="interfaces" />

    HTH,
    Dave

    #312590 Reply

    Greg Soulsby
    Member

    Ok, I even understand that. Progress! Many thanks

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Autowire – how to

You must be logged in to post in the forum log in