Major difference: if @ComponentScan, manually configure(use another class with @ComponentScan to tell Spring IoC container where beans are to inject resources into). If @Autowired, no such class and manually injection are needed.
public class MainComponentScan { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Role role = context.getBean(Role.class); RoleService roleService = context.getBean(RoleService.class); roleService.printRoleInfo(role); context.close(); } }
@Autowired
1 2 3 4 5
package com.chap10.annotation.service;
public interface RoleService2 { public void printRoleInfo(); }
public class MainAutowired { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);