Spring依赖注入的两种种方式

Spring依赖注入的两种种方式

依赖注入的作用就是在使用spring框架时,动态的将其所依赖的对象注入到bean组件中,其实现方式有三种,一种是属性setter方式注入,一种是构造方法注入

  • setter方式注入:是指Spring容器使用setter方法注入被依赖的实例。通过调用无参构造器或无参静态工厂实例化Bean后,调用该Bean的setter方法,即实现了setter方法注入。在本例中就是调用UserServiceImpl的无参构造器实例化Bean后,也就是创建好对象后,使用该对象的setter方法。
  • 构造方法注入:是指Spring容器使用构造方法注入被依赖的实例。

一、setter方式注入

1.在自己创建的包下分别添加Dao包和Service包

1.1 Dao包下编写UserDao接口和UserDaoImpl实现类并为其添加方法
package com.fly.dao;

public interface UserDao {
    public void say();
}
package com.fly.dao;

public class UserDaoImpl implements UserDao {
    @Override
    public void say() {
        System.out.println("Dao层查询数据库成功!");
    }
}
1.2 然后在resource包下添加Spring的配置文件,并创建一个名为userDao的Bean
<?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.fly.dao.UserDaoImpl"/>
</beans>
1.3 service包下同样编写UserService接口和UserService接口实现类UserServiceImpl
package com.fly.service;

public interface UserService {
    public void say();
}
1.4 在实现类UserServiceImpl中声明UserDao方法,并添加属性setter方法
package com.fly.service;

import com.fly.dao.UserDao;

public class UserServiceImpl implements UserService {

    //声明UserDao属性
    private UserDao userDao;

    //为UserDao属性添加setter方法
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void say() {
        this.userDao.say();
        System.out.println("Service层调用Dao层查询数据成功");
    }
}
1.5 然后在spring的配置文件创建一个名为userService的Bean,该Bean用于实例化UserServiceImpl类的信息,并将实例化的userDao注入到userService中
<?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">
    <!--id名是bean的唯一标识,名字可以任意写,但是要一一对应-->
    <bean id="userService" class="com.fly.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
    <bean id="userDao" class="com.fly.dao.UserDaoImpl"/>
</beans>

2.编写测试代码

import com.fly.service.UserService;
import com.fly.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SetTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //UserService userService = new UserServiceImpl();
        UserService userService = (UserServiceImpl) ac.getBean("userService");
        userService.say();
    }
}

注:在上面的代码中UserService userService = (UserServiceImpl)   ac.getBean(“userService”);等价于UserService userService = new UserServiceImpl();

也就是说Bean会为我们new一个对象,而不用我们创建

二、构造方法注入

1. 在UserServiceImpl类中创建构造函数

package com.fly.service;

import com.fly.dao.UserDao;

public class UserServiceImpl implements UserService {

    //声明UserDao属性
    private UserDao userDao;
    
    //创建UserServiceImpl类的构造函数
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void say() {
        this.userDao.say();
        System.out.println("Service层调用Dao层查询数据成功");
    }
}

2.编写Spring的配置文件

<?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="userService" class="com.fly.service.UserServiceImpl">
        <constructor-arg ref="userDao"/>
    </bean>
    <bean id="userDao" class="com.fly.dao.UserDaoImpl"/>
</beans>

3.编写测试代码

import com.fly.service.UserService;
import com.fly.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SetTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //UserService userService = new UserServiceImpl();
        UserService userService = (UserServiceImpl) ac.getBean("userService");
        userService.say();
    }
}
Dao层查询数据库成功!
Service层调用Dao层查询数据成功
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fly狮子座

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值