antacerod
antacerod

Reputation: 1420

Java config beans from dependent Spring project are not being initialized

I have a 3 Spring projects: 'web', worker' and 'core'.

'core' includes common functionality from 'web' and 'worker' such as domain beans or jpa repositories and it is included in 'worker' and 'web' projects as jar.

POM in 'web' project

<dependency>
    <groupId>com.proyecti.magmainside221b</groupId>
    <artifactId>core</artifactId>
    <version>1.0.0.RC1</version>            
</dependency>

When I deploy 'web' project in Tomcat looks like Java config beans from 'core' are not being initialized since the 'datasource' bean (defined in 'core' project with @Bean in one class annotated with @Configuration) is not found to be injected into 'PersistentTokenBasedRememberMeServices'.

Caused by: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices#0': Cannot create inner bean '(inner bean)#1978517' of type [org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1978517': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

What I have to do to initialize my java config beans from 'core' project when I deploy 'web' project?

Thanks!

Edited:

I have found in the logs that my applicationContext files are being loaded.

2014-09-22 15:04:37,922 INFO [unknown] [localhost-startStop-1] o.s.b.f.x.XmlBeanDefinitionReader - Loading XML bean definitions from file [/home/aacevedo/workspaces/magma/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/web/WEB-INF/classes/META-INF/spring/applicationContext.xml]
2014-09-22 15:04:37,940 INFO [unknown] [localhost-startStop-1] o.s.b.f.x.XmlBeanDefinitionReader - Loading XML bean definitions from URL [jar:file:/home/aacevedo/workspaces/magma/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/web/WEB-INF/lib/core-1.0.0.RC1.jar!/META-INF/spring/applicationContext-core.xml]

I n my 'core' project I (think that) don't need applicationContext since I am using Java config and my main configuration class looks like this:

@Configuration
@EnableSpringConfigured
@EnableTransactionManagement
@EnableJpaRepositories("com.proyecti.magma.common.datasources.internal.repositories")
@ComponentScan( basePackages = {"com.proyecti.magma"}, 
            excludeFilters=
            {
                @ComponentScan.Filter(type=FilterType.ANNOTATION, value=Controller.class)
            })
@Order(1)
public class BasicDataSourceConfig 
{
...
}

I have put a breakpoint into BasicDataSourceConfig constructor but it is not being reached.

When I had unified this projects everithing worked ok.

Update 2:

This is how my project structure looks like:

enter image description here

Upvotes: 1

Views: 2171

Answers (2)

antacerod
antacerod

Reputation: 1420

Finally I have found a configuration that works for me.

It seems that @ComponentScan was not being detected when it was included in a config way with the annotation...I have created a regular applicationContext.xml file in my 'web' project adding the component-scan object with XML and now my @Configuration classes in both projects are being detected successfully.

Upvotes: 1

luboskrnac
luboskrnac

Reputation: 24571

You need to import 'core' context into 'web' context using @Import annotation.

Upvotes: 2

Related Questions