Shivayan
Shivayan

Reputation: 136

Can't make messageSource work in the Pojo classes

I am not being able to make messageSource work in the Pojo classes,its throwing a nullpointerexception. However in all the other classes namely controller,service messageSource is working alright. Could someone please suggest what needs to be done ?

@Autowired
private  MessageSource messageSource;

I have autowired the MessageSource using the above code snippet.

public class ProposalWiseSelectionForm implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Autowired
private  MessageSource messageSource;

private String txtPageHierarchy="";
private String txtLineOfBusiness;
private String txtProduct;
private String btn;
private String clickedGo="N";   
private List arrLineOfBusiness=new ArrayList();
private List arrProduct=new ArrayList();
@Valid
private ArrayList documentList=initiateDocumentList();
private String txtPageMode="I";
private String enableDiscardBtn="N";
private String enableInsertBtn="N";
private String isDivVisible="N";
private int numApplicationType=1;


 public ProposalWiseSelectionForm() {
}



public String getTxtPageHierarchy() {
    return txtPageHierarchy;
}


public void setTxtPageHierarchy(String txtPageHierarchy) {
    this.txtPageHierarchy = txtPageHierarchy;
}


public String getTxtLineOfBusiness() {
    return txtLineOfBusiness;
}
public void setTxtLineOfBusiness(String txtLineOfBusiness) {
    this.txtLineOfBusiness = txtLineOfBusiness;
}
public String getTxtProduct() {
    return txtProduct;
}
public void setTxtProduct(String txtProduct) {
    this.txtProduct = txtProduct;
}
public String getBtn() {
    return btn;
}
public void setBtn(String btn) {
    this.btn = btn;
}
public String getClickedGo() {
    return clickedGo;
}
public void setClickedGo(String clickedGo) {
    this.clickedGo = clickedGo;
}


public List getArrLineOfBusiness() {
    return arrLineOfBusiness;
}

public void setArrLineOfBusiness(List arrLineOfBusiness) {
    this.arrLineOfBusiness = arrLineOfBusiness;
}

public List getArrProduct() {
    return arrProduct;
}

public void setArrProduct(List arrProduct) {
    this.arrProduct = arrProduct;
}

public void setArrProduct(ArrayList arrProduct) {
    this.arrProduct = arrProduct;
}
public ArrayList getDocumentList() {
    return documentList;
}
public void setDocumentList(ArrayList documentList) {
    this.documentList = documentList;
}
public String getTxtPageMode() {
    return txtPageMode;
}
public void setTxtPageMode(String txtPageMode) {
    this.txtPageMode = txtPageMode;
}





public String getEnableDiscardBtn() {
    return enableDiscardBtn;
}


public void setEnableDiscardBtn(String enableDiscardBtn) {
    this.enableDiscardBtn = enableDiscardBtn;
}


public String getEnableInsertBtn() {
    return enableInsertBtn;
}


public void setEnableInsertBtn(String enableInsertBtn) {
    this.enableInsertBtn = enableInsertBtn;
}


public String getIsDivVisible() {
    return isDivVisible;
}
public void setIsDivVisible(String isDivVisible) {
    this.isDivVisible = isDivVisible;
}
public int getNumApplicationType() {
    return numApplicationType;
}
public void setNumApplicationType(int numApplicationType) {
    this.numApplicationType = numApplicationType;
}
}

Upvotes: 1

Views: 849

Answers (3)

shlomi33
shlomi33

Reputation: 1568

You have to make your class a Spring bean

Add @Component annotation to your class and add these 2 lines to your appContext.xml:

<context:component-scan base-package="com.<your-company-name>" />
<context:annotation-config />

Or just add the service in your beans section in the appContext.xml if you wish not to work with Spring component-scan feature.

Upvotes: 0

geoand
geoand

Reputation: 64011

In order to be able to use @Autowired in a class, that class has to be managed by Spring. of Your ProposalWiseSelectionForm class is obviously not managed by Spring and therefor messageSource is always null.

Using @Autowired MessageSource messageSource in your other classes works, because as you mention those classes are managed by Spring (as you have mentioned they are either controllers, services etc).

I am guessing that ProposalWiseSelectionForm is a DTO used to capture values from a form. The sort of class will not be a Spring bean and therefor you can't autowire stuff into it.

I suggest you either move the logic you need out of the DTO and into the controller (or some Spring managed utility) or in the extreme case that you absolutely need @Autowired in the DTO, take a look at @Configurable here and here

Upvotes: 1

user3728784
user3728784

Reputation: 11

Try using @Component,you might be getting this issue because of the fact the Pojo class is not being recognized.

Upvotes: 1

Related Questions