Spring Ioc 依赖查找
Spring ioc 有依赖查找和依赖注入,之前不太明白依赖查找是什么意思,翻了一大堆博客看了好多定义也不太清楚 ,后来看了小马哥视频,他通过代码演示,清楚地讲解了什么是 依赖查找以及几种依赖查找的方法,本文结合代码浅析一下Spring ioc的依赖查找,通过代码来更加形象的理解依赖查找。
1.根据bean名称查找:
-
实时查找
-
1.配置xml文件
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd 5 "> 6 <!--实时查找--> 7 <bean id="User" class="thinking.in.spring.ioc.overview.domain.User"> 8 <property name="id" value="1"/> 9 <property name="name" value="小明"/> 10 </bean> 11 <!--延时查找--> 12 <bean id="objectFactory" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean"> 13 <property name="targetBeanName" value="User"/> 14 </bean> 15 </beans>
2.启动spring应用上下文
1 public class DenpendencyLookupDemo { 2 public static void main(String[] args) { 3 BeanFactory beanFactory=new ClassPathXmlApplicationContext("META-INF/dependency-Lookup-context.xml"); 4 //时实查找 5 User user=(User) beanFactory.getBean("User"); 6 System.out.println("实时查找"+user.toString()); 7 //延时查找 8 ObjectFactory<User> objectFactory= (ObjectFactory<User>)beanFactory.getBean("objectFactory"); 9 User user1=objectFactory.getObject(); 10 System.out.println("延时查找"+user1.toString()); 11 } 12 }
输出:
2.通过类型查找
-
集合Bean对象
1 /** 2 * 根据类型查找单个bean 3 * @param beanFactory 4 */ 5 private static void lookupSingleByType(BeanFactory beanFactory){ 6 7 User user2 = beanFactory.getBean(User.class); 8 System.out.println("根据类型时实查找单一" + user2.toString()); 9 } 10 /** 11 * 查找多个bean 12 */ 13 private static void lookupCollectionBtType (BeanFactory beanFactory){ 14 if (beanFactory instanceof ListableBeanFactory) { 15 ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory; 16 Map<String, User> users = listableBeanFactory.getBeansOfType(User.class); 17 System.out.println("查找多个bean" + users); 18 } 19 }
输出:
3.通过注解查找
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd 5 "> 6 <!--实时查找--> 7 <bean id="User" class="thinking.in.spring.ioc.overview.domain.User"> 8 <property name="id" value="1"/> 9 <property name="name" value="小明"/> 10 </bean> 11 <!--延时查找--> 12 <bean id="objectFactory" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean"> 13 <property name="targetBeanName" value="User"/> 14 </bean> 15 <bean id="superUser" class="thinking.in.spring.ioc.overview.domain.SuperUser" parent="User" primary="true"> 16 <property name="number" value="1"/> 17 </bean> 18 </beans>
@Super注解
1 @Target({ElementType.TYPE}) 2 @Retention(RetentionPolicy.RUNTIME) 3 public @interface Super { 4 }
SuperUser类
@Super public class SuperUser extends User{ private String number; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } @Override public String toString() { return "SuperUser{" + "number='" + number + '\'' + "} " + super.toString(); } }
用法
1 /** 2 * 通过注解查找 3 * @param beanFactory 4 */ 5 private static void lookupByAnnotatonType(BeanFactory beanFactory){ 6 if(beanFactory instanceof ListableBeanFactory){ 7 ListableBeanFactory listableBeanFactory=(ListableBeanFactory)beanFactory;
//根据注解查找 8 Map<String,User> users=( Map)listableBeanFactory.getBeansWithAnnotation(Super.class); 9 System.out.println("查找标注@Super 所有的User集合对象"+users); 10 } 11 }
输出:
只查找出标注了@Super注解且是User类型的Bean