Spring Core, Bean configuration and dependency injection with XML
- Create New Java project
- Craete libs directory and add dependent Libery, Spring framework jar
- Copy all dependent Spring jar to the directory
- Add External Jar, Project --> property --> Java Build Path --> Add External Libraries
- Define the dependency interface and class
- Create a constructure in your class for injection
- Configure the dependency injection in Spring configuration file
- Create FortuneService interface
- implement FortuneService interface with HappyFortuneService
- Create Couch interface
- implement Couch interface with
Create constructure dependency injection in HappyFortuneService
public BaseballCoach(FortuneService service) {
fortuneService = service;
}
configure the Bean
<bean id="baseballCouch"
class="com.arifrajib.springcorexml.BaseballCoach">
<!-- Setup Constructure Inhection -->
<constructor-arg ref="myFortune" />
Create application context
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Call the Bean
System.out.println(theCoutch.getDailyFortune());