multiple packages in context:component-scan, spring config
The following approach is correct:
<context:component-scan base-package="x.y.z.service, x.y.z.controller" />
Note that the error complains about x.y.z.dao.daoservice.LoginDAO
, which is not in the packages mentioned above, perhaps you forgot to add it:
<context:component-scan base-package="x.y.z.service, x.y.z.controller, x.y.z.dao" />
How to scan multiple paths using the @ComponentScan annotation?
@ComponentScan uses string array, like this:
@ComponentScan({"com.my.package.first","com.my.package.second"})
When you provide multiple package names in only one string, Spring interprets this as one package name, and thus can't find it.
Putting packages in context:component-scan base-package=/
You can just comma delimit the package names:
<context component-scan base-package="package1, package2"/>
@ComponentScan with multiple configuration class : Annotation Based Configuration
For your Question 1 -
yes, you can register a bean using @ComponentScan
in any of the configuration bean which is registered in spring container.you can register a bean into container by any of the following way-
- Use
@Configuration
to register bean inrootcontext
ordispatchersevletcontext
. - Import a class in any
@Configuration
bean (which is already registered in container).
Let say- you have MvcConfig
class in which you are scanning component-
@ComponentScan(basePackages = {"xxxx","yyyy","zzzz"})
@Configuration
public class MvcConfig {
....
}
To register MvcConfig
in container you must do-
Either
new AnnotationConfigWebApplicationContext().register(MvcConfig.class);
Or
new AnnotationConfigWebApplicationContext().register(AnotherConfig.class);
@Configuration
@Import({MvcConfig.class})
public class AnotherConfig {
....
}
For your Question 2 -
Here spring is not only registering MyConfiguration.class
but also all the component classes those are present in the package in which MyConfiguration
defined.
Using @ComponentScan or context:component-scan / with only one class
Simply add is as a bean to your context e.g.
<bean class="my.package.MyClass" />
How can I specify into the Spring XML configuration to perform the component-scan on two different packages?
You can specify multiple packages using comma
<context:component-scan base-package="it.mycompmany.myproject.registrazione,it.mycompmany.myproject.login">
</context:component-scan>
Related Topics
What Is the Purpose of the Java Constant Pool
How to Execute Windows Commands Using Java - Change Network Settings
Memory Leak When Redeploying Application in Tomcat
Visual Studio Code, Java Extension, How to Add a Jar to Classpath
How to Get Client Information Such as Os and Browser
Adding 3Rd Party Jars to Web-Inf/Lib Automatically Using Eclipse/Tomcat
How to Load/Reference a File as a File Instance from the Classpath
Should You Always Code to Interfaces in Java
Sessions in Struts2 Application
How to Create a Class Literal of a Known Type: Class<List<String>>
Can Anyone Recommend a Simple Java Web-App Framework
How to Create a Modular Jsf 2.0 Application
Java.Sql.Sqlexception: Incorrect String Value: '\Xf0\X9F\X91\Xbd\Xf0\X9F...'
Spring Crudrepository Findbyinventoryids(List<Long> Inventoryidlist) - Equivalent to in Clause
What Package Naming Convention Do You Use for Personal/Hobby Projects in Java
Generic Wildcard Types Should Not Be Used in Return Parameters