7

I have following set up 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 @ComponentScan annotating Controllers with @Component as there are many Controllers in my application 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 so that both beans via RMI and local beans are created?

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125

3 Answers3

2

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

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

--edit--

@Configuration is a candidate for component scan, here is the test case that works in my pc:

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);
    }
}
Septem
  • 3,614
  • 1
  • 20
  • 20
  • Like I've mentioned in question, the beans via RMI (i.e service) are not recognized (i.e No bean of type `Service` found) exceptions when `@ComponentScan` is used for other beans annotated with `@Component` – TheKojuEffect Aug 03 '13 at 04:16
1

May be you could try using the base packages of your classes (RMI, Controller):


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

If the RMI classes package is different than controller then they will fail to instantiate by spring.

Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36
  • Also you do not need \@Configuration annotation when you create controllers. Just use \@Controller – Ayub Malik Jul 29 '13 at 16:01
  • How do I know "RMI packages" as there are no packages for those RMI services as they are invoked remotely ? – TheKojuEffect Jul 29 '13 at 16:57
  • @AyubMalik I think you misunderstood. `@Configuration` is an annotation for defining Java-based spring configuration. And may be you thought it to be `@Component`. And that's basically what I wanna do like, annotating components classes with `@Component` or `@Controller` and use `@ComponentScan` instead of manually defining with `@Bean` for every components. But the problem is that when I do that, `Rmi services` components beans are no longer created or recognized. – TheKojuEffect Jul 29 '13 at 17:01
0

If I understand you correctly, you use "@ComponentScan(basePackageClasses" but it is not detecting and registering your spring beans?

I had the same issue a few minutes ago. I did not give up and tried all funny guesses. One guess did it.

I had to add an XML component-scan entry in XML. I just put a dummy package there, like below:

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

It seems that the component-scan in XML enables the @ComponentScan.

[Late Edit: I noticed, my spring xml schema is spring 2.5. Anyway, I dont know if this matters. Best Regards.]