@Configuration的由来
@Configuration是在Spring3.0之后出现的,其目的是为了简化原有的spring的xml配置。结合@Bean注解,实现了用Java代码的方式来管理spring-bean
@Configuration类的启动方式
第一种方式:将@Configuration类注册到AnnotationConfigApplicationContext中
@Configuration
public class AppConfig {
@Bean
public MySpringBean mySpringBean ( ) {
System. out. println ( "注册mySpringBean" ) ;
MySpringBean mySpringBean = new MySpringBean ( ) ;
mySpringBean. setName ( "mySpringBeann" ) ;
return mySpringBean;
}
}
public class AppMainMethodOne {
public static void main ( String[ ] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( AppConfig. class ) ;
MySpringBean mySpringBean = ( MySpringBean) context. getBean ( "mySpringBean" ) ;
System. out. println ( mySpringBean) ;
}
}
第二种方式:将@Configuration类当作普通的spring-bean,在xml文件中配置(其实这种方式有点本末倒置)
public class AppConfig {
@Bean
public MySpringBean mySpringBean ( ) {
System. out. println ( "注册mySpringBean" ) ;
MySpringBean mySpringBean = new MySpringBean ( ) ;
mySpringBean. setName ( "mySpringBeann" ) ;
return mySpringBean;
}
}
< ? xml version= "1.0" encoding= "UTF-8" ? >
< beans xmlns= "http://www.springframework.org/schema/beans"
xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns: context= "http://www.springframework.org/schema/context"
xsi: schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd" >
< context: annotation- config/ >
< bean id= "appConfig" class = "org.hugh.spring.configuration.AppConfig" / >
< / beans>
public class AppMainMethodTwo {
public static void main ( String[ ] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "classpath:configuration.xml" ) ;
MySpringBean mySpringBean = ( MySpringBean) applicationContext. getBean ( "mySpringBean" ) ;
System. out. println ( mySpringBean) ;
}
}
第三种方式:使用context:component-scan,(和二种方式一样,也是本末倒置)
修改configuration. xml即可
< ? xml version= "1.0" encoding= "UTF-8" ? >
< beans xmlns= "http://www.springframework.org/schema/beans"
xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns: context= "http://www.springframework.org/schema/context"
xsi: schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd" >
< ! -- 用于激活BeanPostProcessor-- >
< context: annotation- config/ >
< ! -- identical with @ComponentScan -- >
< context: component- scan base- package = "org.hugh.spring.configuration" / >
< / beans>
第四种方式:@Configuration和@ComponentScan结合,替换xml配置文件
AppScanConfig
@Configuration
@ComponentScan ( "org.hugh.spring.bean" )
public class AppScanConfig {
}
org.hugh.spring.bean.User
@Component
@Data
public class User implements Serializable {
private static final long serialVersionUID = 5135661460008089536 L;
private String userName = "Jack" ;
private Integer age = 18 ;
}
public class AppMainMethodFour {
public static void main ( String[ ] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( AppScanConfig. class ) ;
User user = ( User) context. getBean ( "user" ) ;
System. out. println ( user) ;
}
}
总结
详细可查看@Configuration的文档说明 代码地址:https://github.com/HughGilbert/my-develop-demo/tree/master/spring-demo/src/main/java/org/hugh/spring/configuration