Spring tutorial example
The Spring Framework consists of features organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming),Instrumentation, and Test.
-The Core Container consists of the Core, Beans, Context, and Expression Language modules.
-The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and Transaction modules.
-The Web layer consists of the Web, Web-Servlet, Web-Struts, and Web-Portlet modules.
-Spring's AOP module provides an AOP Alliance-compliant aspect-oriented programming.
-The Test module supports the testing of Spring components with JUnit or TestNG.
A simple example in spring (3.x)
Create a 'test' package which has two java classes.
Hello.java and HelloTest.java
Required jar files:
commons-logging.jar
org.springframework.beans-3.0.0.M4.jar
org.springframework.core-3.0.0.M4.jar
The directory structure as follows:
Hello.java
package test;
public class Hello {
public void sayHello()
{
System.out.println("Hello S L N V Praveen...!!!");
}
}
HelloTest.java
package test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class HelloTest {
public static void main(String[] args) {
XmlBeanFactory xb = new XmlBeanFactory(new ClassPathResource("spring.xml"));
Hello hl = (Hello)xb.getBean("springHello");
hl.sayHello();
}
}
Now define a spring config. file that will instantiate the Hello bean object and we'll get the instance using getBean() method of AbstractBeanFactory, which returns an object of type Object.
-Object getBean(String name)
-XmlBeanFactory - Create a new XmlBeanFactory with the given resource, which must be parsable using DOM.
-Object getBean(String name)
-XmlBeanFactory - Create a new XmlBeanFactory with the given resource, which must be parsable using DOM.
Now define the spring.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="springHello" class="test.Hello"/>
</beans>
Now run the HelloTest.java....Happy coding!
No comments:
Post a Comment