getBeansOfType(Class type)
引數說明:public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext( "appContext.xml"); String[] eanNames = context.getBeanNamesForType(Date.class); //獲取指定型別的所有JavaBean物件 Map beans = context.getBeansOfType(Date.class); //獲取容器中指定類 for(String name:beanNames){ Date bean = (Date)beans.get(name); System.out.println("名稱為"+name+"的JavaBean輸出結果為:" +bean); } }
<?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-2.5.xsd"> <bean id="time"class="java.util.Date"></bean> <%--設定JavaBean--%> <bean id="time2"class="java.util.Date"></bean> <bean id="time3"class="java.util.Date"></bean> </beans>