Spring getbeansoftype method: get the JavaBean of the specified type
•
Java
getBeansOfType(Class type)
Example
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>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码