Question

This is a simple Spring bean, where one of the fields complexMsg is autowired. But on running, the bean is null. Spring version is 3.2. Similar issue has been posted earlier, the solution of which is already existing in my code. Bean code :

public class GreetingServiceImpl implements IGreetingService {
private String simpleMsg = null;    
//Autowired annotation is equivalent to autowire="byType"
@Autowired
private ComplexMessage complexMsg ; 

public String getSimpleMsg() {
    return simpleMsg;
}
     ...

Spring config xml :

   <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    
<context:annotation-config />
<bean id="greetingBean" class="simple.GreetingServiceImpl">
    <property name="simpleMsg" value="HelloWorld!!" />  
</bean> 
<bean id="complexMsg" class="simple.ComplexMessage">
    <property name="receiver" value="Raka" />
    <property name="content" value="MerryChristmas" />
</bean> 
</beans>

Main class :

public static void main(String[] args) {
    IGreetingService greetingSrvc = null;       
    BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("greetingConfig.xml"));
    greetingSrvc =(IGreetingService) beanFactory.getBean("greetingBean");

greetingSrvc.complexMsg is null.

Was it helpful?

Solution

I really think you should use ApplicationContext other tan BeanFacotry. The reason is that ApplicationContext implements 1 interface which I think is BeanPostProcessor And BeanFacotry doesn't. Note beanfactory is short of features, such as @Transational.

OTHER TIPS

probably you missed out on the component scan?

<context:component-scan base-package="simple"/>

it this .class file is in a .jar file probably you could add the

@Component

annotation in addition to the

@Service

annotation.

eg:

@Service @Component public class Abc{}

I have tried it and it is working for me, i used application context here. your beans.xml file content was not appropriate i changed it.. let me know for anything else.. see below for my full code

beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">   
<context:annotation-config />
<bean id="greetingBean" class="com.kb.autowiring2.GreetingServiceImpl">

</bean> 
<bean id="complexMsg" class="com.kb.autowiring2.ComplexMessage">

</bean> 
</beans>

package com.kb.autowiring2;

public interface IGreetingService {
    public ComplexMessage getComplexMsg();

}


package com.kb.autowiring2;

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

public class GreetingServiceImpl implements IGreetingService {
    @Autowired
    private ComplexMessage complexMsg ;

    public ComplexMessage getComplexMsg() {
        return complexMsg;
    }

}


package com.kb.autowiring2;

public class ComplexMessage {

}


package com.kb.autowiring2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kb.autowiring1.Person;

public class Client {

    public static void main(String[] args) {
         IGreetingService greetingSrvc = null;       
         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/autowiring2/beans.xml");
            IGreetingService p = applicationContext.getBean("greetingBean",IGreetingService.class);
            System.out.println(p.getComplexMsg());


    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top