1. Write Java Bean classes, with get and set methods for each property to be configured by Spring.
These can optionally implement interfaces.
2. Create an XML file the describes the Java Beans to be configured.
Beans can also be described using a Java property file, but using XML is more common.
3. Use an XMLBeanFactory to create configured beans.
There are other ways to read bean descriptions, but using an XmlBeanFactory is more common.
Basic layout of a Spring XML configuration file :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean ...>...</bean> +
</beans>
XMLBeanFactory validates the XML, so the DOCTYPE is required.
Then we can use it and get bean object :
BeanFactory beanFactory =
new XmlBeanFactory(new FileSystemResource("spring-beans.xml"));
// Also consider using Class path resource
MyBeanInterface bean = (MyBeanInterface) beanFactory.getBean("myBeanId");
No comments:
Post a Comment
Note: only a member of this blog may post a comment.