Gorkamorka
Gorkamorka

Reputation: 448

Autowire class from different Maven module?

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

public class DITestMain {
    public static void main(String[] args) {
        new DITest().run();
    }
}

applicationContext.xml located in spring-di-test-core's resources folder

<?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

@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

@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

<?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?

Upvotes: 1

Views: 3698

Answers (2)

pap
pap

Reputation: 27604

1: You're not creating the Spring context in your main method

2: Have you done all the required configuration to enable load-time weaving (so that your @Configurable bean will work)?

Take a look at http://static.springsource.org/spring/docs/3.0.x/reference/aop.html#aop-using-aspectj and read the entire chapter closely.

Upvotes: 2

Japan Trivedi
Japan Trivedi

Reputation: 4483

This will not work since you are executing the class DITestMain as a java application. And your application contexts will not be loaded. You need to run this as a web application to test. If you will run with as an Java Application the object of randomStringService in DITest will not be created and you will get NPE at that point.

Hope this helps you.

Upvotes: 0

Related Questions