Spring(IOC) 对象创建三种方式、对象bean起别名

IOC(控制翻转)
概念
把对象的创建、初始化、销毁等工作交给spring容器来做
helloWorld案例

步骤:

1、写一个HelloWorld类
2、写一个配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <!-- 
        beans
            一个bean代表一个类
          所以beans就是很多个类
     -->
     <!-- 
        一个类
        id  标示符   
        class 类的全名
      -->
    <bean id="helloWorld" class="com.itheima09.spring.ioc.helloworld.HelloWorld">
    </bean>
</beans>
3、客户端
    说明:
    Spring容器的作用就是为HelloWorld这个类创建对象
public class HelloWorldTest {
    @Test
    public void testHello(){
        /**
         * 以前的作法:创建对象,并调用
         */
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.hello();
    }

    @Test
    public void testHello_Spring(){
        /**
         * 1启动spring容器
         * 2从spring容器中把对象提取出来
         * 3对象调用方法
         */
        //启动了spring容器了
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        //从spring容器中把helloworld对象提取出来
        HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
        helloWorld.hello();
    }
}

这里写图片描述

spring 创建对象的三种方法:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
   <!-- 
        把helloWorld这个类放入到spring容器中 :使用构造函数创建对象
    -->
   <bean id="helloWorld" lazy-init="true"
    class="com.itheima09.spring.ioc.createobject.method.HelloWorld"></bean>

   <!-- 
        引入静态工厂类
        factory-bean  指明工厂bean
    -->
   <bean id="helloWorld2"  lazy-init="true"
        class="com.itheima09.spring.ioc.createobject.method.HelloWorldFactory"
        factory-method="getInstance"></bean>

   <!-- 
        引入实例工厂
    -->
   <bean id="helloWorldFactory" 
    class="com.itheima09.spring.ioc.createobject.method.HelloWorldFactory2"></bean>
   <!-- 
        factory-bean  指向工厂bean
        factory-method  工厂方法
    -->
   <bean id="helloWorld3" factory-bean="helloWorldFactory"  lazy-init="true"
    factory-method="getInstance"></bean>
</beans>
public class HelloWorldTest {
    @Test
    public void testCreateObject_DefaultConstructor(){
        //启动spring容器
        ApplicationContext context =  
                new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
        helloWorld.hello();
    }

    @Test
    public void testCreateObject_StaticFactory(){
        /**
         * 引入静态工厂类 :测试
         */
        ApplicationContext context =  
                new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld2");
        helloWorld.hello();
    }

    @Test
    public void testCreateObject_InstanceFactory(){
        /**
         * 引入实例工厂:测试
         */
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld3");
        helloWorld.hello();
    }
}

1.默认构造函数
2.静态工厂
这里写图片描述

3.实例工厂
这里写图片描述

别名:
主要作用:将一个bean 在多个模块中使用不同的别名

  <bean id="helloWorld" class="com.itheima09.spring.ioc.alias.HelloWorld"></bean>

   <alias name="helloWorld" alias="王二麻子"/>

//测试:
@Test
    public void testAlias(){
        //启动spring容器
        ApplicationContext context =  
                new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld)context.getBean("狗蛋");
        helloWorld.hello();
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值