运行函数:
@Autowired在这里获取bean
package com.exa.de.controller; import com.exa.de.entity.User; import com.exa.de.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** * @RestController注解,相当于@Controller+@ResponseBody两个注解的结合 * 返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面 */ @RestController @RequestMapping("action") public class UserActionlController { @Autowired private UserService userService; @GetMapping//url://http://localhost:8080/action public User get(){ return userService.get(); } @GetMapping("{id}")//url://http://localhost:8080/action/id(一个int类型) @CrossOrigin//跨域处理 public User getById(@PathVariable("id")int id){ return userService.getById(id); } }
2.实现类,
@Service注册bean,在实现类中不是接口
package com.exa.de.service.impl;
import com.exa.de.entity.User;
import com.exa.de.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
@Service
public class UserServiceImpl implements UserService {//implements继承:实现类UserServiceImpl 继承implements 接口UserService
public User get() {
User user = new User(1, "lin", "男", 25);
System.out.println("get user =" + user);
return user;
}
public User getById(@PathVariable("id")int id){
User user = new User(id,"huang","nv",18);
System.out.println("getById user"+user);
return user;
}
}
3.接口
package com.exa.de.service; import com.exa.de.entity.User; public interface UserService { public User get(); public User getById(int id); }
结果


@Service和@Component都是获取bean,用途差别不大

@Resource和@Altowired+@Qualifier
如果接口实现只有一个,那么用@Autowired就可以了,也不需要指定名字,
如果接口有多个实现,那么,用@Resource,并指定name(建议)
或者使用导@Altowired+@Qualifier,@Qualifier的value
@Autowired @Qualifier("autowiredServiceImpl")//实现类的name首字母自动小写 private UserService userService; @Resource(name = "userServiceImpl") private UserService userService1;
@Configuration人工写一个bean
@Configuration public class MyBeans { @Bean public UserService autowired(){//假设bean 'autowired'被注册,就禁止重写,重命名一个bean return new AutowiredServiceImpl(); } @Bean public UserService user(){ return new UserServiceImpl(); } }
@Value从配置文件中取参数
application.properties:
view.page=view; local.username =guang;//名字,路径可以随便username也可以
local.userage=123456;
package com.exa.de.controller; import com.exa.de.entity.User; import com.exa.de.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @RestController @RequestMapping("action") public class UserActionlController { @Value("${view.page}") private String page; @Value("${local.username}") private String username; @Value("${local.userage}") private String userage; @GetMapping("val") public String getValue(){ System.out.println(); return "value{page="+page+",username="+username+"+,userage="+userage+"}"; } }
本文来自博客园,作者:阿霖找BUG,转载请注明原文链接:https://www.cnblogs.com/lin-07/articles/16197384.html
浙公网安备 33010602011771号