不带注解的自动装配@Autowired
Autowire without annotation @Autowired
我正在查看工作区中的一些旧示例。我看不出怎么样
自动装配完成,因为没有 @Autowired。 Spring开机+facebook默认配置。
@Controller
@RequestMapping("/")
public class HelloController {
private Facebook facebook;
private ConnectionRepository connectionRepository;
public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
this.facebook = facebook;
this.connectionRepository = connectionRepository;
}
@GetMapping
public String helloFacebook(Model model) {
System.out.println("we are here!!!");
if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
return "redirect:/connect/facebook";
}
PagedList<Post> feed = facebook.feedOperations().getFeed();
model.addAttribute("feed", feed);
return "hello";
}
}
它工作得很好,但是这些 bean 如何在没有 @Autowired 的情况下自动装配自己? 它们是作为字段还是在构造函数中自动装配的?
使用 spring 引导 1.4+ 构造函数会自动自动装配
在这个控制器中你没有给出任何注解@Autowired,可能你在控制器中有接口所以你不需要给出注解并且进一步检查服务层@Service注解是否存在你还没有给@Service 你也不能使用@Autowired。
我正在查看工作区中的一些旧示例。我看不出怎么样 自动装配完成,因为没有 @Autowired。 Spring开机+facebook默认配置。
@Controller
@RequestMapping("/")
public class HelloController {
private Facebook facebook;
private ConnectionRepository connectionRepository;
public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
this.facebook = facebook;
this.connectionRepository = connectionRepository;
}
@GetMapping
public String helloFacebook(Model model) {
System.out.println("we are here!!!");
if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
return "redirect:/connect/facebook";
}
PagedList<Post> feed = facebook.feedOperations().getFeed();
model.addAttribute("feed", feed);
return "hello";
}
}
它工作得很好,但是这些 bean 如何在没有 @Autowired 的情况下自动装配自己? 它们是作为字段还是在构造函数中自动装配的?
使用 spring 引导 1.4+ 构造函数会自动自动装配
在这个控制器中你没有给出任何注解@Autowired,可能你在控制器中有接口所以你不需要给出注解并且进一步检查服务层@Service注解是否存在你还没有给@Service 你也不能使用@Autowired。