Skip to content
Advertisement

Is it necessary to use @Configuration while working with spring annotations

I am working with a simple spring application to check @Configuration and @Bean(java based configuartion only),The program is working with both @Configuration and without it.So is it necessary to have it.

Here is my code,

Student.java

package com.cg.spring;

public class Student {

private int id;
private String name;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Override
public String toString() {
    return "Student [id=" + id + ", name=" + name + "]";
}
    }

Faculty.java

package com.cg.spring;

public class Faculty {

private int empId;
private String name;
public int getEmpId() {
    return empId;
}
public void setEmpId(int empId) {
    this.empId = empId;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Override
public String toString() {
    return "Faculty [empId=" + empId + ", name=" + name + "]";
}
}

MyConfig.java

package com.cg.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

@Bean
public Student stu()
{
     return new Student();
}

@Bean


public Faculty fac()
{
    return new Faculty();
}}

Client.java

package com.cg.spring;

import org.springframework.context.ApplicationContext;
import 
org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Client {

public static void main(String[] args) {

    ApplicationContext context=new 
AnnotationConfigApplicationContext(MyConfig.class);
    Student stu=(Student)context.getBean(Student.class);
    Faculty fac=(Faculty)context.getBean(Faculty.class);
    stu.setName("ajay");
    stu.setId(101);
    System.out.println(stu);

    fac.setEmpId(202);
    fac.setName("Kiran");
    System.out.println(fac);

}}

The output is same with or without the @Configuration

Student [id=101, name=ajay]
Faculty [empId=202, name=Kiran]

Even tried with autowiring,it is also working without @Configuration

Student.java

package com.cg.spring;

import org.springframework.beans.factory.annotation.Autowired;

public class Student {

private int id;
private String name;

@Autowired
private Faculty faculty;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public Faculty getFaculty() {
    return faculty;
}
public void setFaculty(Faculty faculty) {
    this.faculty = faculty;
}
@Override
public String toString() {
    return "Student [id=" + id + ", name=" + name + "]";
}}

Client.java

package com.cg.spring;

import org.springframework.context.ApplicationContext;
import 
org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Client {

public static void main(String[] args) {

    ApplicationContext context=new 
   AnnotationConfigApplicationContext(MyConfig.class);
    Student stu=(Student)context.getBean(Student.class);
    Faculty fac=(Faculty)context.getBean(Faculty.class);
    stu.setName("ajay");
    stu.setId(101);
    System.out.println(stu);

    fac.setEmpId(202);
    fac.setName("Kiran");
    System.out.println(fac);

    stu.setFaculty(fac);
    System.out.println(stu.getFaculty());

}}

Advertisement

Answer

When using Java based configuration with Spring you basically have 2 options (as you already noticed). You have the option to annotate a class with @Configuration and have all the @Bean annotated methods available as beans. However you can also do this without the @Configuration annotation. The latter is called the so called lite mode.

When using @Configuration classes the beans defined in there are regular Spring beans and when calling one method from another this will always result in the same instance of a bean. Spring detects the @Configuration classes and treats them in a very special way (it will create a proxy for those classes).

When using lite-mode the @Bean methods are basically nothing more than factory methods, although they participate in (part of) the lifecycle of Spring Beans. When calling them each call will get you a new bean. Which means that, inter bean dependencies, will get you new instances each time the method gets called.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement