1. Prerequisites
2. Spring Bean Demo
package com.company.demo; public class Developer { private String name; private int experienceYears; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getExperienceYears() { return experienceYears; } public void setExperienceYears(int experienceYears) { this.experienceYears = experienceYears; } } |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans> |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="developer" class="com.company.demo.Developer"> <property name="name" value="munklsnim"></property> <property name="experienceYears" value="5"></property> </bean> </beans> |
package com.company.demo; public class SoftwareCompany { private Developer leadDeveloper; private Developer developer; public Developer getLeadDeveloper() { return leadDeveloper; } public void setLeadDeveloper(Developer leadDeveloper) { this.leadDeveloper = leadDeveloper; } public Developer getDeveloper() { return developer; } public void setDeveloper(Developer developer) { this.developer = developer; } } |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="developer" class="com.company.demo.Developer"> <property name="name" value="munklsnim"></property> <property name="experienceYears" value="5"></property> </bean> <bean id="softwareCompany" class="com.company.demo.SoftwareCompany"> <property name="leadDeveloper"> <ref bean="developer"></ref> </property> <property name="developer"> <bean class="com.company.demo.Developer"> <property name="name"> <value>Venkat</value> </property> <property name="experienceYears"> <value>2</value> </property> </bean> </property> </bean> </beans> |
/** * */ package com.company.demo; import org.apache.log4j.Logger; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class DemoApp { private static final Logger logger = Logger.getLogger(DemoApp.class); /** * @param args */ public static void main(String[] args) { BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource( "demo-beans.xml")); SoftwareCompany company = (SoftwareCompany) beanFactory .getBean("softwareCompany"); logger.info("Lead Developer: " + company.getLeadDeveloper().getName()); logger.info("Developer : " + company.getDeveloper().getName()); } } |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd" "http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.SimpleLayout" /> </appender> <root> <level value="info" /> <appender-ref ref="console" /> </root> </log4j:configuration> |
Output
INFO - Loading XML bean definitions from class path resource [demo-beans.xml]
INFO - Lead Developer: munklsnim
INFO - Developer : Venkat
References
1. http://www.javabeat.net/articles/print.php?article_id=35
2. http://springframework.org/
'Programming > SpringFrameWork' 카테고리의 다른 글
[펌]Spring Framework 따라하기 [1] - 환경 설정 (0) | 2008.11.07 |
---|---|
[펌]Spring Framework v2.0 - Jar Files (0) | 2008.11.07 |
[펌]Spring Framework v2.0? (0) | 2008.11.07 |
[펌]Spring 플러그인 설치(이클립스) (0) | 2008.11.06 |
스프링관련글 사이트 (0) | 2008.11.05 |