码迷,mamicode.com
首页 > 编程语言 > 详细

zbb20170216_spring_ioc

时间:2017-02-23 00:42:57      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:[]   --   bsp   str   方法   location   div   text   构造   

1、结构图

技术分享

2、class文件

MyIoc.java

package com.zbb.ioc;

public class MyIoc {
    MyService myService;
    public void ioc(){
        myService.service();
    }
    /*public MyIoc(MyService myService) {
        super();
        this.myService = myService;
    }*/
    public MyService getMyService() {
        return myService;
    }
    public void setMyService(MyService myService) {
        this.myService = myService;
    }
    
}

MyService.java

package com.zbb.ioc;

public class MyService {
public void service(){
    System.out.println("service");
}
}

MyTest.java

package com.zbb.ioc;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyIoc bean = (MyIoc)classPathXmlApplicationContext.getBean("myIoc");
        bean.ioc();
    }
    
}

 

3、配置文件

applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <!-- 构造器注入 -->
    <!-- 
    <bean id="myIoc" class="com.zbb.ioc.MyIoc">
        <constructor-arg>
            <bean class="com.zbb.ioc.MyService"/>
        </constructor-arg>
    </bean>
     -->
     <!-- setter方法注入 -->
     <bean id="myIoc" class="com.zbb.ioc.MyIoc" >
         <property name="myService" >
             <ref bean="myService"/>
         </property>
     </bean>
     <bean id="myService" class="com.zbb.ioc.MyService" />
</beans>

 

 

技术分享

 

zbb20170216_spring_ioc

标签:[]   --   bsp   str   方法   location   div   text   构造   

原文地址:http://www.cnblogs.com/super-admin/p/6431393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!