Jesse James
Jesse James

Reputation: 1223

Creating A Bean From Another Maven Module

Creating a bean in a Maven project is rather simple. You add the @Autowiredannotation to the bean you wish to create and then declare its config in the spring-config.xml:

@Autowired
private ExampleBean exampleBean;
.
.
.

<bean id="exampleBean" class="path.to.your.bean">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

But what if I have a multi-moduled Maven project and the bean I would like to create is from another module referenced in the build path? How do you go about creating that bean?

I thought about the annotation @Resource but it didn't seem to "detect" the bean that is coming from another module added in the build path.

UPDATE 1: Based on Rémi's answer, here's what I did:

I added the following line to the xml config file of the module I wish to create the bean in:

<import resource="classpath:\dsp2aisp-business\*\root-context.xml" />

I also kept the @Autowired annotation on the AccountService (which is the bean I would like to use in this module).

In the other module where the AccountService is declared I added the bean declaration:

<bean id="accountService" class="fr.bnp.dsp2aisp.service.AccountService">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

But I kept getting this error:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [fr.bnp.dsp2aisp.service.AccountService] for bean with name 'accountService' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]; nested exception is java.lang.ClassNotFoundException: fr.bnp.dsp2aisp.service.AccountService

But I'm certain that the class does in fact exist in the specified path.

Is there maybe another way to use a bean that is declared in another Maven Module?

Upvotes: 1

Views: 3384

Answers (2)

Jesse James
Jesse James

Reputation: 1223

I finally got it to work. When you have a multi-moduled Maven project, and you're trying to import one of those modules into another to use its classes, you shouldn't add your module from the build path (properties -> Java Build Path -> Projects ->add) because the compiler won't be able to find those classes in runtime.

What you need to do is to simply add those modules as Maven dependencies in the POM.xml file of the module you wish to use them in.

Example:

    <dependency>
        <groupId>com.test</groupId>
        <artifactId>test-infra</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

The solution is insanely easy but finding it was a little bit tiring. I hope it'll help somebody else with the same problem.

Upvotes: 3

R&#233;mi Kaeffer
R&#233;mi Kaeffer

Reputation: 21

You could do this by importing other maven project bean context form classpath :

<import resource="classpath:path/to/another/maven/module/context.xml" />

Then you can use all the beans defined in other maven project context in your actual maven project

Upvotes: 0

Related Questions