[spring-context]------@Configuration

本文深入探讨了Spring框架中@Configuration注解的起源及其四种启动方式,包括直接注册到ApplicationContext、XML配置、context:component-scan扫描及与@ComponentScan结合替代XML配置。通过实例代码展示了如何用Java代码替代传统XML配置,实现更简洁的Spring Bean管理。
摘要由CSDN通过智能技术生成

@Configuration的由来

  • @Configuration是在Spring3.0之后出现的,其目的是为了简化原有的spring的xml配置。结合@Bean注解,实现了用Java代码的方式来管理spring-bean

@Configuration类的启动方式

第一种方式:将@Configuration类注册到AnnotationConfigApplicationContext中

  • AppConfig
@Configuration
public class AppConfig {

    @Bean
    public MySpringBean mySpringBean() {
        System.out.println("注册mySpringBean");
        MySpringBean mySpringBean = new MySpringBean();
        mySpringBean.setName("mySpringBeann");
        return mySpringBean;
    }
}
  • AppMainMethodOne
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文件中配置(其实这种方式有点本末倒置)

  • AppConfig
public class AppConfig {
    @Bean
    public MySpringBean mySpringBean() {
        System.out.println("注册mySpringBean");
        MySpringBean mySpringBean = new MySpringBean();
        mySpringBean.setName("mySpringBeann");
        return mySpringBean;
    }
}
  • 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">

    <context:annotation-config/>

    <bean id="appConfig" class="org.hugh.spring.configuration.AppConfig"/>

</beans>
  • AppMainMethodTwo
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 = 5135661460008089536L;

    private String userName = "Jack";

    private Integer age = 18;
}
  • AppMainMethodFour
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值