Spring 配置

PPG007 ... 2021-12-26 Less than 1 minute

# Spring 配置

  • 别名。

    • 通过 alias 标签起:

      <alias name="userService" alias="userService2"/>
      
      1
    • 通过 bean 标签中的 name 属性起别名,可以用逗号或者空格或者分号进行分割起多个别名:

    <bean class="service.UserServiceImpl" id="userService" name="userService2,userService3">
            <constructor-arg ref="userDaoOracle"/>
    </bean>
    
    1
    2
    3
  • scope 作用域:

    bean的作用域

  • import:

    一般用于团队开发,将多个配置文件导入合并为一个,允许多个相同别名。

    <!--在applicationContext.xml中添加import标签-->
    <import resource="beans.xml"/>
    
    1
    2
    //测试代码只读取applicationContext.xml
    public class MyTest2 {
        public static void main(String[] args) {
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    
            Object o=context.getBean("userService3");
            ((UserServiceImpl)o).getUser();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
Last update: August 15, 2022 09:32
Contributors: Koston Zhuang , PPG007