 Uestc_L
      Uestc_L
    
    Interface里很常用的,比如Java里的Collection接口就在使用泛型。
当你定义一个Collection的时候:
Collection<String> strings = new Collection<String>;
<>内的“String”就是泛型的体现啦
 Serena_Cecilia
      Serena_Cecilia
    
    你类忘写@Component了吧
 慕沐7199068
      慕沐7199068
    
    已解决:
 慕粉2497639
      慕粉2497639
    
    没错,你讲的很正确, @Qualifier 注解,确实可以 在 @Autowired 注解内 选出一个 合格的匹配 Bean
 阿崔cuicui
      阿崔cuicui
    
    都可以的,只是调用方法时,如果是Store的话调用不了StringStore的方法,还得强转为StringStore才能调用。
 qq_莣優錵_03493732
      qq_莣優錵_03493732
    
    使用XML一样可以做你说的那些配置,只是使用@Autowired @Qualifier更加方便快捷。
 阳光洒在路上就不怕心碎f
      阳光洒在路上就不怕心碎f
    
    我这5.2的spring的版本一样
 慕标7744821
      慕标7744821
    
        @Autowired
    @Qualifier("stringStore")
    private Store<String> s1;
    
    @Autowired
    @Qualifier("integerStore")
    private Store<Integer> s2;
 未卜先知
      未卜先知
    
    我将你的代码执行了一遍,可以在:
泛型注入的时候指定被注入的Bean的name啊
 未卜先知
      未卜先知
    
    首先interface store<T>,泛型接口,
接下来2个实现类,StringStore和IntegerStore
////////////////////////////
@Bean
public StringStore stringStore(){   
 return new StringStore();  
}
这里没有指定bean的name,默认是成员方法的名字,即stringStore。spring扫描之后初始化的名为stringStore的bean注册到容器中,此时容器中有一个名为stringStore的bean。
/////////////////////////////
@Bean     
 public IntegerStore integerStore() {   
 return new IntegerStore();  
}解释同上此时,此时容器中有2个bean,分别为stringStore和integerStore.
//////////////////////
@Autowired
private Store<String> s1;
@Autowired
private Store<Integer> s2;
@Autowired注解是按照类型在容器中搜索相应的bean,s1对应容器中的stringStore,s2对应integerStore。///////////////////////////
@Bean(name = "stringStoreTest")
public Store stringStoreTest() {  
System.out.println("s1 : " + s1.getClass().getName());    
System.out.println("s2 : " + s2.getClass().getName());  
return new StringStore();}
此时这个方法里面用到的s1,s2就对应不是空了。@bean注解会被扫描初始化生成一个指定名字为stringStoreTest的bean注册到容器中。返回类型是Store时,通过3个@Bean初始化的bean注册到容器中的类型不同,而@Auto wired注解也不会通过类型匹配错误,从而导致s1空指针。当返回类型是StringStore时,容器中会出现2个类型是String Store的bean,@Autowired从容器中通过类型匹配时会出错,不知道是哪一个即bean不唯一,无法匹配,s1为空,即报空指针错误。
解决办法可以用老师的方法修改返回类型,也可以用@Qualifier指定名字,或者使用@Resource注解。
 齐ham
      齐ham
    
    @AutoWire的作用是根据成员变量的名字自动注入相应的bean实例。@Configuration可以看成一个配置文件,@Bean就相当于配置文件的bean节点,当使用@AutoWired 自动装配后,可以通过对应的属性名称通过点操作符调用该类型的方法
 gao634209276
      gao634209276
    
    可以这样写 stringStoreTest:
@Bean(name="stringStoreTest")
public StoreConfig stringStoreTest(){
System.out.println("s1:"+s1.getClass().getName());
System.out.println("s2:"+s2.getClass().getName());
return new StoreConfig();
}
测试类为:
@Test
public void testG() {
super.getBean("stringStoreTest");
}
结果跟老师显示的一样!否则会报 s1,s2 变量空指针异常!
 芙蓉姐姐
      芙蓉姐姐
    
    有可能是有些变化,我没看过4.2.2有哪些细节变化,有些变化在changelist也不会列出来,这就是开源软件让人头疼的地方,spring还算是好的了
 十佳菜鸟
      十佳菜鸟
    
     爱吃咸蛋的超人
      爱吃咸蛋的超人
    
     elang3000
      elang3000
    
    这两个的相同点是都是注解在类上的注解,不同点就是@Component注解的范围最广,所有类都可以注解,但是@Configuration注解一般注解在这样的类上:这个类里面有@Value注解的成员变量和@Bean注解的方法,就是一个配置类。
 倾真鱼
      倾真鱼
    
    代码没问题,可能是spring版本不同,你试试看用spring4.0.5,所有例子在这个版本下是都能正常运行的。
 Wall_E _0001
      Wall_E _0001
    
    不是,有一个压缩包是Spring所有章节源码
 慕丝8802791
      慕丝8802791