在src目录下创建dao包
在dao包下创建接口TestDIDao

package dao;

public interface TestDIDao {
    public void sayHello();
}

创建实现类TestDIDaoImpl

package dao;

public class TestDIDaoImpl implements TestDIDao {
    @Override
    public void sayHello(){
        System.out.println("TestDI");
    }
}

在src目录下创建service包
在service包下创建接口TestDIService

package service;

public interface TestDIService {
    public void sayHello();
}

创建实现类TestDIServiceImpl

package service;

import dao.TestDIDao;

public class TestDIServiceImpl implements TestDIService {
    private TestDIDao testDIDao;

    public void setTestDIDao(TestDIDao testDIDao) {
        this.testDIDao = testDIDao;
    }

    @Override
    public void sayHello() {
        testDIDao.sayHello();
        System.out.println("构造方法注入");
    }
}

创建spring配置文件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="testDIDao" class="dao.TestDIDaoImpl"/>
    <bean id="testDIService" class="service.TestDIServiceImpl">
        <property name="testDIDao" ref="testDIDao"/>
    </bean>
</beans>

在test包下创建TestDI类

package test;

import service.TestDIService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDI {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestDIService testDIService= (TestDIService) ac.getBean("testDIService");
        testDIService.sayHello();
    }
}

结果

4月 06, 2019 11:01:38 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b4523f8: startup date [Sat Apr 06 11:01:38 CST 2019]; root of context hierarchy
4月 06, 2019 11:01:38 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
TestDI
构造方法注入