4.Spring依赖注入

目录

一、为什么要有依赖注入?

二、Bean的依赖注入方法

set方法

构造方法

三、Bean依赖注入的数据类型

引用数据类型

普通数据类型

集合数据类型


一、为什么要有依赖注入?

如果没有依赖注入,controller调用service,service在自己里面调用dao。也就是说,controller每次调用一次service,service都会在他内部再拼装一次dao,即controller和dao耦合了

 

所谓依赖注入,就是把在service中调用getBean()获取dao的这一步不要再代码体现,而是在容器体现

即,我们程序员不管dao层对象,我们只关心service层。dao层交给spring容器

 

二、Bean的依赖注入方法

set方法

 

UserDao.java

package com.xupeng.dao;

public interface UserDao {
    public void save();
}

 

UserDaoImpl.java

package com.xupeng.dao;

public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("save running...");
    }
}

 

UserService:

package com.xupeng.service;

public interface UserService {
    public void save();
}

 

UserServiceImpl:

package com.xupeng.service.impl;

import com.xupeng.dao.UserDao;
import com.xupeng.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

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

    public void save() {
        userDao.save();
    }
}

 

applicationContext.xml:

这个ref里的userDao表示的是userDao这个bean对象,name里的userDao表示的是将ref里的对象通过setUserDao方法赋值

  <bean id="userDao" class="com.xupeng.dao.UserDaoImpl"></bean>

    <bean id="userService" class="com.xupeng.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

 

Controller:

package controller;

import com.xupeng.dao.UserDao;
import com.xupeng.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Controller {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.save();
    }
}

 

注:在applicationContext.xml里<property>属性写的有点烦,我们可以简化:

命名空间添加 xmlns:p="http://www.springframework.org/schema/p"

属性写法变成这样 p:userDao-ref="userDao"

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

    <bean id="userDao" class="com.xupeng.dao.UserDaoImpl"></bean>

    <bean id="userService" class="com.xupeng.service.impl.UserServiceImpl" p:userDao-ref="userDao">
    </bean>


</beans>

 

构造方法

UserServiceImpl:

package com.xupeng.service.impl;

import com.xupeng.dao.UserDao;
import com.xupeng.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserServiceImpl(UserDao userDao){
        this.userDao = userDao;
    }

    public UserServiceImpl(){
    }


    public void save() {
        userDao.save();
    }
}

 

applicationContext.xml

注:此处service里的name表示的是构造函数给定的参数,ref还是表示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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="userDao" class="com.xupeng.dao.UserDaoImpl"></bean>

    <bean id="userService" class="com.xupeng.service.impl.UserServiceImpl">
        <constructor-arg name="userDao" ref="userDao"></constructor-arg>
    </bean>

</beans>

 

三、Bean依赖注入的数据类型

引用数据类型

之前我们的都是引用数据类型

 

普通数据类型

我们dao对象会有一些普通数据,这是很正常的,这怎么让容器来做呢?

UserDaoImpl:
package com.xupeng.dao;

public class UserDaoImpl implements UserDao {
    private String username;
    private int age;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void save() {
        System.out.println(username + "====" + age);
        System.out.println("save running...");
    }
}

 

applicationContext.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="userDao" class="com.xupeng.dao.UserDaoImpl">
        <property name="username" value="aa"></property>
        <property name="age" value="12"></property>
    </bean>

    <bean id="userService" class="com.xupeng.service.impl.UserServiceImpl">
        <constructor-arg name="userDao" ref="userDao"></constructor-arg>
    </bean>

</beans>

 

集合数据类型

 

User:

package com.xupeng.domain;

public class User {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

UserDaoImpl

package com.xupeng.dao;

import com.xupeng.domain.User;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class UserDaoImpl implements UserDao {
    private List<String> list;
    private Map<String, User> map;
    private Properties properties;

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMap(Map<String, User> map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void save() {
        System.out.println(list);
        System.out.println(map);
        System.out.println(properties);
        System.out.println("save running...");
    }
}

 

applicationContext.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="userDao" class="com.xupeng.dao.UserDaoImpl">
        <property name="list">
            <list>
                <value>aa</value>
                <value>bb</value>
                <value>cc</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="u1" value-ref="user1"></entry>
                <entry key="u2" value-ref="user2"></entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="p1">11</prop>
                <prop key="p2">22</prop>
            </props>
        </property>
    </bean>

    <bean id="user1" class="com.xupeng.domain.User">
        <property name="name" value="ff"></property>
        <property name="age" value="11"></property>
    </bean>
    <bean id="user2" class="com.xupeng.domain.User">
        <property name="name" value="gg"></property>
        <property name="age" value="22"></property>
    </bean>

    <bean id="userService" class="com.xupeng.service.impl.UserServiceImpl">
        <constructor-arg name="userDao" ref="userDao"></constructor-arg>
    </bean>

</beans>

 

©️2020 CSDN 皮肤主题: 点我我会动 设计师:上身试试 返回首页