使用ComponentScan.basePackageClasses和ComponentScan.basePackages来注册单个Spring webMVC控制器? [英] ComponentScan.basePackageClasses vs ComponentScan.basePackages to register a single Spring webMVC Controller?

查看:1174
本文介绍了使用ComponentScan.basePackageClasses和ComponentScan.basePackages来注册单个Spring webMVC控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Spring WebApplicationContext中添加一个特定的控制器类.我遇到了以下示例:(在Scala中,但从此处改编而成:

I want to add a single specific controller class to my Spring WebApplicationContext. I ran across the following example: (its in Scala, but is adapted from here: using ComponentScan or context:component-scan with only one class)

@Configuration 
@ComponentScan(
  basePackages = Array("com.example.controllers"),
  useDefaultFilters = false,
  includeFilters = Array(
    new ComponentScan.Filter(`type` = FilterType.ASSIGNABLE_TYPE,
      value = Array(classOf[com.example.controllers.MyController]))))
class MyConfig {
}

这很好用(但很冗长). 但是Spring的@ComponentScan也有basePackageClasses

This works nicely (but is very verbose). But Spring's @ComponentScan also has basePackageClasses

@Configuration 
@ComponentScan( basePackageClasses=Array(classOf[com.example.controllers.MyController])) 
class MyConfig {
}

在basePackageClasses中,Spring的文档说:

Of basePackageClasses, Spring's docs says:

Type-safe alternative to basePackages() for specifying the packages to
scan for annotated components.

但是,虽然第一个ComponentScan仅正确添加了com.example.controllers.MyController,但是第二个导致了我所有@Controller的扫描和添加!为什么? basePackageClasses的用途是什么?

However, while the first ComponentScan correctly adds only com.example.controllers.MyController, but the second cause all of my @Controller to be scanned and added! Why? What's the use of basePackageClasses?

示例,例如: https ://github.com/mikaelhg/springmvc-example/blob/master/src/main/java/mikaelhg/example/ExampleConfiguration.java 建议可以使用basePackageClasses加载单个组件.

Example like: https://github.com/mikaelhg/springmvc-example/blob/master/src/main/java/mikaelhg/example/ExampleConfiguration.java suggest that basePackageClasses can be used to load a single component.

更新:

顺便说一句,替换为:

@Configuration 
@ComponentScan(
  basePackages = Array("com.example.controllers"),
  useDefaultFilters = false,
  includeFilters = Array(
    new ComponentScan.Filter(`type` = FilterType.ASSIGNABLE_TYPE,
      value = Array(classOf[com.example.controllers.MyController]))))
class MyConfig {
}

使用

@Configuration 
class MyConfig {

  @Bean
  var myController = new com.example.controllers.MyController

}

当添加到WebApplicationContext中时,MyController似乎永远不会连接到servlet(行为已更改为404-NotFound).

it seems that MyController never gets gets connected to the servlet (the behavior changed to 404-NotFound) -- when added to a WebApplicationContext.

推荐答案

该属性的完整javadoc读取

The full javadoc for that attribute reads

basePackages()的类型安全替代方案,用于指定要打包的软件包 扫描带注释的组件. 指定的每个班级的套餐 将被扫描.

Type-safe alternative to basePackages() for specifying the packages to scan for annotated components. The package of each class specified will be scanned.

考虑在每个中创建一个特殊的无操作标记类或接口 除了被本手册引用之外,没有其他用途的软件包 属性.

Consider creating a special no-op marker class or interface in each package that serves no purpose other than being referenced by this attribute.

通过类型安全,您不能对包名称的String值犯任何错误.如果您指定了不正确的类,则它将在编译时失败.

By type-safe, you can't make any mistakes with the String value of the name of the package. If you specify an incorrect class, it will fail at compile time.

当您指定basePackageClasses时,Spring将扫描您指定的类的包(和 subpackages ).对于没有操作的类/接口(例如ControllersServices等),这是一个很好的技巧.将所有控制器放入一个包含Controllers类的程序包中,并在basePackageClasses中指定您的Controllers类.春天会把它们全部捡起来.

When you specify basePackageClasses, Spring will scan the package (and subpackages) of the classes you specify. This is a nice trick with no-op classes/interfaces like Controllers, Services, etc. Put all your controllers in one package containing the Controllers class and specify your Controllers class in the basePackageClasses. Spring will pick them all up.

您仍然需要指定过滤器.

You still need to specify the filters.

这篇关于使用ComponentScan.basePackageClasses和ComponentScan.basePackages来注册单个Spring webMVC控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆