spring 依赖注入

在web项目开发过程中总是集成spring框架进行开发,而spring框架的核心则是依赖注入,之前一直在用spring的依赖注入却没有记录下来。今天则专门来记录下依赖注入的技术;

spring容器:我个人的理解就是一个对象工厂,对象工厂中管理着所有的在容器中定义的对象,在传统配置中,以xml的配置方式来将对象定义在spring容器中,首先spring容器需要解析xml的标签:包含 bean context tx 等标签,并且把xml中的bean的定义转化为内部的beanDifination;

spring xml方式实现依赖注入:

为了模拟效果:我将项目结构分为 DAO SERVICE CONTOLLER 三个层级,其中将DAO 注入SERVICE 中,service 注入controller中。

DAO java代码如下:

public class UserDao {

    public void userDao(){
        System.out.println("this is the userDao test!");
    }
}

Service java代码如下:

public interface UserService {

    public void getUser();
}
public class UserServiceImpl implements UserService{

    private UserDao userDao;
    
    
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }


    @Override
    public void getUser() {
//        System.out.println("i am beppe!!");
        userDao.userDao();
    }

    
}

CONTROLLER 层的代码如下:

public class UserController {

    private UserService userService;
    
    private User user;
    
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    

    public void setUser(User user) {
        this.user = user;
    }



    public void getUser(){
        userService.getUser();
        System.out.println(user.getName());
    }
}

在spring容器中定义这些需要被spring容器管理的对象:本例用的是xml文件来配置;

1:xml文件的schema头:

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

beans 的schema头文件是必须的,contexxt头文件不是必须的;

配置相应的依赖以及在对象中注入其他对象:说明:<bean> 标签表示将对象注册在spring容器中,id:该对象在容器中的唯一表示,class属性:指定对象的具体的java类;

<property> 对象需要注入的属性;name:属性名称;需要和对象中set方法中的属性一致,如:

在  UserController中需要注入 UserService ,那么则需要在 UserController类中维护 UserService 的set方法,而property 中的name属性的名称和set方法一致

ref 属性:引用需要注入的对象;

区别:ref属性和value属性:ref属性:应用对象,value属性:注入值;

private UserService userService;
 public void setUserService(UserService userService) {
        this.userService = userService;
    }
 
<bean id="userController" class="bz.beppe.controller.UserController">
          
          <property name="userService" ref="userService"></property>
          <property name="user" ref="user"></property>
      </bean>
      
      <bean id="userService" class="bz.beppe.serviceImpl.UserServiceImpl">
          <property name="userDao" ref="userDao"></property> 
      </bean> 
      
      <bean id="userDao" class="bz.beppe.dao.UserDao"></bean> 
      <bean id="user" class="bz.beppe.entity.User">
          <property name="name" value="beppe"></property> 
      </bean>

测试代码:

public static void main(String[] args) {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-context.xml");//手动开启spring容器
        UserController bean = (UserController)context.getBean("userController");  //通过id获取相应的对象
        bean.getUser();
    }

现在项目中一般都用注解的方式来配置spring,其中的配置原理和xml的原理一致;具体如何配置这里就不一一描述了;

原文地址:https://www.cnblogs.com/beppezhang/p/6547992.html