Resource of spring core module Applications are used
1 Spring interface.
2 Spring bean class.
3 Spring- configuration file.
4 Client Applications
All the spring releted jar files put in lib folder directory structure is same as normal java/j2ee applications.
Demo.xml
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="demo" class="s1.DemoInterImpl" > <property name="message"> <value>hello</value> </property> </bean> </beans> |
This example is setter base dependency injection this xml file is spring configuration file we set all those bens in this xml file. Again we create interface and its implantation class.
DemoInterImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package s1; import java.util.*; import java.text.*; public class DemoInterImpl implements DemoInter { private String message; public void setMessage(String s) { message=s; } public String wish(String uname) { String str=""; Calendar c1=Calendar.getInstance(); int h=c1.get(Calendar.HOUR_OF_DAY); if( h < 12) str="Good Morning "; else if( h < 17) str="Good Afternoon "; else str="Good Evening "; return message+" "+str+uname; } public String getDateTime() { DateFormat df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); return df.format(new Date()); } } |
DemoInter.java
1 2 3 4 5 6 7 | package s1; public interface DemoInter { public String wish(String uname); public String getDateTime(); } |
DemoClient.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package s1; import org.springframework.core.io.*; import org.springframework.beans.factory.*; import org.springframework.beans.factory.xml.*; public class DemoClient { public static void main(String args[]) { Resource res=new ClassPathResource("s1/Demo.xml"); BeanFactory factory=new XmlBeanFactory(res); DemoInter d1=(DemoInter)factory.getBean("demo"); System.out.println(d1.wish("anurag")); System.out.println(d1.getDateTime()); } } |
When we compile and run this program this will print massage with username and current date and time.