spring关于@Service注解的一点理解

关于spring注解的一点理解。
@Service 把类当做容器中的一个组件来使用。
当使用@Autowired注解则是实例化构造器。因为在自动注入时,是一个接口类型,所以要在容器中找到相应的实现类注入。故@Service加到类上。

@Service
public interface UserService {
   User login(String username,String password); 
}
//当把注解写在接口上时,spring容器会注入失败。


//注解写在类上  注入不会失败。
@Service
public class UserServiceImpl  implements UserService{
       @Autowired
       private  UserMapper userMapper;
}


@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService
}

你可能感兴趣的:(spring关于@Service注解的一点理解)