スピニング2.5注入



package com.test.spring.inject;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

/**
 * Spring   
 * 1.  String(   Bean  ) <BR>
 * 2.  Map <BR>
 * 3.  List   private List<String> listInstance;    <BR>
 * 
 * @author Alex
 * 
 */
@Service
public class Property {

	@Autowired
	private String propertyName;

	// @Autowired
	// @Qualifier("otherName")
	// private String propertyOtherName;

	@Autowired
	private Bean bean;

	@Autowired(required = true)
	@Qualifier("mapInstance")
	//    mapInstance      <String, Object>;Spring  byType  .
	private Map<String, Object> mapInstance;

	@Autowired
	@Qualifier("listInstance")
	private List<Object> listInstance;

	@Autowired
	@Qualifier("mapBean")
	private Map<String, Object> mapBean;

	public static void main(String[] args) {
		// ApplicationContext context = new FileSystemXmlApplicationContext(
		// "applicationContext.xml");
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		String[] beans = context.getBeanDefinitionNames();
		for (String bean : beans) {
			System.out.println(bean);
			if ("property".equals(bean)) {
				Property property = (Property) context.getBean(bean);
				System.out.println("***************************************");
				System.out.println("name:" + property.propertyName);
				// System.out.println("OtherName:" +
				// property.propertyOtherName);
				System.out.println("mapInstance:" + property.mapInstance);
				System.out.println("listInstance:" + property.listInstance);
				System.out.println("mapBean:" + property.mapBean);
				System.out.println("bean.name:" + property.bean.getName());
				System.out.println("***************************************");
			}
		}
	}
}


<?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:util="http://www.springframework.org/schema/util"
	xmlns:tool="http://www.springframework.org/schema/tool"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-lazy-init="false">
	<description>Spring    </description>

	<!--     bean,          -->
	<context:component-scan base-package="com.test.spring.inject" />

	<bean class="java.lang.String">
		<constructor-arg type="java.lang.String" value="string_1234" />
	</bean>

	<util:map id="mapInstance">
		<entry key="map_key1" value="map_Value1" />
		<entry key="map_key2" value="map_Value2" />
	</util:map>

	<util:list id="listInstance" list-class="java.util.ArrayList">
		<value type="java.lang.String">list_Value2</value>
		<value type="java.lang.String">list_Value2</value>
	</util:list>

	<util:map id="mapBean" map-class="java.util.HashMap">
		<entry key="mapKeyMap" value-ref="mapInstance" />
		<entry key="mapKeyList" value-ref="listInstance" />
	</util:map>

	<!-- 
		<util:constant id="name" static-field="java.lang.Integer.MAX_VALUE" />
		
		<bean id="otherName" class="java.lang.String">
		<constructor-arg type="java.lang.String" value="anotherString_1234" />
		</bean>
	-->

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName"
			value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>file:config.properties</value>
			</list>
		</property>
	</bean>

</beans>