Spring: set up @ComponentScan correctly

I have the following setting for my Spring Application Context .

 @Configuration public class RmiContext { @Bean public RmiProxyFactoryBean service() { RmiProxyFactoryBean rmiProxy = new RmiProxyFactoryBean(); rmiProxy.setServiceUrl("rmi://127.0.1.1:1099/Service"); rmiProxy.setServiceInterface(Service.class); return rmiProxy; } } 

 @Configuration public class LocalContext { @Bean public Controller Controller() { return new ControllerImpl(); } } 

 @Configuration @Import({RmiContext.class, LocalContext.class}) public class MainContext { 

}

The above setup works fine, but I want to enable the @ComponentScan annotation of the Controller using @Component , since my application has a lot of Controller , which is tedious when declared one by one using @Bean .

 @Configuration @ComponentScan(basePackageClasses = {Controller.class}) public class LocalContext { /* ... */ } 
The problem is that when I do @ComponentScan(basePackageClasses = {Controller.class}) , the previously fine working RmiProxyFactoryBean are not recognized or can't be created.

So, how do I configure my MainContext to create beans through RMI and local beans?

+6
source share
3 answers

@Configuration is also a candidate for component verification, so you can scan all beans in RmiContext and all controllers in your controller package:

 @Configuration @ComponentScan(basePackages = {"org.example.controllers", "package.of.RmiContext"}) public class MainContext { } 

- edit -

@ Configuration is a candidate for component verification, here is a test case that works on my computer:

 package scan.controllers; @Controller public class ExampleController { } package scan; public interface RMIService { } package scan; @Configuration public class RmiContext { @Bean public RmiProxyFactoryBean service() { RmiProxyFactoryBean rmiProxy = new RmiProxyFactoryBean(); rmiProxy.setServiceUrl("rmi://127.0.1.1:1099/Service"); rmiProxy.setServiceInterface(RMIService.class); rmiProxy.setLookupStubOnStartup(false); return rmiProxy; } } package scan; @Configuration //MainContext will auto scan RmiContext in package scan and all controllers in package scan.controllers @ComponentScan(basePackages = {"scan", "scan.controllers"}) public class MainContext { } package scan; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={MainContext.class}) public class TestContext { @Autowired private RMIService rmi; @Autowired private ExampleController controller; @Test public void test() { //both controller and rmi service are autowired as expected assertNotNull(controller); assertNotNull(rmi); } } 
+2
source

Perhaps you can try using the base packages of your classes (RMI, Controller):

 @ComponentScan(basePackages = {"your controller package", "your rmi package"}) 

If the RMI class package is different from the controller, it will not be able to instantiate spring.

+1
source

If you understand correctly, you use "@ComponentScan (basePackageClasses", but it does not detect and register your spring beans?

I had the same problem a few minutes ago. I did not give up and tried all the ridiculous guesses. One of them did it.

I had to add an XML component record to an XML file. I just put a dummy package there, as shown below:

  component-scan base-package="dummy.filler.to.enable.component.scan" 

XML scan component seems to allow @ComponentScan.

[Late Edit: I noticed my spring xml spring 2.5 schema. Anyway, I don't know if that matters. Best wishes.]

0
source

Source: https://habr.com/ru/post/950265/


All Articles