类变量
接口类
package com.imooc.beanannotation.javabased; public interface Store<T> { }
注入类
@Configuration public class StoreConfig { @Bean(name = "stringStore") @Scope(value="prototype", proxyMode = ScopedProxyMode.TARGET_CLASS) public Store stringStore() { return new StringStore(); } @Autowired private Store<String> s1; @Autowired private Store<Integer> s2; @Bean public StringStore stringStore() { return new StringStore(); } @Bean public IntegerStore integerStore() { return new IntegerStore(); } @Bean(name = "stringStoreTest") public Store stringStoreTest() { System.out.println("s1 : " + s1.getClass().getName()); System.out.println("s2 : " + s2.getClass().getName()); return new StringStore(); } }
Bean注解会生成对象,并使用Autowired注解将对象注入到成员变量中。使用时候,使用getBean从IOC容器中,获取对象。
测试方法
@Test public void testG() { StringStore store = super.getBean("stringStoreTest"); }