Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Autowired and bean declaration in applicationContext.xml

Tags:

spring

Hi I am relatively new to spring. Am using Annotations. My doubt begin I have a class as

public class MyClassA{

@Autowired
private MyClassB variableClassB;

// more code here
.
.
.

in my applicationContext.xml

<context:component-scan base-package="package containing MyClassB" />

My question being do I need to add bean declaration in applicationContext.xml as follows

<bean id="classB" class="com.MyClassB"

or is it sufficient to have @Autowired annotation

like image 921
Aditya Avatar asked Jul 15 '13 06:07

Aditya


1 Answers

No it is not.

If your MyClassB is annotated with annotations like @Component, @Service, @Repository or @Controller the component scan will create a bean for the class in the bean factory.

If you are not using any of these annotations then, you need to create a bean manually as you have given

Ex:

@Component
public class MyClassB{
}
like image 71
Arun P Johny Avatar answered Nov 15 '22 05:11

Arun P Johny