0

hello everyone what Im trying to do is prefill a CustomDetail Object with some properties with the help of @Value and @PostConstruct, then use this prefilled Object on a service but when the Object arrives to the service class its null already...

UPDATED!!!! this is the right code

@Service
public Class CustomDetail(){


 ProxyObject prx; 
 ...
 @Value("#{myProperties.proxy.address}")
 String propertyPrx;

 @PostConstruct
 private void setProperty(){
  prx= new ProxyObject(propertyPrx);
 }
 .....
 ....
}

Now setProperty() method effectively does its job correctly, if I Start Tomcat on debug I can see that propertyPrx its good and the Object prx is NOT null... so new ProxyObject(propertyPrx) gets Called without any issues, at this point everything fine. now when I move to the service part and try to useit

public Class ServiceClient(){

 ....
 ...
 @Autowired
 CustomDetail cDetail;


 ....
 ...more code
  cDetail.someMethod(a,n,y);   //<---cDetail Object is null so I get a NullPointerException

}

any ideas why this is happening and why the @PostConstruct only gets initialized when Tomcat starts for the first time and when I try to injected into the service class is already null like if it never got initialized thanks for your help... I'm using SpringMVC 3.1 Tomcat

Jesse
  • 29
  • 1
  • 9
  • Can you please also show the place where you have the bean definition of ServiceClient, is it also annotated with @Service? – Biju Kunjummen Jun 19 '12 at 01:11
  • no it does not have any @annotation(Component,Service... at least not in the original one nor any bean definition on any .xml file) I have tried with all this before and nothing appears to replicate the original code so I'm thinking maybe its some configuration(apart from Subin suggestion have tried also the – Jesse Jun 19 '12 at 04:51
  • I think that is the problem Jesse, Autowired works by scanning bean definitions, looking for @Autowired annotated fields and injecting in the dependencies, so even ServiceClient has to be a Spring bean for it's dependencies to be wired in by Spring. – Biju Kunjummen Jun 19 '12 at 12:20
  • I have found a xml file containing the bean reference to ServiceClient in the original code, still no luck on getting this to work on my new code.Im trying to use @Component("serviceClient") so there will be a bean reference with the same name, I'm not sure if doing both xml style beans and annotations cause some incompatibility between them? still the "@"autowired CustomDetail object goes null at that point. What I did test was to Autowired CustomDetail on a "@"Controller and it works so Im not sure what is the difference in context I presume between declaring the bean on xml or"@controller – Jesse Jun 20 '12 at 00:41

1 Answers1

0

try using @Component on CustomDetail, and use

<context:component-scan base-package=”your.package” />

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
  • Hi Subin I updated the code so it will reflect what I have right now I already have those configurations on my XMLs conf files and as you can see I'm also using @service instead of "@"component so I think Spring container will find it. the thing with this code is that it comes from another project so we're trying to reuse it, once again thanks for the help – Jesse Jun 18 '12 at 16:10