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

package com.imooc.beanannotation.javabased;

public interface Store {

}
package com.imooc.beanannotation.javabased;

public class StringStore implements Store {

}
package com.imooc.beanannotation.javabased;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StoreConfig {

	@Bean
	public Store stringStore(){
		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.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());;
	}
}

运行结果:

注意:如果使用@Bean注解,在没有指定它内部的情况下,那Bean的name是方法的名称。

指定Bean名称:

package com.imooc.beanannotation.javabased;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StoreConfig {

	@Bean(name="store")
	public Store stringStore(){
		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.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("store");
		System.out.println(store.getClass().getName());;
	}
}

运行结果:

initMethod&destroyMethod:

package com.imooc.beanannotation.javabased;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StoreConfig {

	@Bean(name="stringStore",initMethod="init",destroyMethod="destroy")
	public Store stringStore(){
		return new StringStore();
	}
}
package com.imooc.beanannotation.javabased;

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

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

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

运行结果:

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