Java & J2EE Page 3 - Introducing the Spring Framework |
Property is the child tag of the bean tag. It is used to provide the details of the property of the bean that needs to be accessed or populated. Property is the member variable of the bean that has been mapped using a bean tag. It is done using the name attribute of the property tag. To access the greeting property of the SimpleInterest bean, the property tag will be: <beans> <bean id=”Hello” class=”org.me.Hello”> <property name=”greeting”> : </property> </bean> <beans> The value tag is the child of property tag. The value to be given to the property is passed as the child node of the value tag. To pass Good Morning to the greeting property as the value, the code will be: <beans> <bean id=”Hello” class=”org.me.Hello”> <property name=”greeting”> <value>Good Morning<value> </property> </bean> <beans> Next, we will see what is required to implement the client. Developing the Client: Clients need to do the following to access the Spring Framework service:
Here are the details. The first step is to instantiate the configuration file as a resource. To access the business methods, first the configuration file needs to be passed to the framework. Spring accepts the configuration file as an instance of the Resource interface’s implementation class. The most commonly used implementation class is ClassPathResource. It is instantiated using the name of the configuration file. For example, to create an instance of ClasspathResource with the filename beans.xml and assign it to the reference of the Resource interface, the statement will be: Resource resource=new ClassPathResource(“beans.xml”); Now we are ready for the second step. To access the beans populated with values, the second step is to get a reference for the BeanFactory interface. The reference would contain the instance of an implementation of the BeanFactory interface. The commonly used implementation is XmlBeanFactory. To get an instance of XmlBeanFactory, the reference of the Resource interface has to be passed to the constructor of XmlBeanFactory. For example, to get an instance of XmlBeanFactory and assign it to a reference of BeanFactory, the statement will be: BeanFactory factory=new XmlBeanFactory(resource); Finally, to call the business methods, the instance of bean needs to be retrieved using the getBean() method of BeanFactory. The method return Object instance needs to be typecast into the POJO class. Once that is done, the business objects can be called. In code, it will be: Hello hello=( Hello)factory.getBean(“Hello”); System.out.println(hello.sayHello()); That completes the steps for using the Spring Framework. Next, let us look at a real world example.
blog comments powered by Disqus |
|
|
|
|
|
|
|