Announcement Announcement Module
Collapse
No announcement yet.
Autowiring class from different Maven module throws NullPointerException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring class from different Maven module throws NullPointerException

    I have a maven project with the following structure:

    spring-di-test
    +--spring-di-test-core
    | com.example.core.DITestMain
    +--spring-di-test-store
    com.example.store.DITest
    com.example.store.RandomStringService

    where spring-di-test is the root project and the two below are modules.

    My classes look like this:

    DITestMain located in spring-di-test-core

    Code:
    public class DITestMain {
        public static void main(String[] args) {
            new DITest().run();
        }
    }
    applicationContext.xml located in spring-di-test-core's resources folder

    Code:
    <?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:spring-configured/>
        <context:component-scan base-package="com.example.*" annotation-config="true"/>
    </beans>
    DITest located in spring-di-test-store

    Code:
    @Configurable(preConstruction = true)
    @Controller
    public class DITest {   
        @Autowired(required=true)
        private RandomStringService randomStringService;
    
        public void run() {
            System.out.println(randomStringService.getRandomString());
        }
    }
    RandomStringService located in spring-di-test-store

    Code:
    @Service("randomStringService")
    public class RandomStringService {
    
        private final Random random;
    
        public RandomStringService() {
            random = new Random();
        }
    
        public String getRandomString() {
            StringBuilder sb = new StringBuilder();
            int length = random.nextInt(20);
            for (int i = 0; i < length + 1; i++) {
                sb.append(new Character((char) ('a' + random.nextInt(20))));
            }
            return sb.toString();
        }
    }
    applicationContext.xml located in spring-di-test-store

    Code:
    <?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:spring-configured/>
        <context:component-scan base-package="com.example.*" annotation-config="true"/>
    </beans>
    When I run DITestMain, I get a NullPointerException for randomStringService. What went wrong?

  • #2
    I don't see anywhere you are loading the applicationContext.xml. For this to work, you need to get the DITestMain bean from the applicationContext.xml.

    Comment

    Working...
    X