在src目录下创建annotation包
在annotation包下创建dao包
在dao包下创建接口TestDao

package annotation.dao;

public interface TestDao {
    public void save();
}

创建实现类TestDaoImpl

package annotation.dao;

import org.springframework.stereotype.Repository;

@Repository("testDao")
public class TestDaoImpl implements TestDao {
    @Override
    public void save() {
        System.out.println("test Dao save");
    }
}

在annotation包下创建service包
在service包下创建接口TestService

package annotation.service;

public interface TestService {
    public void save();
}

创建实现类TestServiceImpl

import annotation.dao.TestDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("testService")
public class TestServiceImpl implements TestService {
    @Autowired
    private TestDao testDao;

    @Override
    public void save() {
        testDao.save();
        System.out.println("test service save");
    }
}

在annotation包下创建controller包
在controller包下创建类TestController

package annotation.controller;

import annotation.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class TestController {
    @Autowired
    private TestService testService;
    public void save(){
        testService.save();
        System.out.println("test controller save");
    }
}

创建spring配置文件annotationContext.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: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">
    <!--配置自动扫描annotation包下的类-->
    <context:component-scan base-package="annotation"/>
</beans>

在test包下创建TestMoreAnnotation类

package test;

import annotation.controller.TestController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMoreAnnotation {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("annotationContext.xml");
        TestController testController=(TestController) ac.getBean("testController");
        testController.save();

    }
}

结果

4月 06, 2019 11:49:50 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5a8e6209: startup date [Sat Apr 06 11:49:50 CST 2019]; root of context hierarchy
4月 06, 2019 11:49:50 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [annotationContext.xml]
test Dao save
test service save
test controller save