What Is Spring
What Is Spring
JDBC Exception Handling: The JDBC abstraction layer of the Spring offers a
meaningful exception hierarchy, which simplifies the error handling strategy
MVC Framework: Spring comes with MVC web application framework, built
on core Spring functionality. This framework is highly configurable via strategy
interfaces, and accommodates multiple view technologies like JSP, Velocity,
Tiles, iText, and POI. But other frameworks can be easily used instead of Spring
MVC Framework..
Spring Architecture
1. Spring AOP
One of the key components of Spring is the AOP framework. AOP is used in
Spring:
To provide declarative enterprise services, especially as a replacement for
EJB declarative services. The most important such service is declarative
transaction management, which builds on Spring's transaction abstraction.
To allow users to implement custom aspects, complementing their use of
OOP with AOP
2. Spring ORM
The ORM package is related to the database access. It provides integration layers
for popular object-relational mapping APIs, including JDO, Hibernate and iBatis.
3. Spring Web
The Spring Web module is part of Spring’s web application development stack,
which includes Spring MVC.
4. Spring DAO
The DAO (Data Access Object) support in Spring is primarily for standardizing
the data access work using the technologies like JDBC, Hibernate or JDO.
5. Spring Context
This package builds on the beans package to add support for message sources and
for the Observer design pattern, and the ability for application objects to obtain
resources using a consistent API.
7. Spring Core
The Core package is the most import component of the Spring Framework.
This component provides the Dependency Injection features. The BeanFactory
provides a factory pattern which separates the dependencies like initialization,
creation and access of the objects from your actual program logic.
Spring 2.5.1 is one the most advance light weight framework for develop enterprise
applications. The most exiting feature is Custom components. Spring ships with few
namespaces such as jee, aop, util, and etc, now you can write your own custom
namespaces.
In the next section we will extract the download and explore the different directories of
the downloaded file.
Spring Framework Install
Spring Framework Install - a quick tutorial to install Spring Framework on your
development environment. In the last section we downloaded the latest version of Spring
framework (spring-framework-2.5.1-with-dependencies.zip). In this section we will
extract the downloaded file and pick the required libraries and then install in our
development environment.
Once extract go to the extracted folder and you will find many files.
build.bat
build.xml
build-continuous.xml
changelog.txt
license.txt
maven.xml
notice.txt
project.properties
readme.txt
tests.bat
spring-aop.jar
spring-beans.jar
spring-context.jar
spring-context-support.jar
spring-core.jar
spring-jdbc.jar
spring-jms.jar
spring-orm.jar
spring-test.jar
spring-tx.jar
spring-web.jar
spring-webmvc.jar
spring-webmvc-portlet.jar
spring-webmvc-struts.jar
We will be using these libraries as per our requirement in our application.
Another most important directory is "lib", which contains required library files for the
application.
The following libraries are included in the Spring Framework distribution because they
are required either for building the framework or for running the sample apps. Note that
each of these libraries is subject to the respective license; check the respective project
distribution/website before using any of them in your own applications.
* antlr/antlr-2.7.6.jar
- ANTLR 2.7.6 (http://www.antlr.org)
- required for running PetClinic (by Hibernate)
* aopalliance/aopalliance.jar
- AOP Alliance 1.0 (http://aopalliance.sourceforge.net)
- required for building the framework
- included in spring.jar and spring-aop.jar
NOTE: aspectjtools.jar is not included in the Spring distribution because of its size.
It needs to be taken from the AspectJ distribution or from Spring CVS. Note that this
is only necessary if you want to rebuild the Spring jars including the AspectJ aspects.
* axis/axis.jar, axis/wsdl4j.jar
- Apache Axis 1.4 (http://ws.apache.org/axis)
- required for building the framework
- required for running JPetStore
* bsh/bsh-2.0b4.jar
- BeanShell 2.0 beta 4 (http://www.beanshell.org)
- required for building the framework
- required at runtime when using Spring's BeanShell support
* bnd/bnd.jar
- BND tool 0.0.208 (http://www.aqute.biz/Code/Bnd)
- required for adding OSGi entries to the jar manifests at build time
* c3p0/c3p0-0.9.1.2.jar
- C3P0 0.9.1.2 connection pool (http://sourceforge.net/projects/c3p0)
- required for building the framework
- required at runtime when using Spring's C3P0NativeJdbcExtractor
- required for running Image Database
* caucho/hessian-3.1.3.jar
- Hessian/Burlap 3.1.3 (http://www.caucho.com/hessian)
- required for building the framework
- required at runtime when using Spring's Hessian/Burlap remoting support
* cglib/cglib-nodep-2.1_3.jar
- CGLIB 2.1_3 with ObjectWeb ASM 1.5.3 (http://cglib.sourceforge.net)
- required for building the framework
- required at runtime when proxying full target classes via Spring AOP
* commonj/commonj-twm.jar
- CommonJ TimerManager and WorkManager API 1.1
(http://dev2dev.bea.com/wlplatform/commonj/twm.html)
- required for building the framework
- required at runtime when using Spring's CommonJ support
* concurrent/backport-util-concurrent.jar
- Dawid Kurzyniec's JSR-166 backport, version 3.0
(http://dcl.mathcs.emory.edu/util/backport-util-concurrent)
- required for building the framework
- required at runtime when using Spring's backport-concurrent support
* dom4j/dom4j-1.6.1, dom4j/jaxen-1.1-beta-7.jar
- DOM4J 1.6.1 XML parser (http://www.dom4j.org)
- required for running PetClinic (by Hibernate)
* easymock/easymock.jar, easymock/easymockclassextension.jar
- EasyMock 1.2 (JDK 1.3 version) (http://www.easymock.org)
- required for building and running the framework's test suite
* ehcache/ehcache-1.3.0.jar
- EHCache 1.3.0 (http://ehcache.sourceforge.net)
- required for building the framework
- required at runtime when using Spring's EHCache support
- required for running PetClinic (by Hibernate)
* freemarker/freemarker.jar
- FreeMarker 2.3.11 (http://www.freemarker.org)
- required for building the framework
- required at runtime when using Spring's FreeMarker support
* glassfish/glassfish-clapi.jar
- GlassFish ClassLoader API extract (http://glassfish.dev.java.net)
- required for building the framework
* groovy/groovy-1.5.1.jar
- Groovy 1.5.1 (http://groovy.codehaus.org)
- required for building the framework
- required at runtime when using Spring's Groovy support
* hibernate/hibernate3.jar
- Hibernate 3.2.5 (http://www.hibernate.org)
- required for building the framework
- required at runtime when using Spring's Hibernate support
* hibernate/hibernate-annotation.jar
- Hibernate Annotations 3.3.0 (http://www.hibernate.org)
- required for building the "tiger" part of the framework
- required at runtime when using Spring's Hibernate Annotations support
* hibernate/hibernate-entitymanager.jar
- Hibernate EntityManager 3.3.1 (http://www.hibernate.org)
- required for building the "tiger" part of the framework
- required at runtime when using Spring's Hibernate-specific JPA support
* hibernate/jboss-archive-browsing.jar
- JBoss Archive Browsing 5.0.0 alpha
- required at runtime when using Hibernate EntityManager
* hsqldb/hsqldb.jar
- HSQLDB 1.8.0.1 (http://hsqldb.sourceforge.net)
- required for running JPetStore and PetClinic
* ibatis/ibatis-2.3.0.677.jar
- iBATIS SQL Maps 2.3.0 b677 (http://ibatis.apache.org)
- required for building the framework
- required at runtime when using Spring's iBATIS SQL Maps 2.x support
* itext/iText-2.0.7.jar
- iText PDF 2.0.7 (http://www.lowagie.com/itext)
- required for building the framework
- required at runtime when using Spring's AbstractPdfView
* j2ee/activation.jar
- JavaBeans Activation Framework 1.1
(http://java.sun.com/products/javabeans/glasgow/jaf.html)
- required at runtime when using Spring's JavaMailSender on JDK < 1.6
* j2ee/common-annotations.jar
- JSR-250 Common Annotations (http://jcp.org/en/jsr/detail?id=250)
- required at runtime when using Spring's Common Annotations support on JDK < 1.6
* j2ee/connector.jar
- J2EE Connector Architecture 1.5 (http://java.sun.com/j2ee/connector)
- required for building the framework
* j2ee/ejb-api.jar
- Enterprise JavaBeans API 3.0 (http://java.sun.com/products/ejb)
- required for building the framework
- required at runtime when using Spring's EJB support
* j2ee/el-api.jar
- JSP 2.1's EL API (http://java.sun.com/products/jsp), as used by JSF 1.2
- required for building the framework
- required at runtime when using Spring's JSF 1.2 support
* j2ee/jaxrpc.jar
- JAX-RPC API 1.1 (http://java.sun.com/xml/jaxrpc)
- required for building the framework
- required at runtime when using Spring's JAX-RPC support
* j2ee/jms.jar
- Java Message Service API 1.1 (java.sun.com/products/jms)
- required for building the framework
- required at runtime when using Spring's JMS support
* j2ee/jsf-api.jar
- JSF API 1.1 (http://java.sun.com/j2ee/javaserverfaces)
- required for building the framework
- required at runtime when using Spring's JSF support
* j2ee/jsp-api.jar
- JSP API 2.0 (http://java.sun.com/products/jsp)
- required for building the framework
- required at runtime when using Spring's JSP support
* j2ee/jstl.jar
- JSP Standard Tag Library API 1.1 (http://java.sun.com/products/jstl)
- required for building the framework
- required at runtime when using Spring's JstlView
* j2ee/jta.jar
- Java Transaction API 1.1 (http://java.sun.com/products/jta)
- required for building the framework
- required at runtime when using Spring's JtaTransactionManager
* j2ee/mail.jar
- JavaMail 1.4 (http://java.sun.com/products/javamail)
- required for building the framework
- required at runtime when using Spring's JavaMailSender
* j2ee/persistence.jar
- Java Persistence API 1.0 (http://www.oracle.com/technology/products/ias/toplink/jpa)
- required for building the framework
- required at runtime when using Spring's JPA support
* j2ee/rowset.jar
- JDBC RowSet Implementations 1.0.1 (http://java.sun.com/products/jdbc)
- required at runtime when using Spring's RowSet support on JDK < 1.5
* j2ee/servlet-api.jar
- Servlet API 2.4 (http://java.sun.com/products/servlet)
- required for building the framework
- required at runtime when using Spring's web support
* jakarta-commons/commons-attributes-api.jar, jakarta-commons/commons-attributes-
compiler.jar
- Commons Attributes 2.2 (http://jakarta.apache.org/commons/attributes)
- commons-attributes-api.jar has a patched manifest (not declaring QDox and Ant as
required extensions)
- required for building the framework
- required at runtime when using Spring's Commons Attributes support
* jakarta-commons/commons-beanutils.jar
- Commons BeanUtils 1.7 (http://jakarta.apache.org/commons/beanutils)
- required for running JPetStore's Struts web tier
* jakarta-commons/commons-collections.jar
- Commons Collections 3.2 (http://jakarta.apache.org/commons/collections)
- required for building the framework
- required for running PetClinic, JPetStore (by Commons DBCP, Hibernate)
* jakarta-commons/commons-dbcp.jar
- Commons DBCP 1.2.2 (http://jakarta.apache.org/commons/dbcp)
- required for building the framework
- required at runtime when using Spring's CommonsDbcpNativeJdbcExtractor
- required for running JPetStore
* jakarta-commons/commons-digester.jar
- Commons Digester 1.6 (http://jakarta.apache.org/commons/digester)
- required for running JPetStore's Struts web tier
* jakarta-commons/commons-discovery.jar
- Commons Discovery 0.2 (http://jakarta.apache.org/commons/discovery)
- required for running JPetStore (by Axis)
* jakarta-commons/commons-fileupload.jar
- Commons FileUpload 1.2 (http://jakarta.apache.org/commons/fileupload)
- required for building the framework
- required at runtime when using Spring's CommonsMultipartResolver
* jakarta-commons/commons-httpclient.jar
- Commons HttpClient 3.1 (http://hc.apache.org/httpclient-3.x)
- required for building the framework
- required at runtime when using Spring's CommonsHttpInvokerRequestExecutor
* jakarta-commons/commons-io.jar
- Commons IO 1.3.1 (http://jakarta.apache.org/commons/io)
- required at runtime when using Spring's CommonsMultipartResolver (by Commons
FileUpload)
* jakarta-commons/commons-lang.jar
- Commons Lang 2.2 (http://jakarta.apache.org/commons/lang)
- required at runtime when using Spring's OpenJPA support (by OpenJPA)
* jakarta-commons/commons-logging.jar
- Commons Logging 1.1 (http://jakarta.apache.org/commons/logging)
- required for building the framework
- required at runtime, as Spring uses it for all logging
* jakarta-commons/commons-pool.jar
- Commons Pool 1.3 (http://jakarta.apache.org/commons/pool)
- required for running JPetStore and Image Database (by Commons DBCP)
* jakarta-commons/commons-validator.jar
- Commons Validator 1.1.4 (http://jakarta.apache.org/commons/validator)
- required for running JPetStore's Struts web tier on servers that eagerly load tag libraries
(e.g. Resin)
* jakarta-taglibs/standard.jar
- Jakarta's JSTL implementation 1.1.2 (http://jakarta.apache.org/taglibs)
- required for running JPetStore, PetClinic, Countries
* jamon/jamon-2.4.jar
- JAMon API (Java Application Monitor) 2.4 (http://www.jamonapi.com)
- required for building the framework
- required at runtime when using Spring's JamonPerformanceMonitorInterceptor
* jarjar/jarjar.jar
- Jar Jar Links 1.0 RC5 (http://code.google.com/p/jarjar)
- required for building the framework jars
* jasperreports/jasperreports-2.0.2.jar
- JasperReports 2.0.2 (http://jasperreports.sourceforge.net)
- required for building the framework
- required at runtime when using Spring's JasperReports support
* jdo/jdo2-api.jar
- JDO API 2.0 (http://db.apache.org/jdo)
- required for building the framework
- required at runtime when using Spring's JDO support (or jdo.jar for JDO 1.0)
* jexcelapi/jxl.jar
- JExcelApi 2.6.6 (http://jexcelapi.sourceforge.net)
- required for building the framework
- required at runtime when using Spring's AbstractJExcelView
* jmx/jmxri.jar
- JMX 1.2.1 reference implementation
- required at runtime when using Spring's JMX support on JDK < 1.5
* jmx/jmxremote.jar
- JMX Remote API 1.0.1 reference implementation
- required at runtime when using Spring's JMX support on JDK < 1.5
* jmx/jmxremote_optional.jar
- JMXMP connector (from JMX Remote API 1.0.1 reference implementation)
- required at runtime when using the JMXMP connector (even on JDK 1.5)
* jotm/jotm.jar
- JOTM 2.0.10 (http://jotm.objectweb.org)
- required for building the framework
- required at runtime when using Spring's JotmFactoryBean
* jotm/xapool.jar
- XAPool 1.5.0 (http://xapool.experlog.com, also included in JOTM)
- required for building the framework
- required at runtime when using Spring's XAPoolNativeJdbcExtractor
* jruby/jruby.jar
- JRuby 1.0.1 (http://jruby.codehaus.org)
- required for building the framework
- required at runtime when using Spring's JRuby support
* junit/junit-3.8.2.jar, junit/junit-4.4.jar
- JUnit 3.8.2 / 4.4 (http://www.junit.org)
- required for building and running the framework's test suite
* log4j/log4j-1.2.14.jar
- Log4J 1.2.14 (http://logging.apache.org/log4j)
- required for building the framework
- required at runtime when using Spring's Log4jConfigurer
* oc4j/oc4j-clapi.jar
- Oracle OC4J 10.1.3.1 ClassLoader API extract
(http://www.oracle.com/technology/tech/java/oc4j)
- required for building the framework
* openjpa/openjpa-1.0.1.jar
- OpenJPA 1.0.1 (http://openjpa.apache.org)
- required for building the framework
- required at runtime when using Spring's JPA support with OpenJPA as provider
* poi/poi-3.0.1.jar
- Apache POI 3.0.1 (http://jakarta.apache.org/poi)
- required for building the framework
- required at runtime when using Spring's AbstractExcelView
* portlet/portlet-api.jar
- Portlet API 1.0 (http://jcp.org/aboutJava/communityprocess/final/jsr168)
- required for building the framework
- required at runtime when using Spring's Portlet support
* qdox/qdox-1.5.jar
- QDox 1.5 (http://qdox.codehaus.org)
- used by Commons Attributes 2.2 to parse source-level metadata in the build process
- required for building the framework and the attributes version of JPetStore
* quartz/quartz-all-1.6.0.jar
- Quartz 1.6.0 (http://www.opensymphony.com/quartz)
- required for building the framework
- required at runtime when using Spring's Quartz scheduling support
* serp/serp-1.13.1.jar
- Serp 1.13.1 (http://serp.sourceforge.net)
- required at runtime when using OpenJPA
* struts/struts.jar
- Apache Struts 1.2.9 (http://jakarta.apache.org/struts)
- required for building the framework
- required at runtime when using the Struts 1.x support or Tiles 1.x TilesView
- required for running JPetStore's Struts web tier
* testng/testng-5.5-jdk15.jar
- TestNG 5.5 (http://testng.org)
- required for building and running the framework's test suite
* tomcat/catalina.jar, tomcat/naming-resources.jar
- Apache Tomcat 5.5.23 (http://tomcat.apache.org)
- required for building the Tomcat-specific weaver
* toplink/toplink-api.jar
- Oracle TopLink 10.1.3 API (http://www.oracle.com/technology/products/ias/toplink)
- required for building the framework
- replaced with full toplink.jar at runtime when using Spring's TopLink support
* toplink/toplink-essentials.jar
- Oracle TopLink Essentials v2 b41
(http://www.oracle.com/technology/products/ias/toplink/jpa)
- required for building the framework
- required at runtime when using Spring's JPA support with TopLink as provider
* velocity/velocity-1.5.jar
- Velocity 1.5 (http://jakarta.apache.org/velocity)
- required for building the framework
- required at runtime when using Spring's VelocityView
* velocity/velocity-tools-view-1.4.jar
- Velocity Tools 1.4 (http://jakarta.apache.org/velocity/tools)
- required for building the framework
- required at runtime when using VelocityView's support for Velocity Tools
You can use above libraries in your application as per your requirement.
In the next section I will show you how to setup development environment step-by-step.
<bean id="mybean" class="Inject">:- Here "Inject" is the name of the bean class
which would be referred in the xml file with the id "mybean".
p:name="Girish" :-Here we simply use the property name in the "p" namespace, as in
"p:name".
context.xml
Main.java
This is the file through which we are retrieving the property of the bean defined above.
Some of the methods which are used here are as follows:-
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.
xml")):-This method creates an instance of the XmlBeanFactory which is used to read
bean definition from an XML document
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
class Inject {
@Override
public String toString() {
return String.format("Name: %s\n" +
"Age: %d\n" +
"Address: %s\n" +
"Company: %s\n" +
"E-mail: %s",
this.name, this.age, this.address, this.company, this.email);
}
}
context.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:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.10.75:3306/komal"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="lobHandler"
class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>
<bean id="nativeJdbcExtractor"
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
Main.java
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
class Main {
public static void main(String args[]) {
try {
ApplicationContext ac = new
ClassPathXmlApplicationContext("context.xml",
Main.class);
DataSource source = (DataSource)
ac.getBean("dataSource");
JdbcTemplate jt = new JdbcTemplate(source);
jt.batchUpdate(new String[]{"update employee set
departement = 'Finance#'",
"delete from employee where EmployeeId =31"
});
System.out.println("Data updated successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
parent="parent":-Specify that this bean is inheriting the properties of some other bean.
</bean>
<bean id="child" class="mybean" parent="parent">
<property name="address" value="Rohini"/>
</bean>
context.xml
</beans>
Main.java
This is the file through which we are retrieving the property of the bean defined above.
Some of the methods which are used here are as follows:-
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
}
}
class mybean {
private String name;
private String address;
@Override
public String toString() {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Bean");
stringBuilder.append("{name='").append(name).append('\'');
stringBuilder.append(", address=").append(address);
stringBuilder.append('}');
return stringBuilder.toString();
}
}
For that we have created a file called "applicationContext.xml", which defines the bean
name, their properties as well as their values.
Required fields:-
1)class name: A class name is the implementation of the bean class that is described in
the bean definition.
2)bean behavioral configuration elements: Here we define the bean behavior that
is bean initialization, dependency checking mode, destruction methods etc.
3)constructor arguments and property values :-Defines the no of arguments and the
property values that are to be set in the newly created bean.
Note:- We have not defined any of these behavior in our context file since we haven't
created any of the bean to be used.
dispatcher-servlet. xml:-This is the file from which the mapping from actions to
controllers is done .We have used ControllerClassNameHandlerMapping and
SimpleUrlHandlerMapping classes to do such mapping.
ControllerClassNameHandlerMapping class generate URL path mappings from the class
names of the Controller.SimpleUrlHandlerMapping maps URLs to the request handler
beans.
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.web.servlet.mvc.support.Controller
ClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handle
r.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.htm">indexController
</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceV
iewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableVie
wController"
p:viewName="index" />
</beans>
redirect.jsp:-
<% response.sendRedirect("index.htm"); %>:-This is a method of Interface
HttpServletResponse that is used to sends a temporary redirect response to the client
using the specified redirect location.
<body>
<h2>Hello World</h2>
</body>
</html>
<constructor-arg>
<util:map>
<entry key="CompanyName" value="Roseindia.net"/>
<entry key="Address" value="Rohini"/>
</util:map>
</constructor-arg>
context.xml
<bean id="encyclopedia"
name="mybean"
class="Configure">
<constructor-arg>
<util:map>
<entry key="CompanyName" value="Roseindia.net"/>
<entry key="Address" value="Rohini"/>
</util:map>
</constructor-arg>
</bean>
Here is the file named Main.java through which we are retrieving the properties of the
bean which we have defined in the above file i.e. context.xml
Main.java
import java.util.Map;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;
interface company {
String Name();
String address();
}
interface Detail {
<property name="name">
<value>Roseindia.net</value>
</property>
<property name="address">
<value>Rohini</value>
</property>
<property name="type">
<value>Software Development Company</value>
</property>
</bean>
initMethod.xml
Here is the file named SimpleBean.java through which we are retrieving the properties
of the bean which we have defined in the above file i.e. initMethod.xml.
SimpleBean.java
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
if (name == null) {
System.out.println("Using default name");
name = DEFAULT_NAME;
}
if (address == null) {
System.out.println("Using default name");
address = DEFAULT_NAME;
}
if (type == null) {
System.out.println("Using default name");
type = DEFAULT_NAME;
}
}
public String toString() {
return "Name: " + name + "\nAddress: " + address + "\nType: " + type ;
}
Initializing bean
Name: Roseindia.net
Address: Rohini
Type: Software
Development Company
context.xml
Here is the file named Main.java through which we are retrieving the properties of the
bean which we have defined in the above file i.e. context.xml
@Override
public String toString() {
return String.format("%s : \"%s\"", this.company, getValue());
Here the method toString() is overridden which is returning the the company name and
value that has been defined in the context.xml file.
Main.java
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
System.out.println(factory.getBean("Mybean"));
}
}
class Bean extends BeanSupport {