问题描述
我为我的 Spring Application Context
设置了以下设置.
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 {
}
上述设置工作正常,但我想启用 @ComponentScan
用 @Component
注释 Controller
,因为有很多 控制器
s 在我的应用程序中使用 @Bean
一一声明时很乏味.
The above setup works fine, but I want to enable @ComponentScan
annotating Controller
s with @Component
as there are many Controller
s in my application which is tedious when declared one by one using @Bean
.
@Configuration
@ComponentScan(basePackageClasses = {Controller.class})
public class LocalContext {
/* ... */
}
问题是当我执行 @ComponentScan(basePackageClasses = {Controller.class})
时,之前正常工作的 RmiProxyFactoryBean
无法识别或无法创建.
The problem is that when I do @ComponentScan(basePackageClasses = {Controller.class})
, the previously fine working RmiProxyFactoryBean
are not recognized or can't be created.
那么,如何配置我的 MainContext
以便通过 RMI 和本地 bean 创建 bean?
So, How do I configure my MainContext
so that both beans via RMI and local beans are created?
推荐答案
@Configuration 也是组件扫描的候选者,因此您可以通过以下方式扫描 RmiContext 中的所有 bean 以及控制器包中的所有控制器:
@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 {
}
--编辑--
@Configuration 是组件扫描的候选者,这是在我的电脑上工作的测试用例:
@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);
}
}
这篇关于Spring:正确设置@ComponentScan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!