观看慕课moocer老师视频Spring Bean装配之Autowire注解说明-7

基于泛型自动装配:

package com.imooc.beanannotation.javabased;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;

@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {

	/*@Value("${url}")
	private String url;
	
	@Value("${jdbc.username}")
	private String username;
	
	@Value("${password}")
	private String password;
	
	@Bean
	public MyDriverManager myDriverManager(){
		return new MyDriverManager(url,username,password);
	}*/
	/*
	@Bean(name="stringStore",initMethod="init",destroyMethod="destroy")
	public Store stringStore(){
		return new StringStore();
	}*/
	
	/*@Bean(name="stringStore")
	@Scope(value="prototype")
	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();
}
}
package com.imooc.test.beanannotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.beanannotation.javabased.MyDriverManager;
import com.imooc.beanannotation.javabased.Store;
import com.imooc.ioc.interfaces.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestJavabased extends UnitTestBase {
	
	public TestJavabased(){
		super("classpath*:spring-beanannotation.xml");
}
	@Test
	public void test(){
		Store store=super.getBean("stringStore");
		System.out.println(store.getClass().getName());;
	}
	
	@Test
	public void testMyDriverManager(){
		MyDriverManager manager=super.getBean("myDriverManager");
		System.out.println(manager.getClass().getName());;
	}
	
	@Test
	public void testScope(){
		Store store=super.getBean("stringStore");
		System.out.println(store.hashCode());
		
		store=super.getBean("stringStore");
		System.out.println(store.hashCode());
	}
	
	@Test
	public void testG(){
		Store store=super.getBean("stringStoreTest");
}
}
package com.imooc.beanannotation.javabased;

public class StringStore implements Store<String> {
	public void init(){
		System.out.println("This is init.");
	}
	
	public void destroy(){
		System.out.println("This is destroy.");
	}
}
package com.imooc.beanannotation.javabased;

public interface Store<T> {

}
package com.imooc.beanannotation.javabased;

public class IntegerStore implements Store<Integer> {

}

运行结果:

基于集合方式自动装配:

package com.imooc.beanannotation.javabased;

import org.springframework.stereotype.Component;

@Component
public class IntegerStore implements Store<Integer> {

}
package com.imooc.beanannotation.javabased;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Com<T> {
	
	@Autowired
	private List<Store<Integer>> list;

	public void say(){
		if(null!=list){
			for(Store<Integer> bean:list){
				System.out.println(bean.getClass().getName());//getClass().getName()是用来返回Class对象所代表的具体对象的名称。
			}
		}else{
			System.out.println("List<Store<Integer>> list is null");
		}
	}

}
package com.imooc.beanannotation.javabased;

public interface Store<T> {

}
package com.imooc.test.beanannotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.beanannotation.javabased.Com;
import com.imooc.beanannotation.javabased.MyDriverManager;
import com.imooc.beanannotation.javabased.Store;
import com.imooc.ioc.interfaces.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestJavabased extends UnitTestBase {
	
	public TestJavabased(){
		super("classpath*:spring-beanannotation.xml");
}
	@Test
	public void test(){
		Store store=super.getBean("stringStore");
		System.out.println(store.getClass().getName());;
	}
	
	@Test
	public void testMyDriverManager(){
		MyDriverManager manager=super.getBean("myDriverManager");
		System.out.println(manager.getClass().getName());;
	}
	
	@Test
	public void testScope(){
		Store store=super.getBean("stringStore");
		System.out.println(store.hashCode());
		
		store=super.getBean("stringStore");
		System.out.println(store.hashCode());
	}
	
	@Test
	public void testG(){
		Com com=super.getBean("com");
		com.say();
}
}

运行结果:

自定义qualifier

原文链接:加载失败,请重新获取