zoukankan      html  css  js  c++  java
  • xml配置和基于java类的bean配置搭配使用

    如果同时使用了xml配置,和java类的bean配置(当然估计项目中一般不会这样),

    在初始化容器指定资源文件的时候可能会比较麻烦

    此时我们可以把基于java类的bean配置整合到xml中,或xml的配置整合到java类的配置中

    1、整合到xml

      和使用annotation一样,只需要在xml中指定<context:compent-span/>即可

    2、整合到java类

      只需要使用ImportResource annotation即可,在java类中需要注入来自于xml中的bean时,需要在方法中标注Autowired,来给参数注入值

      

    @Configuration
    @ImportResource("classpath:applicationContext.xml")
    public class BeanConf {
        
        @Bean
        @Autowired
        public User user(Car car){
            User user = new User();
            user.setId(123l);
            user.setName("zhengqun");
            user.setCar(car);
            return user;
        }
    }
      基于xml的配置 基于注解的配置 基于java类的配置
    适合场合

    bean的实现类来源于第三方类库DataSource,SessionFactory;

    命名空间的配置aop、context

    bean的实现类是当前项目中开发的 实例化Bean的逻辑比较复杂
  • 相关阅读:
    3.5——课题选择
    3.8——K-th Substring
    01背包dp
    贪心——IQ
    Secret Project Gym
    快速幂&矩阵快速幂
    用栈非递归实现fib数列
    CodeForces Round #590 (Div 3)
    SQL Server常见问题
    SQL基础函数
  • 原文地址:https://www.cnblogs.com/zhengqun/p/3511028.html
Copyright © 2011-2022 走看看