HelloWorld.java
package com.sdnext.dineshonjava.tutorial;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
SpringTestApp.java
package com.sdnext.dineshonjava.tutorial;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTestApp
{
public static void main(String[] args)
{
//ClassPathXmlApplicationContext is load all beans in the application
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//this step is used to get required bean using getBean() method of the created context
HelloWorld helloWorld= (HelloWorld) context.getBean("helloWorld");
helloWorld.getMessage();
}
}
spring.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="com.sdnext.dineshonjava.tutorial.HelloWorld" id="helloWorld">
<property name="message" value="Hello World!">
</property></bean>
</beans>
After creation of the bean configuration file(Spring.xml), now we put this file into “src” directory of the application(i.e. which is present in classpath). Then configuration file(Spring.xml) is loaded in the ApplicationContext as follows.
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Now suppose we put configuration file(Spring.xml) into the different directory package other than “src” like “com.dineshonjava.sdnext.springConfig” then we have to give classpath of configuration file(Spring.xml) to ApplicationContext as follows:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/dineshonjava/sdnext/springConfig/spring.xml");
We can use wildcard for this as follows:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/dineshonjava/**/springConfig/spring.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/*/**/springConfig/*-spring.xml");
When Spring application gets loaded into the memory, Framework makes use of the above configuration file to create all the beans defined and assign them a unique ID as defined in tag. You can use tag to pass the values of different variables used at the time of object creation.
Now run the program you will get following output on console.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…