PDA

View Full Version : @ComponentScan and Lazy-initialization of beans ?



wichtounet
Apr 21st, 2009, 07:14 AM
Hi,

I'm converting my spring configuration to use JavaConfig and i've a problem with component-scan.

Before using JavaConfig, i used a ClassPathBeanDefinitionScanner. I need lazy-init on the beans, so i made :


BeanDefinitionDefaults defaults = new BeanDefinitionDefaults();
defaults.setLazyInit(true);
scanner.setBeanDefinitionDefaults(defaults);

But now, i didn't found how to do that in JavaConfig. I thougt that i can just set the defaultLazy option of the @Configuration, but it didn't work.

Actually, i use the context like that :


JavaConfigApplicationContext configApplicationContext =
new JavaConfigApplicationContext(SpringConfiguration.c lass, FilmsSpringConfiguration.class, PrimaryConfiguration.class);

And my three configurations looks like that :


@ComponentScan("org.jtheque.core.managers.persistence.context")
@Configuration(defaultLazy = Lazy.TRUE)
@AnnotationDrivenConfig
@AnnotationDrivenTx(transactionManager = "txManager")
public class SpringConfiguration extends ConfigurationSupport{
@Bean
public EntityManagerFactory entityManagerFactory(){
EntityManagerFactoryBean factory = new EntityManagerFactoryBean();

return (EntityManagerFactory)getObject(factory);
}

@Bean
public JpaTransactionManager txManager(){
return new JpaTransactionManager(entityManagerFactory());
}

@Bean
public ResourceBundleMessageSource messageSource(){
return new ResourceBundleMessageSource();
}
}


@Configuration(defaultLazy = Lazy.TRUE)
@ComponentScan(value = {
"org.jtheque.films.services.impl",
"org.jtheque.films.persistence.dao.impl",
"org.jtheque.films.view.impl.frames",
"org.jtheque.films.view.impl.panels",
"org.jtheque.films.controllers.impl"}
)
public class FilmsSpringConfiguration {

}


@Configuration(defaultLazy = Lazy.TRUE)
@ComponentScan(value = {
"org.jtheque.primary.dao.impl",
"org.jtheque.primary.services.impl",
"org.jtheque.primary.view.frames",
"org.jtheque.primary.controller"}
)
public class PrimarySpringConfiguration {
}

Is there a way to enable the lazy-initialization on my scanned components ?

Thank you very much and thanks for this very good project :)

Chris Beams
Apr 21st, 2009, 12:56 PM
Hi @wichtounet,

I'm sorry to report that, even though the 'defaultLazy' attribute is there on @Configuration, it was never properly supported. There is an existing bug on this matter, and you can read my response there:

http://jira.springframework.org/browse/SJC-263

Best,

- Chris

wichtounet
Apr 21st, 2009, 01:54 PM
Hi @wichtounet,

I'm sorry to report that, even though the 'defaultLazy' attribute is there on @Configuration, it was never properly supported. There is an existing bug on this matter, and you can read my response there:

http://jira.springframework.org/browse/SJC-263

Best,

- Chris

Hi,

Thank you for responding.

That's not a good news for me :(

I need the lazy-init on my scanned beans.

Is there a way to mix a ClassPathBeanDefinitionScanner and a JavaConfigApplicationContext ?

wichtounet
Apr 21st, 2009, 04:15 PM
Or another way : Is that possible to define component-scan with defaults lazy in different XML imported with @ImportXML ?

This is not elegant, but if it works, i can use that waiting of an issue to JavaConfig ComponentScan.