Skip to content
Advertisement

Are Java classes in Spring automatically a @Bean?

If I create some classes, can I automatically @Autowire those classes somewhere else without using the @Bean, @Component, @Service? Or if I want to @Autowire a class somewhere else, do I need to use the annotations described before?

I want to @Autowire a class to another that uses the @Configuration and @ComponentScan annotation

For example:

public class SomeClass{
    //do Something
}

or

@Bean
public class SomeClass{
     //do Something
}

to achieve this..

@Configuration
@ComponentScan("some.package")
public class SomeOtherClass{

    @Autowire
    private SomeClass someClass;

}

Advertisement

Answer

If i create some class, can i automatically @Autowire that class somewhere else without using the @Bean, @Component, @Service

No

You can read more about creating Spring beans.

Advertisement