如何在context:component-scan
元素的spring-servlet.xml文件中添加多个包?
我试过了
<context:component-scan base-package="z.y.z.service" base-package="x.y.z.controller" />
Run Code Online (Sandbox Code Playgroud)
和
<context:component-scan base-package="x.y.z.service, x.y.z.controller" />
Run Code Online (Sandbox Code Playgroud)
和
<context:component-scan base-package="x.y.z.service" />
<context:component-scan base-package="x.y.z.controller" />
Run Code Online (Sandbox Code Playgroud)
但得到了错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [x.y.z.dao.daoservice.LoginDAO] found for dependency:
Run Code Online (Sandbox Code Playgroud) 扩展时getServletConfigClasses()
vs 之间有什么区别.我从今天早上开始阅读了很多资料,但我还没有对差异有任何明确的了解:getRootConfigClasses()
AbstractAnnotationConfigDispatcherServletInitializer
请看看这两个配置:
1).
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ConServlet.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
....
....
}
Run Code Online (Sandbox Code Playgroud)
的ConServlet.class
是指的
@EnableWebMvc
@Configuration
@ComponentScan({ "com" })
@Import({ SecurityConfig.class })
public class ConServlet {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
Run Code Online (Sandbox Code Playgroud)
2).
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() …
Run Code Online (Sandbox Code Playgroud) 我正在维护一个包含两组主程序包的项目,该项目使用的是Spring和Spring MVC,其中一个程序包包含多个控制器,并使用XML configuration(<context:component-scan />
)进行扫描.
问题是在另一个包中有一个类(未扫描),我需要扫描这个类,但只需要这个类,而不是包中的其他类.我现在无法改变它的包装,因为它现在风险太大了.
那么有没有办法使用注释或XML 来做到这一点?
我无法从另一个项目自动连接假装客户端.似乎没有生成和注入假装客户端的实现.
这是我得到的错误.
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'passportRestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.wstrater.service.contacts.client.ContactService com.wstrater.service.passport.server.controllers.PassportRestController.contactService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.wstrater.service.contacts.client.ContactService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Run Code Online (Sandbox Code Playgroud)
假装客户非常直截了当.为简洁起见,我删除了导入.
package com.wstrater.service.contacts.client;
@FeignClient("contact-service")
public interface ContactService {
@RequestMapping(method = RequestMethod.GET, value = ContactConstants.CONTACTS_USER_ID_PATH)
public Collection<Contact> contactsByUserId(@PathVariable("userId") String userId);
}
Run Code Online (Sandbox Code Playgroud)
我将组件扫描添加到我的项目中以包含应用程序及其控制器,并将feign客户端包含在另一个项目中.
package com.wstrater.service.passport.server;
@EnableEurekaClient
@EnableFeignClients
@SpringCloudApplication …
Run Code Online (Sandbox Code Playgroud) 我正在努力从我的自定义库中自动装配bean,并使用gradle导入.在阅读了几个类似的主题后,我仍然无法找到解决方案.
我有Spring Boot项目,它依赖于其他项目(我的自定义库包含Components,Repositories等...).该库是一个Spring不可运行的jar,主要由域实体和存储库组成.它没有可运行的Application.class和任何属性......
当我启动应用程序时,我可以看到我的'CustomUserService'bean(来自库)正在尝试初始化,但是在其中自动装配的bean无法加载(接口UserRepository)...
错误:
com.myProject.customLibrary.configuration.CustomUserDetailsService中构造函数的参数0需要一个无法找到的类型为"com.myProject.customLibrary.configuration.UserRepository"的bean.
我甚至试图设置'Order',显式加载它(使用'scanBasePackageClasses'),使用包和标记类扫描,添加额外的'EnableJPARepository'注释但没有任何效果......
代码示例(为简单起见,包名称已更改)
package runnableProject.application;
import runnableProject.application.configuration.ServerConfigurationReference.class
import com.myProject.customLibrary.SharedReference.class
//@SpringBootApplication(scanBasePackages = {"com.myProject.customLibrary", "runnableProject.configuration"})
//@EnableJpaRepositories("com.myProject.customLibrary")
@SpringBootApplication(scanBasePackageClasses = {SharedReference.class, ServerConfigurationReference.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
来自图书馆的课程:
package com.myProject.customLibrary.configuration;
import com.myProject.customLibrary.configuration.UserRepository.class;
@Service
public class CustomUserDetailsService implements UserDetailsService {
private UserRepository userRepository;
@Autowired
public CustomUserDetailsService(UserRepository userRepository) {
this.userRepository = userRepository;
}
...
package myProject.customLibrary.configuration;
@Repository
public interface UserRepository extends CustomRepository<User> {
User findByLoginAndStatus(String var1, Status var2);
...
}
Run Code Online (Sandbox Code Playgroud) multi-module spring-data-jpa spring-ioc spring-boot component-scan
Spring的@ComponentScan提供了一个类型安全的basePackageClasses
属性 - 这似乎是一件好事,特别是因为在我正在处理的项目中重命名包这种情况并不少见.文件说:
考虑在每个包中创建一个特殊的无操作标记类或接口,除了被该属性引用之外没有其它用途.
......但没有提供关于这类课程名称的进一步指导.我想知道是否有任何约定.package-info.java
已经存在于所有包中(由Checkstyle强制执行) - 本来希望重用它,但遗憾的是Java不允许使用这个名称的类.
(如果不存在上述标准在想或许PackageInfo
,BasePackage
,PackageMarker
或类似,但宁愿遵循惯例,如果有一个.)
我遇到了一个奇怪的问题.我的印象是,如果为扫描指定了顶级包,则组件扫描会递归扫描子包.
我的存储库和实体是项目的maven依赖项.它们位于包名com.foo.bar.xyz下,我的应用程序配置在包com.foo.bar下.当我写的时候@ComponentScan(basePackages = "com.foo.bar")
,@EnableJpaRepositories
它会给出一个错误,即找不到存储库bean.
但是,当我指定一个顶级存储库包@EnableJpaRepositories(basePackages = com.foo.bar.xyz)
,以及如上所述的组件扫描时,它会检测到存储库就好了.
现在发生这种情况只是因为存储库和实体被注入maven依赖项?那么组件扫描的递归部分,扫描子包还是子目录?
根据Spring Doc-
配置组件扫描指令以与@Configuration类一起使用.提供与Spring XML
<context:component-scan>
元素并行的支持.
在我的春季Web应用程序中,有多个文件被标记@Configuration
,以便@component
在spring容器中注册bean-
Question1-我们能否用@ComponentScan
在任何的的@Configuration
类 或所有 @Configuration
类?
问题2-
我也在春天看过doc
@Configuration
@ComponentScan(basePackageClasses = { MyConfiguration.class })
public class MyConfiguration extends WebMvcConfigurerAdapter {
...
}
Run Code Online (Sandbox Code Playgroud)
为什么在这里扫描配置类本身.
编辑:基本上我用理解@ComponentScan
是:扫描并注册立体式豆(EX- @componant
,@Controller
,@Services
等..),为什么我们正在注册@Configuration
的Bean.
@ComponentScan
将为您提供@Component
包含(或@Service
/ @Repository
)注释的所有类的列表.为此,我想他们使用反射来枚举包中的所有类,并找到具有该注释的类.
但是,根据其他StackOverflow答案,由于ClassLoader
工作原理,您无法可靠地枚举包中的所有类.那么@ComponentScan
看似如何设法实现这一目标呢?
我想实现类似于Spring Data的东西.
开发人员可以定义一些接口,向接口添加自定义注释以标记它们(我的代码将为接口创建代理实例)并通过@Autowire将它们用于必要的服务.
在spring初始化期间,我需要获取所有接口的列表(正确注释)<为接口创建动态代理并将它们注入到必要的位置.
代理创建,创建bean注入很好.现在的问题是:
如何查找所有接口的列表?
它们可以放在任何包装中(或者甚至放在一个单独的罐子里)并且有任何名称.扫描类路径上存在的所有类需要太多时间.
我找到了问题,但它需要基础包启动.
试过一个基于思考的解决方案,但它再次需要基础包或者从root开始需要非常大的时间来扫描所有可用的类.
Reflections reflections = new Reflections("...");
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(<annotation>);
Run Code Online (Sandbox Code Playgroud)
所以我需要一个完整的基础包列表Spring扫描在包中找到我的接口(必须要快得多).
信息在SpringContext中绝对可用.我试图调试并看看basePackages []是如何初始化的,但是有很多私有类/方法用于初始化,我只是没有看到如何从ApplicationContext正确访问basePackages.
component-scan ×10
spring ×8
java ×7
spring-mvc ×3
spring-boot ×2
annotations ×1
convention ×1
maven ×1
multi-module ×1
reflection ×1
spring-cloud ×1
spring-ioc ×1