Spring的 @Bean和@Configuration注解使用

上代码:

@Configuration
public class TestBeanConfig {
	
	@Bean(autowire=Autowire.BY_NAME,name="testBean")
	@Scope(value="prototype")
	public TestBean testBean(){
		System.out.println("You are going to new a objec TestBean ");
		return new TestBean();
	}
	
}
public class TestBean {

	public void sayHello(){
		System.out.println("Hello how are you?");
	}
}

在springBoot中测试 TestBean的sayHello方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestBeanConfigTest {
	@Resource(name="testBean")
	private TestBean testBean;
	
	@Test
	public void test() {
		System.out.println(testBean);
		testBean.sayHello();
	}

}

打印

You are going to new a objec TestBean 
com.example.demo.config.TestBean@5a6d5a8f
Hello how are you?

有后续详细参数设置,再添加

发布了57 篇原创文章 · 获赞 33 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/qq_37155959/article/details/82849939