0

I have two Spring Boot in a Maven project in Eclipse. Lets call it project1 and project2. I have to use some POJO classes in project1 from project2. So I made project2 a dependency of project1. If I use project1 in eclipse it works fine. However if I create a war file from project1 I get an exception during startup in Tomcat.

Could not autowire field: private com.project2.entity.SomeBean com.project2.controller.SomeController.someBean;

So it seems Spring try to autowire the fields in project2 while I only want to use some POJO from project2. Do you have any idea how to prevent it?

This is my project1 application. As you can see I tried ComponentScan's exlude filter without success. I guess it works differently when I use it from Tomcat.

@SpringBootApplication
@ComponentScan(basePackages = {"com.project1"} ,excludeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "com.project2.*"))
public class Project1Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AbstractServiceGuiApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(AbstractServiceGuiApplication.class, args);
    }
}
Peter Ambruzs
  • 7,763
  • 3
  • 30
  • 36
  • 1
    What build system do you use? Maven? You need to define the project dependency inside of Maven. How do you build the war file? For me it sound like you use Eclipse do run the project (with dependency configured inside Eclipse) and Maven to build the WAR file (with the depdency missing inside the POM file). – Kevin Wittek Jan 05 '16 at 12:49
  • You can use `@ComponentScan`'s [excludeFilters](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html#excludeFilters--) property to specify the packages to exclude from scanning. However, the *real* fix would be to make your library not depend on Spring, for example by extracting the POJOs to a separate "vanilla" Java submodule. – kryger Jan 05 '16 at 13:09
  • Possible duplicate of [exclude @Component from @ComponentScan](http://stackoverflow.com/questions/18992880/exclude-component-from-componentscan) – kryger Jan 05 '16 at 13:25
  • I use a Maven project, I updated it in the question. – Peter Ambruzs Jan 05 '16 at 13:35
  • I tried ComponentScan exclude, but it did not helped for me. I updated the question. – Peter Ambruzs Jan 05 '16 at 14:32
  • Post your MAven POM as well as SomeController and SomeBean – luboskrnac Jan 05 '16 at 15:08
  • You probably need to fix your pattern to `com.project2..*`. Check this question for the explanation of wild characters: http://stackoverflow.com/questions/12303142/what-do-and-mean-in-aspectj – jny Jan 05 '16 at 18:30

0 Answers0