Java Configuration Spring 12
Java Configuration Spring 12
Date: 20/09/2024
UseBeans.java
java
Copy code
package in.scalive.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
// Retrieving the Student bean from the container and invoking the display()
method
s1.display();
// Retrieving the Car bean from the container and invoking the show() method
c1.show();
AppConfig.java
java
Copy code
package in.scalive.conf;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
3
@Configuration
Directory Structure
src
└── in
└── scalive
├── app
│ └── UseBeans.java
├── conf
│ └── AppConfig.java
├── demobeans
│ └── Car.java
└── samplebeans
└── Student.java
How It Works:
1. UseBeans.java: This is your main application class where you initialize the Spring
context (ApplicationContext) with the configuration provided in AppConfig. It
retrieves beans like Student and Car from the container and invokes methods on them.
4
● Spring automatically scans for any classes annotated with @Component (or similar) in
the provided packages, creates instances (beans), and makes them available in the
ApplicationContext.
java
Copy code
○ @Configuration
○ @ComponentScan(basePackages =
{"in.scalive.demobeans","in.scalive.samplebeans"})
○ public class AppConfig {
○ }
@Configuration:
● This annotation tells Spring that this class contains configuration information for
the application. It is similar to an XML configuration file, but in Java.
● By marking this class as @Configuration, Spring knows to process it as a
source of bean definitions.
6
@ComponentScan(basePackages = {"in.scalive.demobeans",
"in.scalive.samplebeans"}):
These methods are part of Spring’s internals when managing the ApplicationContext.
Example:
java
7
Copy code
Summary of Flow:
This entire process is happening automatically due to Spring’s annotations and its
built-in IoC and DI (Dependency Injection) capabilities.
Key Points:
● @Bean Annotation:
annotations.
8
Additional Notes:
framework.
The @Bean annotation is a key feature in the Spring framework used to declare beans in a
configuration class. Let me elaborate on it and clarify the differences between @Bean,
Key Differences:
○ @Bean:
■ For example, you may want to create a bean with specific parameters or
Example:
java
9
Copy code
@Configuration
@Bean
○ @Component:
■ This annotation is used at the class level to indicate that the class is a
the package.
beans.
Example:
java
Copy code
10
@Component
public Car() {
// Constructor logic
2. @ComponentScan:
○ This annotation is used to tell Spring which packages to scan for @Component,
● You use @Bean when you want more control over the instantiation of your bean. This is
○ You need to configure some properties manually before creating the bean.
○ You want to use an external library or class that cannot be annotated with
@Component.
● If you just want Spring to automatically detect beans, you can annotate classes with
Copy code
@Configuration
@ComponentScan(basePackages = {"in.scalive.demobeans"})
@Bean
@Component annotated beans, and the customCar() method explicitly declares a Car
bean.
12