Spring IOC 一

Spring IOC

标签: JAVA框架


Spring IOC应用

IOC Inversion of Control 控制反转

a.管理对象

创建、初始化、释放资源、销毁

b.维护对象关系

采用注入的方式建立对象关系
Dependency Injection(DI)依赖注入
依赖注入:set方法注入,构造器注入

c.搭建SpringIOC开发环境

引入相关jar包
commons-logging-1.2.jar
spring-beans-5.0.2.RELEASE.jar
spring-context-5.0.2.RELEASE.jar
spring-core-5.0.2.RELEASE.jar
spring-expression-5.0.2.RELEASE.jar
在src添加applicationContext.xml

xml中三种配置bean的方法

<!-- new GregorianCalendar,构造方法-->
<bean id="c1" class="java.util.GregorianCalendar">
</bean>
<!-- 采用Calendar.getInstance()创建,静态工厂方法 -->
<bean id="c2" class="java.util.Calendar"
    factory-method="getInstance">
</bean>
<!-- 采用c2.getTime(),对象工厂方法 -->
<bean id="date" factory-bean="c2" 
    factory-method="getTime">
</bean>
</beans>
Spring容器对象ApplicationContext根据applicationContext.xml来生成bean对象的实例化
//      Calendar c = new GregorianCalendar();
//      Calendar c1 = Calendar.getInstance();
        //创建Spring容器对象(实例化Spring容器)
        String conf="applicationContext.xml";
        ApplicationContext ac = 
                new ClassPathXmlApplicationContext(conf);
        //从Spring容器获取bean对象
        Calendar c1 = ac.getBean("c1",Calendar.class);
        System.out.println(c1);

Spring创建Bean对象的控制

a.控制对象创建方式(使用范围)

在<bean>元素中使用scope属性控制
scope可以支持singleton或prototype
默认值是singleton
<bean scope="singleton">该组件在Spring容器里只有一个bean对象

每次 ac.getBean(“id”);只取出一个相同的对象
该组件每次ac.getBean(“id”);都返回一个对象

b.指定对象初始化方法

利用<bean>元素的init-method指定
当创建对象后自动执行init-method方法

c.指定对象销毁的方法

利用<bean>元素的destroy-method方法
条件:1.组件对象为单例singleton
     2.调用容器AbstractApplicationContext容器对象的close()方法

d.控制单例对象创建时机

在默认情况下,单例对象是Spring容器创建时实例化;可以使用<bean>元素的lazey-init=true属性将创建时机推迟到ac.getBean();时使用

e.信息注入

属性注入set方法

xml文件写法

<bean id="com1" class="org.tarena.bean.Computer">
    <!-- 信息注入 -->
    <property name="cpu" value="I7"></property>
    <property name="hdd" value="索尼"></property>
    <property name="mainbord" value="华硕"></property>
</bean>

java文件中使用

//      Computer com1 = new Computer();
//      com1.setCpu("i5");
//      com1.setHdd("西部数据");
//      com1.setMainbord("华硕主板");
//      com1.show();
        //这两者是相同的
        String conf = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        Computer com1 = ac.getBean("com1",Computer.class);
        com1.show();
构造器方法

xml文件写法

<bean id="phone1" class="org.tarena.bean.Phone">
    <constructor-arg index="0" value="高通"></constructor-arg>
    <constructor-arg index="1" value="4GB"></constructor-arg>
</bean>

构造器写法

    private String cpu;
    private String ram;
    public Phone(String cpu,String ram){
        this.cpu = cpu;
        this.ram = ram;
    }
    public void show(){
        System.out.println("手机配置设置");
        System.out.println("cpu:"+cpu+"ram"+ram);
    }

测试类

        String conf="applicationContext.xml";
        ApplicationContext ac =
                new ClassPathXmlApplicationContext(conf);
        Phone phone = ac.getBean("phone1",Phone.class);
        phone.show();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值