依赖注入及其实现

1.依赖注入的优点
如果某个java的A对象要调用java的B对象,则要在A中new一个B对象。A是调用者,B是被调用者。这样的话,调用者和被调用者耦合性大,不适合后期项目的维护和升级。而Spring的依赖注入解决了这个问题。
2.依赖注入(Dependency Injection)和控住反转(Inversion Of Control)
这两个词的意义相同,只不过是角度不同。被调用者的创建不再是由调用者进行实现,而是由Spring容器进行控制,控制权发生了反转,因此这就是Srping的控制反转。从另一角度看,调用者依赖着被调用者,而spring容器提供了这个依赖,为调用者注入了它依赖的实例,这就是依赖注入。
3.依赖注入的两种实现方式
编写两个类UserDaoImpl和AnotherUserDaoImpl,在AnotherUserDaoImpl实例中注入UserDaoImpl实例。
这是UserDaoImpl的实现代码

package com.itheima.ioc;

public interface UserDao {
    public void say();
}
package com.itheima.ioc;

public class UserDaoImpl implements UserDao{
    @Override
    public void say() {
        System.out.println("UserDao say hello world!");
    }
}

测试依赖注入的类的代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDI {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        AnotherUserDaoImpl anotherUserDao = (AnotherUserDaoImpl) applicationContext.getBean("anotherUserDao");
        anotherUserDao.say();
    }
}

由于依赖注入的形式不同,被注入的实例的对象实现和xml的编写是不同的。
* 属性setter方法注入
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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="com.itheima.ioc.UserDaoImpl"/>
    <bean id="anotherUserDao" class="com.itheima.ioc.AnotherUserDaoImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
</beans>

AnotherUserDaoImpl的实现如下

package com.itheima.ioc;

public class AnotherUserDaoImpl implements UserDao {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    public void say() {
        this.userDao.say();
        System.out.println("anotherUserDao say hello world");
    }
}
  • 构造方法注入
    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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="com.itheima.ioc.UserDaoImpl"/>
    <bean id="anotherUserDao" class="com.itheima.ioc.AnotherUserDaoImpl"/>
</beans>

AnotherUserDaoImpl的实现如下

package com.itheima.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnotherUserDaoImpl implements UserDao {
    @Override
    public void say() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.say();
        System.out.println("anotherUserDao say hello world");
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值