1)Coach of base ball will provide the work out time 2)My app can be configured 3)I can add any other coach will provide me the work out time 4)cricket , hockey, any other Class BaseBall Class Coach
Ideal Solution My App BaseballCoach give me a “Coach” object HockeyCoach CricketCoach Object Fa c tory configuration
Spring Container My App BaseballCoach give me a “Coach” object HockeyCoach CricketCoach Object Fa c tory configuration Spring
Spring Container • Primary functions • Create and manage objects (Inversion of Control) • Inject object’s dependencies (Dependency Injection) Object Fa c tory Spring
Configuring Spring Container • XML configuration file (legacy, but most legacy apps still use this) • Java Annotations (modern) • Java Source Code (modern)
Spring Development Process Configure your Spring Beans Create a Spring Container Retrieve Beans from Spring Container Ste p -B y-Ste p
Step 1: Configure your Spring Beans < beans … > < bean id = "myCoach" class = " com.springdemo.BaseballCoach " > </ bean > </ beans > File: applicationContext.xml
Step 2: Create a Spring Container Spring container is generically known as ApplicationContext Specialized implementations ClassPathXmlApplicationContext AnnotationConfigApplicationContext GenericWebApplicationContext others … Object Fa c tory Spring
Step 2: Create a Spring Container ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" );
My App BaseballCoach give me a “Coach” object HockeyCoach CricketCoach Object Fa c tory configuration Step 3: Retrieve Beans from Container Spring
Step 3: Retrieve Beans from Container // create a spring container ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); // retrieve bean from spring container Coach theCoach = context .getBean( "myCoach" , Coach. class ); File: applicationContext.xml < bean id = "myCoach" class = " com.springdemo.BaseballCoach " > </ bean >
What is a Spring Bean? 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". Spring Beans are created from normal Java classes .... just like Java objects. File: applicationContext.xml < bean id = "myCoach" class = " com.springdemo.BaseballCoach " > </ bean >