Spring Core
Spring Core
Spring Framework:
Core Concepts:
Spring Container
BeanFactory:
ApplicationContext:
• Extends BeanFactory.
• Provides more advanced features.
• Supports:
o Message Resource Handling: For internationalization.
o Event Propagation: For implementing application event handling.
o AOP Integration: Directly integrated with Spring's AOP.
Configuration Example:
java
Copy code
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
beans.xml:
xml
Copy code
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myBean" class="com.example.MyBean" />
</beans>
Dependency Injection
Types of DI:
1. Constructor Injection:
o Dependencies are provided through the constructor of the class.
o Ensures that an object is created with all its dependencies.
java
Copy code
public class MyService {
private final MyRepository repository;
Configuration:
xml
Copy code
<bean id="myService" class="com.example.MyService">
<constructor-arg ref="myRepository" />
</bean>
<bean id="myRepository" class="com.example.MyRepository" />
2. Setter Injection:
o Dependencies are provided through setter methods after the object is
constructed.
o Allows for optional dependencies and changing dependencies.
java
Copy code
public class MyService {
private MyRepository repository;
Configuration:
xml
Copy code
<bean id="myService" class="com.example.MyService">
<property name="repository" ref="myRepository" />
</bean>
<bean id="myRepository" class="com.example.MyRepository" />
java
Copy code
public class MyService {
@Autowired
private MyRepository repository;
Configuration:
xml
Copy code
<context:component-scan base-package="com.example" />
Metadata / Configuration
Configuration Metadata:
• Defines how beans are configured and managed in the Spring container.
• Can be provided in various formats:
o XML Configuration:
▪ Traditional way of configuring Spring beans.
▪ Uses XML files to define beans and their dependencies.
xml
Copy code
<bean id="myBean" class="com.example.MyBean">
<property name="property1" value="value1" />
</bean>
o Java-based Configuration:
▪ Uses @Configuration annotated classes to define beans.
▪ Provides a type-safe, refactoring-friendly way of configuring Spring
beans.
java
Copy code
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
o Annotation-based Configuration:
▪ Uses annotations to define bean dependencies directly in the
component classes.
▪ Annotations like @Component, @Service, @Repository, @Autowired,
and @Qualifier are commonly used.
java
Copy code
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class MyService {
@Autowired
public MyService(MyRepository repository) {
this.repository = repository;
}
Spring Profiles:
• Allows to segregate parts of the application configuration and make it available only
in certain environments.
• Use @Profile to annotate configuration classes or bean definitions.
• Example:
java
Copy code
@Configuration
@Profile("development")
public class DevelopmentConfig {
@Bean
public MyBean myBean() {
return new MyBean("Development Bean");
}
}
Property Source:
java
Copy code
@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {
@Value("${my.property}")
private String myProperty;
@Bean
public MyBean myBean() {
return new MyBean(myProperty);
}
}