What's the difference between @ComponentScan and @Bean in a context configuration?

What's the difference between @ComponentScan and @Bean in a context configuration?

Content Index :

What's the difference between @ComponentScan and @Bean in a context configuration?
Tag : java , By : Daljit Dhadwal
Date : January 11 2021, 03:34 PM

Any of those help It is because @ContextHierarchy will create multiple spring contexts with parent children hierarchy. In your case, TestSecurityContext defines the bean configuration for the parent context while UserInfoServiceTest.Config defines for the children context.
If there is no @ComponentScan on the UserInfoServiceTest.Config , the security related beans are defined in the parent context which are invisible to the UserInfoService bean in the children context , and hence it does not get proxied by Spring Security.
@ContextConfiguration(classes= {TestSecurityContext.class,UserInfoServiceTest.Config.class})
public class UserInfoServiceTest {

    public static class Config {
        public UserInfoService userInfoService() {
            return new UserInfoServiceImpl();

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Spring @Configuration @Bean - How can I get more context information for bean creation?

Tag : java , By : Ernie Thomason
Date : March 29 2020, 07:55 AM
wish of those help The bean you define is a Singleton, so its created at Context-Initialization before the app know that anyone might autowire the value. You must create the Bean as Prototype to request the instance on autowire only.
This is still not possible to get infos about the autowire-target. You can use *Aware-interfaces to get

Does @ComponentScan register discovered @Configuration classes to current application context automatically?

Tag : java , By : hlpimfalling
Date : March 29 2020, 07:55 AM
it fixes the issue Yes. Because with that setting of @ComponentScan initialization will pick up everything from the app package, including FooConfig. Without it, you would need the explicit import.
Alternatively you could provide all config classes directly for the ApplicationContext constructor:
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class, FooConfig.class);

How to proper create bean Spring using annotations Bean and ComponentScan?

Tag : java , By : Sonal
Date : March 29 2020, 07:55 AM
should help you out Main: , You can create a new configuration, let's say SpringConfiguration,
package my.pkg.config;

public class SpringConfiguration {
    public Test test(){
        return new Test();
@ComponentScan(basePackageClasses = {"my.pkg.config", "my.pkg.example"})
public class Application {

  public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
package my.pkg.example;

public class TestExample {

    private Test tst;


Whats the difference between a EJB object and implementation bean?

Tag : java , By : Steve
Date : March 29 2020, 07:55 AM

@SpringBootApplication and @ComponentScan not working together (bean configuration)

Tag : java , By : artifex
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Spring Boot will scan all the packages and sub packages starting with the package that the @SpringBootApplication annotated class is in. Your class is in nl.example.hots.boot scanning only that package. The other classes are in different, non-scanned packages.
Due to this package structure and not following the best practices you basically loose a lot of the auto configuration features (Spring Data JPA, ReST etc) and you have to resort to manually enabling/configuring that. Partially through adding additional @ComponentScan annotation, for JPA the @EntityScan annotations. But you would also need to add all the @EnableJpaRepository etc. annotations as those aren't added anymore (at least not with the right packages).
Related Posts Related QUESTIONS :
  • No ParameterResolver registered for parameter in BeforeAll method
  • Finding the path in a graph with the least casualties according to the lanchester square law
  • MongoWriteException when inserting into Mongodb with composite custom _id
  • Fetch Oracle procedure metadata with Java when multiple procedure signatures
  • Value modification of key-pair in HashMap and impact for a HashCode
  • Migration from solrj to spring-data-solr
  • How to check if you're still connected to the database with jpa
  • Use Date type in the graphql scheme
  • Split and add the string based on length
  • Is "main" method of spring boot application required when deploy as war
  • Getting the average within specific numbers in an array
  • how to use izpack to make my jar application to installer?
  • What is meant by src in Java Eclipse?
  • Create a mirrored linked list in Java
  • Examples of good JPA Java Desktop Application
  • Translate Java to Python -- signing strings with PEM certificate files
  • Algorithm Analysis tool for java
  • Java serial comm API - what does inputstream.read() return if a timeout occurs?
  • How do I make a background thread in Java that allows the main application to exit completely? This works in Linux, but
  • How to add an image dynamically at runtime in java
  • Java App on Mac asking for allow network connections everytime
  • Best actively maintained Java XMPP Library?
  • Multi-Threaded Application - Help with some pseudo code!
  • Scoping a StringBuilder inside a for loop
  • How to specify hash algorithm when updating LDAP via Java?
  • Class not found exception (org.apache.openjpa.enhance.PersistenceCapable) thrown in a client of WLS 10
  • In Java ,where in memory are class functions put?
  • How do I test expectedExceptionsMessageRegExp (exception message) using TestNG?
  • Help In understanding Multi Dimentional Arrays
  • No bean named '...' is defined and Spring @Resource annotation
  • Singleton design pattern vs Singleton beans in Spring container
  • flashvars object was not working in mozilla browser
  • Shell script to import mysql dump file
  • What are the best practices to separate data from users
  • May I use com.google.code prefix for my packages?
  • How to set classpath in manifest file , while creating JAR from eclipse?
  • dealing with voice in java
  • Error: java.security.AccessControlException: Access denied
  • How to verify the root trust of a ceritifcate
  • Searching for a standalone JSP compiler
  • Spring 2.5 managed servlets: howto?
  • iBatis get executed sql
  • GRAPH: find an algorithm to determine the shortest path from one point to another in a rectangular maze?
  • Efficient JSON encoding for data that may be binary, but is often text
  • How to Read Java File Structure using Java?
  • Java-Hibernate-Newbie: How do I acces the values from this list?
  • Smallest executable for Windows
  • Java: matching two different type of array
  • how to accept valid e-mail id in text box?
  • How can I know where a Null Pointer Exception originated?
  • Memory limited image processing in server
  • Java Multiple ResourceBundles
  • How to serialize Java primitives using Jersey REST
  • Casting objects to Integer,string ,
  • Using Java how can I plot the path of a sprite in a straight line?
  • IntelliJ shortcut for inserting a null check?
  • Eclipselink and update trigger on multiple access to the database
  • How to ICMPs and traceroutes in Java
  • "NOT_SUPPORTED_BY_GUI" Exception in JCo
  • Controlling the order of PicoContainer startup
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com