
1. Component
/springboot 假设是一个容器 没有使用@Component 这个类就没有进入springboot容器
@Component
//现在已经将这个类注入到 springboot了 现在可以被使用了 可以在其他的类中使用
public class questionServiceImpl implements questionservice {
}
2. Autowired
Autowired 用于将一个bean 注入到其他bean
@RestController //Spring会转换返回值并将日写入http相应
//@RequestMapping("get") //用于类和防范,在方法级别时, 处理http的各种防范
public class getListController {
@Autowired //向这个bean中注入其他bean questionServiceImpl
private questionServiceImpl questionServiceimpl;
@GetMapping("get")
public String get(){
return "hello get";
}
@Component
//现在已经将这个类注入到 springboot了 现在可以被使用了 可以在其他的类中使用
public class questionServiceImpl implements questionservice {
@Override
public String getQuestion3(questionDto question){
System.out.println(question.getQuestionName());
System.out.println(question.getQuestionType());
return question.getQuestionName()+" "+question.getQuestionName();
}
}
@Autowired //向这个bean中注入其他bean questionServiceImpl
private questionServiceImpl questionServiceimpl;
@PostMapping("getquestion4impl")
public String getQuestion3(@RequestBody questionDto q){
return "getquestion4impl >>"+ questionServiceimpl.getQuestion3(q);
}

3. @Repository
4. @Service
//springboot 假设是一个容器 没有使用@Component 这个类就没有进入springboot容器
//@Component
@Service //用于service的bean 是Component的子层
//现在已经将这个类注入到 springboot了 现在可以被使用了 可以在其他的类中使用
public class questionServiceImpl implements questionservice {
@Override
public String getQuestion3(questionDto question){
System.out.println(question.getQuestionName());
System.out.println(question.getQuestionType());
return question.getQuestionName()+" "+question.getQuestionName();
}
}
5. Configuration
用于声明springboot的配置文件类
6. Value(${key})
@Value("${config.key}") //如果没有${} 就会将config.key 赋值给key
private String key;
@GetMapping("get")
public String get(){
return "hello get"+key;
}

网友评论