今天试着自己搞了个springboot项目玩玩,结果启动报错了,特地记录一下自己的知识盲点
报错如下
代码贴上
controller
package com.zou.demo.controller;
import com.zou.demo.po.User;
import com.zou.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private UserService userService;
@RequestMapping(name = "getNameMapping",value = "getName")
public List<User> getName(@RequestParam(required = true,value = "userId") Integer id){
return userService.getUsers(id);
}
}
接口
package com.zou.demo.service;
import com.zou.demo.po.User;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface UserService {
public List<User> getUsers(Integer id);
}
实现
package com.zou.demo.service.impl;
import com.zou.demo.po.User;
import com.zou.demo.service.UserService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements UserService {
@Override
public List<User> getUsers(Integer id) {
ArrayList<User> users = new ArrayList<>();
users.add(new User(1,"xiaoming",13,"敲代码"));
return users;
}
}
通过代码可以看出来,我对autowired注解不清楚。autowired注解是按类型注入,因此controller层注入的类型要改为具体实现类的类型,修改后如下
报错没有发现类型为UserServiceImpl类型的bean,所以需要给实现加@Service注解。接口上就不用加了
package com.zou.demo.service.impl;
import com.zou.demo.po.User;
import com.zou.demo.service.UserService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Override
public List<User> getUsers(Integer id) {
ArrayList<User> users = new ArrayList<>();
users.add(new User(1,"xiaoming",13,"敲代码"));
return users;
}
}
package com.zou.demo.service;
import com.zou.demo.po.User;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
public List<User> getUsers(Integer id);
}
如果想按名称注入怎么实现?可以使用@Resource注解,类型为接口类型,变量名称默认为要注入的实现类名称首字母小写,代码如下
package com.zou.demo.controller;
import com.zou.demo.po.User;
import com.zou.demo.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Resource
private UserService userServiceImpl;
@RequestMapping(name = "getNameMapping",value = "getName")
public List<User> getName(@RequestParam(required = true,value = "userId") Integer id){
return userServiceImpl.getUsers(id);
}
package com.zou.demo.service.impl;
import com.zou.demo.po.User;
import com.zou.demo.service.UserService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Override
public List<User> getUsers(Integer id) {
ArrayList<User> users = new ArrayList<>();
users.add(new User(1,"xiaoming",13,"敲代码"));
return users;
}
}
也可以自定义要注入bean名称,实现类的Service注解value属性值为bean在spring容器中注入的名称
package com.zou.demo.service.impl;
import com.zou.demo.po.User;
import com.zou.demo.service.UserService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service("userService01")
public class UserServiceImpl implements UserService {
@Override
public List<User> getUsers(Integer id) {
ArrayList<User> users = new ArrayList<>();
users.add(new User(1,"xiaoming",13,"敲代码"));
return users;
}
}
controller层使用@Resource注解,name属性值为要注入的bean名称,如果要注入的bean设置了自定义名称,就不会使用默认名称。
package com.zou.demo.controller;
import com.zou.demo.po.User;
import com.zou.demo.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Resource(name = "userService01")
private UserService userServiceImpl;
@RequestMapping(name = "getNameMapping",value = "getName")
public List<User> getName(@RequestParam(required = true,value = "userId") Integer id){
return userServiceImpl.getUsers(id);
}
}