Spring Hello World Example Basic Example
Student.java
Student.java
package com.spring; public class Student { private String name; private String Address; private String Course; private int Age; public String getCourse() { return Course; } public void setCourse(String course) { Course = course; } public int getAge() { return Age; } public void setAge(int age) { Age = age; } public String getAddress() { return Address; } public void setAddress(String address) { Address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void displayInfo(){ System.out.println("Hello: "+name); System.out.println("UR Adress :"+Address); System.out.println("UR Course :"+Course); System.out.println("UR Age :"+Age); } }
MyBeans.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <bean id="sb" class="com.spring.Student"> <property name="name" value="Naresh Nalla"></property> <property name="Address" value="Metpally"></property> <property name="Course" value="Mtech CSE"></property> <property name="Age" value="24"></property> </bean> </beans>
TestCase.java
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import com.spring.Student; public class TestCase { public static void main(String[] args) { Resource resource=new ClassPathResource("MyBeans.xml"); BeanFactory beans=new XmlBeanFactory(resource); Student student=(Student)beans.getBean("sb"); student.displayInfo(); } }
Folder Structure to run:
No comments :
Post a Comment