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.

当Spring生成所有的Bean后,发现这个注解,就会在现在有的Bean中查找,找到对应的bean,将class定义好的bean注入到这个注解下的bean中,完成依赖注入。

@ComponentScan

1
2
3
4
5
6
7
package com.chap10.annotation.service;

import com.chap10.annotation.pojo.Role;

public interface RoleService {
public void printRoleInfo(Role role);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.chap10.annotation.service.impl;

import com.chap10.annotation.pojo.Role;
import com.chap10.annotation.service.RoleService;
import org.springframework.stereotype.Component;

@Component
public class RoleServiceImpl implements RoleService {
@Override
public void printRoleInfo(Role role) {
System.out.println("id = " + role.getId());
System.out.println("roleName = " + role.getRoleName());
System.out.println("note= " + role.getNote());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.chap10.main;

import com.chap10.annotation.config.ApplicationConfig;
import com.chap10.annotation.pojo.Role;
import com.chap10.annotation.service.RoleService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

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();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.chap10.annotation.service.impl;

import com.chap10.annotation.pojo.Role;
import com.chap10.annotation.service.RoleService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class RoleServiceImpl2 implements RoleService2 {

@Autowired
private Role role = null;

public Role getRole() {
return role;
}

public void setRole(Role role) {
this.role = role;
}

@Override
public void printRoleInfo() {
System.out.println("id = " + role.getId());
System.out.println("roleName = " + role.getRoleName());
System.out.println("note= " + role.getNote());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.chap10.main;

import com.chap10.annotation.config.ApplicationConfig;
import com.chap10.annotation.service.RoleService2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainAutowired {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ApplicationConfig.class);

RoleService2 roleService2 = context.getBean(RoleService2.class);
roleService2.printRoleInfo();
context.close();
}
}

Using @Autowired is recommended.