Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowired Spring Beans across Java packages

I’m attempting to break my project up into three modules: core, admin and user so that I can share common code via core. The problem is that I can’t get Spring to pickup the autowired beans across different main packages, when I have everything in the same package it works.

In the com.mickeycorp.core package I have the models, services, etc that I want the admin and user modules to use. In com.mickeycorp.admin is the my WebApplicationStarter (extends SpringBootServletInitializer) where I’ve got:

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(SpringConfiguration.class);
    return application.sources(WebApplicationStarter.class);
}

Which I believe should pickup my configuration class where I have the following:

@Configuration
@ComponentScan("com.mickeycorp")
public class SpringConfiguration {

}

Clearly I’ve misunderstood something.. I thought that setting ComponentScan would have Spring scan through packages under com.mickeycorp for component annotations?

like image 383
Clint Avatar asked Mar 26 '15 06:03

Clint


People also ask

What is difference between @bean and @autowire?

@Bean is just for the metadata definition to create the bean(equivalent to tag). @Autowired is to inject the dependancy into a bean(equivalent to ref XML tag/attribute).

Can we use @autowired in pojo?

The beans can be wired via constructor or properties or setter method. For example, there are two POJO classes Customer and Person. The Customer class has a dependency on the Person. @Autowired annotation is optional for constructor based injection.

Can we Autowire a bean in configuration class?

4.2.1.One @Configuration class may directly reference bean instances registered from another using Spring's @Autowired annotation.

Can beans be Autowired?

The Spring framework enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, Spring container can autowire relationships between collaborating beans. This is called Spring bean autowiring.


2 Answers

I was on the right track.. adding @ComponentScan was only a third of the way there and is correct but it doesn't configure Spring to scan for other types - it only covers @Component @Repository, @Service, or @Controller annotations. I had to add the following to pickup @Entity and @Repository:

@EntityScan("com.mickeycorp.core")
@EnableJpaRepositories("com.mickeycorp.core")

Overriding SpringApplicationBuilder is also unnecessary in this case as the SpringConfiguration class is automatically picked up.

References:

Spring Docs: Entity Scan

Spring Docs: EnableJpaRepositories

like image 58
Clint Avatar answered Oct 26 '22 22:10

Clint


@ComponentScan annotation has to be used.

Please refer the Spring documentation which says

Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan.

@ComponentScan(basePackages={"package1","package2"})
like image 23
Harish Kumar Avatar answered Oct 26 '22 21:10

Harish Kumar