/*
@Configuration is marked on a class, which is equivalent to using the class as <beans> in the spring.xml configuration file, which is used to configure the spring container (application context)
@Bean can be understood as using the <bean> tag in spring.xml
Note:
(1), @Bean annotation is on the method that returns the instance, if the name of the bean is not specified through @Bean, the default is the same as the method name marked;
(2) The default scope of the @Bean annotation is the singleton scope, which can be set to the prototype scope through @Scope("prototype");
(3) Since the role of @Bean is to register bean objects, you can use @Component, @Controller, @Service, @Ripository and other annotations to register beans. Of course, you need to configure the @ComponentScan annotation for automatic scanning.
(4), @Bean annotation registers beans, and can specify initialization and destruction methods @Bean(name="testNean", initMethod="start", destroyMethod="cleanUp")
ApplicationContext Architecture: https://blog.csdn.net/h12kjgj/article/details/53725507
AnnotationConfigApplicationContext (replace ClassPathXmlApplicationContext with AnnotationConfigApplicationContext) Detailed explanation: https://blog.csdn.net/tuoni123/article/details/79976105
*/
@Configuration
public class SpringConfig {
public SpringConfig(){
System.out.println("spring container startup initialization...");
}
@Bean
public WpTerms wpTerms(){//Custom
return new WpTerms();
}
//
@Bean(name = "wpTermmeta")
public WpTermmeta wpTermmeta() {//Custom
return new WpTermmeta("allen","28");
}
public static void main(String[] args) {
ApplicationContext annotationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
ApplicationContext annotationContext1 = new AnnotationConfigApplicationContext("com.wordpress.master.bean");
//The way to load the spring-context.xml file before
//ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
WpTermmeta c = annotationContext.getBean("wpTermmeta", WpTermmeta.class);
WpTermmeta c1 = annotationContext1.getBean("wpTermmeta", WpTermmeta.class);
Assert.assertEquals("allen",c.getMetaKey()); //测试
Assert.assertEquals("allen",c1.getMetaKey()); //测试
}
}
package com.wordpress.master.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
//Add the annotation for the registered bean
@Component
public class TestBean {
public void sayHello(){
System.out.println("TestBean sayHello...");
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
// get bean
TestBean tb = (TestBean) context.getBean("testBean");
tb.sayHello();
}
}
package com.wordpress.master.bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
//添加自动扫描注解,basePackages为TestBean包路径
@ComponentScan(basePackages = "com.wordpress.master.bean")
public class TestConfiguration {
public TestConfiguration(){
System.out.println("spring容器启动初始化。。。");
}
//取消@Bean注解注册bean的方式
//@Bean
//@Scope("prototype")
//public TestBean testBean() {
// return new TestBean();
//}
}