Liav Ezer
Liav Ezer

Reputation: 29

After adding @ComponentScan to load beans from a jar my controllers isn't scan and i get 404

I have a spring-boot application (Java8, spring-boot 2.1.4-RELEASE).

One of the services in the business layer need to @Autowire a bean from a certain jars in my classpath. In order to achieve that i had to add @ComponentScan({"package.to.my.bean.inside.the.jar"}) and it was magically scanned and wired successfully (this was added to the main spring-boot class which declare the main method).

However, since then my controllers aren't scanned hence the DispatcherServlet is returning 404 for every request i trigger (default dispatcher). Actually my entire spring-boot app annotations is being ignored - no scan is performed. Just to emphasis - the application worked perfectly before adding the @ComponentScan.

Main spring-boot app class:

package com.liav.ezer;

// This is the problematic addition that cause the endpoints to stop
// inside a jar in the classpath, in com.internal.jar package resides an 
// object which i need to wire at run time
@ComponentScan({"com.internal.jar"})
@SpringBootApplication
public class JobsApplication {
    public static void main(String[] args) {
        SpringApplication.run(JobsApplication .class, args);
        }
}

Controller example:

package com.liav.ezer.controller;

@RestController
@EnableAutoConfiguration
@RequestMapping(path = "/jobs")
public class JobController {

    @GetMapping(path="/create", produces = "application/json")
    @ResponseStatus(HttpStatus.OK)
    String createJob(@RequestParam() String jobName) String jobName){       
        return "job created...";
    }
}

I tried adding my spring-boot app base package to the list of packages in the @ComponentScan with no luck. I tried narrowing down the scope of the package declaration to be only on the class which i need with no luck. Here is the code

Upvotes: 1

Views: 2822

Answers (2)

Peter Gyschuk
Peter Gyschuk

Reputation: 1029

According to Spring documentation

Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's element. Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

In your case when you are adding

@ComponentScan({"com.internal.jar"}) you are disabling scanning of com.liav.ezer.controller

To fix it you can do the following configuration

@ComponentScan(basePackages = {"com.internal.jar", "com.liav.ezer.controller"})

Upvotes: 2

Qingfei Yuan
Qingfei Yuan

Reputation: 1212

If so, remove @ComponentScan, can declare that bean in yourself configuration. try below

@SpringBootApplication
public class JobsApplication {
    public static void main(String[] args) {
        SpringApplication.run(JobsApplication .class, args);
        }
     @Bean
     public BeanInOtherJar xxBean(){
        return new com.internal.jar.XXX();
     }
}

Upvotes: 0

Related Questions