Tags in my blog

19 April 2009

Spring getting started

To start using spring just add this two jars:
- spring.jar
- commons-logging.jar

Put this spring xml bean configuration file with .xml extension. For example beans.xml.
<beans xmlns="http://www.springframework.org/schema/beans"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="beanName" class="path.to.ClassName">
<property name="propName" value="1"/>
<property name="otherPropName" value="2"/>
</bean>
</beans>


Load the xml bean configuration by using this syntax in your java class:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

You can get your object using that context:
ClassName beanName = (ClassName) context.getBean("beanName");

No comments: