SpringIOC 容器注入方式

SpringIOC 依赖注入的方式

手动注入

  1. set 方式注入
  2. 构造器注入

set方式注入

需求: 想要在 UserService 这个类中 使用UserDao 的方法

定义UserService.java

public class UserService {


    /**
     * SpringIOC 容器注入方式:
     * set方式注入
     * 1) 在需要注入的类中加入setter方法
     * 2) 在配置文件中加入property 属性,ref依赖 被注入的对象
     */
    /**
     * java bean注入
     */
    private UserDao userDao;

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

    /**
     * 基本类型注入
     */
    private String ip;

    public void setIp(String ip) {
        this.ip = ip;
    }

    /**
     * 注入list 类型
     */
    private List<String> list;

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

    private Set<String> set;

    public void setSet(Set<String> set) {
        this.set = set;
    }

    private Map<String, Object> maps;

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    private Properties properties;

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

    public void printList() {
        list.stream().forEach(e -> {
            e = e.toUpperCase();
            System.out.println(e);
        });
    }

    /**
     * set
     */
    public void printSet() {
        set.stream().forEach(e -> {
            e = e.toUpperCase();
            System.out.println(e);
        });
    }

    public void printMap() {
        maps.forEach((k, v) -> System.out.println(k + "=" + v));
    }

    public void printProperties() {
        properties.forEach((k, v) -> System.out.println(k + "=" + v));
    }

    public void test() {
        userDao.method01(ip);
        printList();
        printSet();
        printMap();
        printProperties();
    }
}

定义 UserDao.java

public class UserDao {

    public void method01(String ip) {
        System.out.println("UserDao    test... " + ip);

    }
}

定义Spring.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">

    <!-- SpringIOC 依赖注入   -->
    <!-- set方式注入-->
    <bean id="userService" class="org.wlc.service.UserService">
        <property name="userDao" ref="userDao"></property>
        <property name="ip" value="192.168.83.129"></property>
        <!--        注入list 属性-->
        <property name="list">
            <list>
                <value>上海</value>
                <value>天津</value>
                <value>a</value>
                <value>b</value>
            </list>

        </property>

        <!--        注入set 属性-->
        <property name="set">
            <set>
                <value>上海</value>
                <value>天津</value>
                <value>a</value>
                <value>b</value>
            </set>

        </property>
        <!--        注入map属性-->
        <property name="maps">
            <map>
                <entry>
                    <key>
                        <value>周杰伦</value>
                    </key>
                    <value>稻香</value>
                </entry>
                <entry>
                    <key>
                        <value>张杰</value>
                    </key>
                    <value>少年</value>
                </entry>
            </map>

        </property>

        <!--        properties属性配置文件-->
        <property name="properties">
            <props>
                <prop key="bj">北京</prop>
                <prop key="sh">上海</prop>
            </props>
        </property>

    </bean>

    <bean id="userDao" class="org.wlc.dao.UserDao"></bean>
</beans>

测试

public class App {
    private static ApplicationContext context ;

    static {
        context = new ClassPathXmlApplicationContext("Spring.xml");
    }

    public static void main(String[] args) {
        method02();
    }

    /**
     * 手动注入: set方式注入
     */
    public static void method02(){
        UserService userService = (UserService) context.getBean("userService");
        userService.test();
    }
    /**
     *
     * 手动注入: 构造器注入方式注入
     */
    public void method01(){

        UserService2 userService = (UserService2) context.getBean("userService2");
        userService.test();

        UserDao2 userDao2 = (UserDao2) context.getBean("userDao2");
        userDao2.method01();
    }
}

构造器注入

自动注入

上一篇:Spring快速入门


下一篇:阿里云安装nginx 和 php-fpm