/**
* @author pdai
*/@RepositorypublicclassUserDaoImpl{/**
* mocked to find user list.
*
* @return user list
*/publicList<User>findUserList(){returnCollections.singletonList(newUser("pdai",18));}}
packagetech.pdai.springframework.service;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importtech.pdai.springframework.dao.UserDaoImpl;importtech.pdai.springframework.entity.User;/**
* @author pdai
*/@ServicepublicclassUserServiceImpl{/**
* user dao impl.
*/@AutowiredprivateUserDaoImpl userDao;/**
* find user list.
*
* @return user list
*/publicList<User>findUserList(){return userDao.findUserList();}}
在App中扫描tech.pdai.springframework包
packagetech.pdai.springframework;importjava.util.List;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importtech.pdai.springframework.entity.User;importtech.pdai.springframework.service.UserServiceImpl;/**
* @author pdai
*/publicclassApp{/**
* main interfaces.
*
* @param args args
*/publicstaticvoidmain(String[] args){// create and configure beansAnnotationConfigApplicationContext context =newAnnotationConfigApplicationContext("tech.pdai.springframework");// retrieve configured instanceUserServiceImpl service = context.getBean(UserServiceImpl.class);// use configured instanceList<User> userList = service.findUserList();// print info from beans
userList.forEach(a ->System.out.println(a.getName()+","+ a.getAge()));}}