Dependency injection spring framework -DI (II)

A, set the injection method
1. Introduction

  • a. In the class to be injected, is defined: Some Private member variables, and generates a get method set Lee
  • b. In the configuration file, the class definition of the object bean, and bean tag, the tag write property, wherein a ref attribute name and assign, with reference to the specific meaning attribute class code comments
  • c. Test

2. The code shows
the first to write a UserDao Interface:

package com.hnpi.dao;

public interface UserDao {
    void test();
}

Implement this interface methods:

package com.hnpi.dao.impl;

import com.hnpi.dao.UserDao;

public class UserDaoImpl implements UserDao {

    @Override
    public void test() {
        System.out.println("Hello World!");
    }
}

In writing a UserService Interface:

package com.hnpi.service;

public interface UserService {
    void test2();
}

Implement this interface methods:

package com.hnpi.service.impl;

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

public class UserServiceImpl implements UserService {
    //定义UserDao接口变量
    private UserDao userDao;

    /**
     * UserService接口的方法
     */
    @Override
    public void test2(){
        System.out.println("这是UserService层");
        //调用UserDao接口中的方法
        userDao.test();
    }

    //get和set方法
    public UserDao getUserDao() {
        return userDao;
    }

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

    //无参构造
    public UserServiceImpl() {
        super();
    }

    //有参构造
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }
}

Then is the configuration file 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

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

    <bean id="userService" class="com.hnpi.service.impl.UserServiceImpl">
        <!--
            name:类中成员变量名;
            ref:引用IOC容器托管的bean对象;
            类中有多个成员变量名可以写多个<property></property>标签
        -->
        <property name="userDao" ref="userDao"/>

    </bean>
</beans>

The final step is the test class:

package com.hnpi.test;

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

public class Test {
    public static void main(String[] args) {
        //加载applicationContext.xml配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //UserService接口的对象,被spring的IOC容器所实例化
        UserService userService = (UserService) ac.getBean("userService");
        userService.test2();
    }
}

After the test, successful implementation of the interface in the call UserService test2 () method, and outputs the result:
Here Insert Picture Description
two, Constructor injection
only in applicationContext.xml add a label file:

<?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.hnpi.dao.impl.UserDaoImpl"></bean>

    <bean id="userService" class="com.hnpi.service.impl.UserServiceImpl">
        <!--
            name:构造方法中成员变量名;
            ref:引用IOC容器托管的bean对象;
            构造方法中有多个成员变量名可以写多个<constructor-arg></constructor-arg>标签
        -->
        <constructor-arg name="userDao" ref="userDao"/>
    </bean>
</beans>

Then also the normal output.

Published 36 original articles · won praise 7 · views 2067

Guess you like

Origin blog.csdn.net/q_2540638774/article/details/103695176