Spring:正确设置@ComponentScan

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17857770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:06:32  来源:igfitidea点击:

Spring: Properly setup @ComponentScan

springcomponentsrmiapplicationcontextspring-remoting

提问by TheKojuEffect

I have following set up for my Spring Application Context.

我为我的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 @ComponentScanannotating Controllers with @Componentas there are many Controllers in my application which is tedious when declared one by one using @Bean.

上面的设置工作正常,但我想启用@ComponentScan注释Controllers,@Component因为Controller我的应用程序中有很多s,当使用@Bean.


@Configuration
@ComponentScan(basePackageClasses = {Controller.class})
public class LocalContext {
    /* ... */
}
问题是,当我这样做时@ComponentScan(basePackageClasses = {Controller.class})@ComponentScan(basePackageClasses = {Controller.class}),以前的良好工作RmiProxyFactoryBeanRmiProxyFactoryBean无法识别或无法创建。

So, How do I configure my MainContextso that both beans via RMI and local beans are created?

那么,如何配置我的MainContext以便通过 RMI 和本地 bean 创建两个 bean?

回答by Septem

@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 也是组件扫描的候选对象,因此您可以通过以下方式扫描 RmiContext 中的所有 bean 和控制器包中的所有控制器:

@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:

@Configuration 是组件扫描的候选对象,这是在我的电脑上运行的测试用例:

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);
    }
}

回答by Shamim Ahmmed

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

也许你可以尝试使用你的类的基本包(RMI,控制器):


@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.

如果 RMI 类包与控制器不同,那么它们将无法通过 spring 实例化。

回答by Jesus Angeles

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

如果我理解正确,您使用“@ComponentScan(basePackageClasses”但它没有检测和注册您的 spring bean?

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:

我必须在 XML 中添加一个 XML 组件扫描条目。我只是在那里放了一个虚拟包,如下所示:

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

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

XML 中的组件扫描似乎启用了@ComponentScan。

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

[后期编辑:我注意到,我的 spring xml 架构是 spring 2.5。无论如何,我不知道这是否重要。此致。]