美文网首页
spring ioc注入demo

spring ioc注入demo

作者: Eve0 | 来源:发表于2017-09-17 10:37 被阅读0次

    • 设值注入
    <bean id="iBuyServiceImpl" class="com.example.demo.ioc.service.IBuyServiceImpl">
        <property name="ibuyDao" ref="ibuyDaoImpl"></property>
    </bean>
    
    public class IBuyServiceImpl implements IBuyService {
        private IBuyDao ibuyDao;
        
        public void setIbuyDao(IBuyDao ibuyDao) {
            this.ibuyDao = ibuyDao;
        }
        
        public void buysomething(String name) {
            // TODO Auto-generated method stub
            ibuyDao.buysomething(name);
        }
    
    }
    
    • 构造器注入
    <bean id="iBuyServiceImpl" class="com.example.demo.ioc.service.IBuyServiceImpl">
        <constructor-arg name="ibuyDao" ref = "ibuyDaoImpl"></constructor-arg>
    </bean>
    
    public class IBuyServiceImpl implements IBuyService {
        private IBuyDao ibuyDao;
        
        public IBuyServiceImpl(IBuyDao ibuyDao) {
            super();
            this.ibuyDao = ibuyDao;
        }
    
    
        public void buysomething(String name) {
            // TODO Auto-generated method stub
            ibuyDao.buysomething(name);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:spring ioc注入demo

          本文链接:https://www.haomeiwen.com/subject/xgypsxtx.html