上代码:
@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?
有后续详细参数设置,再添加