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对象

  • 集合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

 

 ,

Sunbet 申博

Sunbet 申博www.1888ss.com是当前各类实时网站中更新最迅速、影响力最大的网络综合新闻共享平台,旨在为您提供最新鲜最准确的热点时事,这里有越搜越热的最新最远话题,还有热心网友在线交流,让您抓住世界最新一刻,把握关注要点的最新动态,利用精准权威的大数据网络新闻平台,真正实现坐家中,览世界的小目标,在短时间内先人一步获得最新资讯,引领网络资讯新生活

Allbet Gaming声明:该文看法仅代表作者自己,与阳光在线无关。转载请注明:佳木斯信息网:Spring Ioc 依赖查找
发布评论

分享到:

ug环球电脑版下载:削减成本+预防熏染 IOC与东京奥组委确认简化办赛
1 条回复
  1. 欧博亚洲网址
    欧博亚洲网址
    (2020-06-12 00:36:42) 1#

    联博统计接口www.326681.com采用以太坊区块链高度哈希值作为统计数据,联博以太坊统计数据开源、公平、无任何作弊可能性。联博统计免费提供API接口,支持多语言接入。好多人都看啊

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。