xxxxxxxxxx
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
xxxxxxxxxx
<?xml version = "1.0" encoding = "UTF-8"?>
<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-3.0.xsd">
</beans>
xxxxxxxxxx
// XML configuration is not recommended to use : Obsolete . Only used in legacy applications
// Recommended : Use java based configuration
// PacmanGame.java (Independent class)
public class PacmanGame {
// Add any properties or methods specific to PacmanGame
}
// GameRunner.java (Dependent class)
public class GameRunner {
private PacmanGame game;
public GameRunner(PacmanGame game) { // Dependency
this.game = game;
}
public void run(){System.out.println("Game is running");}
}
// contextConfiguration.xml
<?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 definitions here -->
<!-- Directly creating Bean of type String -->
<bean id="name" class="java.lang.String">
<constructor-arg value="Abir" />
</bean>
<!-- Directly creating Bean of type Integer -->
<bean id="age" class="java.lang.Integer">
<constructor-arg value="30" />
</bean>
<!-- This will do component scan to get -->
<context:component-scan base-package="com.your.package.name"/>
<!-- Creating bean of Independent Class -->
<bean id="game" class="com.your.package.name.PacmanGame" scope="singleton"/>
<!-- Creating bean of Dependent Class that requires bean from Independent class as injection for loading -->
<bean id="gameRunner"
class="com.your.package.name.GameRunner" scope="prototype">
<constructor-arg ref="game" /> <!-- Bean is injected as reference -->
</bean>
</beans>
// MainApplication.java
package com.your.package.name;
public class MainApplication {
public static void main(String[] args) {
// Load the Spring application context from the XML configuration file
ApplicationContext context = new ClassPathXmlApplicationContext("contextConfiguration.xml");
// Retrieve the GameRunner bean from the context by specifying id
//GameRunner gameRunner = context.getBean("gameRunner", GameRunner.class);
//gameRunner.run();
// context.getBean(GameRunner.class).run();
}
}
xxxxxxxxxx
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->
</beans>