spring学习笔记03--依赖注入与控制反转

本文详细介绍了如何使用Spring框架进行依赖注入。提供了两种依赖注入的方法,一种是通过property标签的name和ref属性,另一种是通过内部bean标签。同时,还展示了如何注入基本数据类型的属性,并给出了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一。依赖注入


要将PersonDao注入到PersonService中,需要如下的配置:
方式一 ,通过property标签的name属性和ref属性

  <?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.xsd">

   <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>

   <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype" init-method="init"
   destroy-method="destroy">
      <property name="personDao" ref="personDao"></property>
   </bean>
</beans>

其中<property name="personDao" ref="personDao"></property>中的name表示需要注入的是哪个属性,ref是需要注入的bean的id。

方法二,通过内部bean标签,这个bean不能被其他组件所引用

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype" init-method="init"
   destroy-method="destroy">
      <property name="personDao">
         <bean class="cn.itcast.dao.impl.PersonDaoBean"/>
      </property>
   </bean>

如果需要注入的这个属性是一个基本数据类型(包括String)呢?
这时候的配置如下:

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype">
      <property name="name" value="jalenTest"></property>
   </bean>

同时,PersonService的实现类需要有personDao属性的set方法。
完整代码:

1.服务层:

PersonService.java

package cn.itcast.service;

public interface IPersonService {

    /*
     * (non-Javadoc)
     * 
     * @see cn.itcast.service.impl.IPersonService#save()
     */
    void save();

    void add();

}

PersonServiceBean .java

package cn.itcast.service.impl;

import cn.itcast.dao.PersonDao;
import cn.itcast.service.IPersonService;

public class PersonServiceBean implements IPersonService {
    private PersonDao personDao;
    /*
     * (non-Javadoc)
     * 
     * @see cn.itcast.service.impl.IPersonService#save()
     */
    /* (non-Javadoc)
     * @see cn.itcast.service.impl.IPersonService#save()
     */
    @Override
    public void save() {
        System.out.println("我是save()方法!");
    }

    private void init() {
        System.out.println("初始化方法被调用了");
    }
    /* (non-Javadoc)
     * @see cn.itcast.service.impl.IPersonService#add()
     */
    @Override
    public void add(){
        personDao.add();
    }
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

}
2.Dao层:

PersonDao.java

package cn.itcast.dao;

public interface PersonDao {
    void add();
}

PersonDaoBean.java

package cn.itcast.dao.impl;

import cn.itcast.dao.PersonDao;

public class PersonDaoBean implements PersonDao {
    /* (non-Javadoc)
     * @see cn.itcast.dao.impl.PersonDao#add()
     */
    @Override
    public void add(){
        System.out.println("PersonDaoBean的add方法被执行!");
    }
}

测试方法:

@Test
    public void instanceSpring(){
        //初始化Spring容器
        AbstractXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        IPersonService personService = (IPersonService) ctx.getBean("personService");
        personService.add();
        //ctx.close();
    }

运行结果:
运行结果
可以看到,如果没有注入成功,则调用add()方法会报空指针异常,这里正常执行,说明注入成功。

二。控制反转


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值