Java 基于范型的自动装配

类变量

接口类

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");
	}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部