Skip to content

Instantly share code, notes, and snippets.

@max747
Created June 2, 2011 00:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save max747/1003666 to your computer and use it in GitHub Desktop.
Save max747/1003666 to your computer and use it in GitHub Desktop.
Using list, map, ... with annotated Spring bean
package sandbox.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
SampleBean sample = context.getBean(SampleBean.class);
sample.showFruits();
sample.showPrefs();
sample.showTimetable();
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan base-package="sandbox.spring" />
<bean id="fruitsList"
class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
<list value-type="java.lang.String">
<value>apple</value>
<value>banana</value>
<value>orange</value>
</list>
</property>
</bean>
<util:list id="prefsList" value-type="java.lang.String">
<value>hokkaido</value>
<value>aomori</value>
<value>akita</value>
</util:list>
<bean id="timetable"
class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
<map key-type="java.lang.String" value-type="java.lang.String">
<entry key="1st" value="Math" />
<entry key="2nd" value="Science" />
<entry key="3rd" value="Reading" />
<entry key="4th" value="Social Studies" />
</map>
</property>
</bean>
</beans>
2011/06/02 9:15:09 org.springframework.context.support.AbstractApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@f81843: startup date [Thu Jun 02 09:15:09 JST 2011]; root of context hierarchy
2011/06/02 9:15:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [applicationContext.xml]
2011/06/02 9:15:10 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
情報: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@62937c: defining beans [sampleBean,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,fruitsList,prefsList,timetable]; root of factory hierarchy
fruit: apple
fruit: banana
fruit: orange
pref: hokkaido
pref: aomori
pref: akita
timetable: 1st, Math
timetable: 2nd, Science
timetable: 3rd, Reading
timetable: 4th, Social Studies
package sandbox.spring;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class SampleBean {
@Resource
private List<String> fruitsList;
public void showFruits() {
for (String fruit : fruitsList) {
System.out.println("fruit: " + fruit);
}
}
@Resource(name = "prefsList")
private List<String> prefs;
public void showPrefs() {
for (String pref : prefs) {
System.out.println("pref: " + pref);
}
}
@Resource
private Map<String, String> timetable;
public void showTimetable() {
for (Map.Entry<String, String> subject : timetable.entrySet()) {
System.out.println(String.format("timetable: %s, %s",
subject.getKey(), subject.getValue()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment