Spring. Spring Container

 Primary functions:

1. Create and manage objects (Inversion of Control)

2. Inject object's dependencies (Dependency Injection)


Configuring Spring Container

1. XML configuration file 

2. Java Annotations (modern)

3. Java Source Code (modern)


Spring container is generally known as ApplicationContext.


A "Spring Bean" is simply a Java object. When Java objects are created by the Spring Container, 

then Spring refers to them as "Spring Beans".

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<bean id = "myCoach" class="com.example.demo.TrackCoach">
</bean>

</beans>





package com.example.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloSpringApp {
public static void main(String[] args) {

// load the spring configuration file
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

// retrieve bean from spring container
Coach theCoach = context.getBean("myCoach", Coach.class);

// call methods on the bean
System.out.println(theCoach.getDailyWorkout());

// close the context
context.close();
}
}

Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class