Spring Framework — Spring Core

Inversion Of Control

Dependency Injection

  1. Constructor injection — dependencies are injected when creating the object.
  2. Setter injection — dependencies are injected by calling setter methods of the object.

Spring IOC container

  • Instantiating the bean
  • Wiring the beans together
  • Configuring the beans
  • Managing the bean’s entire life-cycle
  1. BeanFactory container
  2. ApplicationContext container

How to Create a Spring Container?

  1. AnnotationConfigApplicationContext: If we are using Spring in standalone Java applications and using annotations for Configuration, then we can use this to initialize the container and get the bean objects.
  2. ClassPathXmlApplicationContext: If we have spring bean configuration XML file in a standalone application, then we can use this class to load the file and get the container object.
  3. FileSystemXmlApplicationContext: This is similar to ClassPathXmlApplicationContext except that the XML configuration file can be loaded from anywhere in the file system.
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("applicationContext.xml"));

Bean Scope

  • singleton: (Default) Scopes a single bean definition to a single object instance per Spring IOC container.
  • prototype: Scopes a single bean definition to any number of object instances.
  • request: Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
  • session: Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
  • application: Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
  • WebSocket: Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

Annotation-based Container Configuration

Why Spring Configuration with Annotations?

  1. XML configuration can be verbose
  2. Easy to configure your Spring beans with Annotations
  3. Annotations minimize the XML configuration
<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>
<!-- bean definitions go here -->

</beans>
  • @Required
  • @Autowired
  • @Qualifier
  • @Primary
  • @Resource
  • @PostConstruct and @PreDestroy

@Required

@Autowired

class Car {
private Engine engine;

@Autowired
Car(Engine engine) {
this.engine = engine;
}
}
class Car {
private Engine engine;

@Autowired
void setEngine(Engine engine) {
this.engine = engine;
}
}
class Car {
@Autowired
private Engine engine;
}

@Qualifier

@Autowired 
@Qualifier("datasource")
private DataSource datasource;

@Autowired
@Qualifier("datasource1")
private DataSource datasource;

@Resource

public class SimpleMovieLister {

private MovieFinder movieFinder;

@Resource(name="myMovieFinder")
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}

@PostConstruct and @PreDestroy

@PostConstruct
public void init(){
//some initialization work
}
@PreDestroy
Public void cleanUp(){
//some initialization work
}

Java-based Container Configuration

@Configuration

@Bean

@Configuration
public class Application {

@Bean
public CustomerService customerService() {
return new CustomerService();
}

@Bean
public OrderService orderService() {
return new OrderService();
}
}
<beans>
<bean id="customerService" class="com.companyname.projectname.CustomerService"/>
<bean id="orderService" class="com.companyname.projectname.OrderService"/>
</beans>

@Import

@Configuration
class DataBaseConfig{
@Bean
public DataBaseService databaseService(){
return new DataBaseServiceImpl();
}
}

@Configuration
@Import(DataBaseConfig.class)
public class AppConfig {

@Bean
public Controller controller(DataBaseService databaseService){
return new ControllerImpl(databaseService);
}
}

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store