# 使用注解方式实现Spring IoC
# 14-自动装配与Autowired
两类自动装配注解
分类 | 注解 | 说明 |
---|---|---|
按类型装配 | @Autowired | 按容器内对象类型动态注入属性,由Spring机构提供 |
@Inject | 基于JSR-330(Dependency Injection for java)标准,其他同@Autowired,但不支持required属性 | |
按名称装配 | @Named | 与@Inject配合使用,JSR-330规范,按属性名自动装配属性 |
@Resource | 基于JSR-250规范,优先按名称、再按类型智能匹配 |
# 按类型装配-示例代码一
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
package com.torey.spring.ioc.controller;
import org.springframework.stereotype.Controller;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/30 20:25
* @描述:
*/
@Controller
public class UserController {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.torey.spring.ioc.dao;
import org.springframework.stereotype.Repository;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/30 20:06
* @描述:
*/
//组件类型注解默认beanId为类名首字母小写,@Repository : beanId=userDao
//也可以指定beanId=@Repository("user"), beanId=user
@Repository
public class UserDao{
public UserDao() {
System.out.println(this.getClass().getName() + "正在被创建!");
}
public void getById(Long id){
System.out.println(this.getClass().getName() + ".getById()");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.torey.spring.ioc.service;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/30 20:22
* @描述:
*/
import com.torey.spring.ioc.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public UserService() {
System.out.println(this.getClass().getName() + "正在被创建!");
}
@Autowired
//如果装配注解放在set方法上,则自动按类型/名称对set方法参数进行注入
//如果放在属性上,则不再执行set方法
private UserDao userDao;
//如果装配注解放在set方法上,则自动按类型/名称对set方法参数进行注入,实际项目这种一般不使用
//如果放在属性上,则不再执行set方法
// @Autowired
// public void setUserDao(UserDao userDao) {
// System.out.println(this.getClass().getName() + ".setUserDao()");
// this.userDao = userDao;
// }
public void getById(Long id){
System.out.println(this.getClass().getName() + ".getById()");
userDao.getById(id);
}
public UserDao getUserDao() {
return userDao;
}
}
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
29
30
31
32
33
34
35
36
37
38
39
40
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
29
30
31
32
33
34
35
36
37
38
39
40
package com.torey.spring.ioc.util;
import org.springframework.stereotype.Component;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/30 20:27
* @描述:
*/
@Component("stringUtils")
public class StringUtils {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--在IoC容器初始化时自动扫描四种组件类型注解并完成实例化
@Repository @Service @Controller @Component
-->
<context:component-scan base-package="com.torey.spring.ioc.*"></context:component-scan>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
package com.torey.spring.ioc;
import com.torey.spring.ioc.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/30 20:13
* @描述:
*/
public class SpringApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.getById(5L);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 一个接口有多个实现类的解决方法
按类型注入时,当一个接口有多个实现类时的解决方法如下:
- 将另外一个实现类的@Repository注解去掉
- 在其中一个实现类中加上@Primary注解
package com.torey.spring.ioc.dao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/30 20:06
* @描述:
*/
//组件类型注解默认beanId为类名首字母小写,@Repository : beanId=userDao
//也可以指定beanId=@Repository("user"), beanId=user
@Repository
public class UserDao implements IUserDao{
public UserDao() {
System.out.println(this.getClass().getName() + "正在被创建!");
}
public void getById(Long id){
System.out.println(this.getClass().getName() + ".getById()");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.torey.spring.ioc.dao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/30 20:06
* @描述: @Primary: 如果容器中出现两个实现类,标注@Primary的类可以被使用
*/
@Repository
@Primary
public class UserOracleDao implements IUserDao{
public UserOracleDao() {
System.out.println(this.getClass().getName() + "正在被创建!");
}
public void getById(Long id){
System.out.println(this.getClass().getName() + ".getById()");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.torey.spring.ioc.service;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/4/30 20:22
* @描述:
*/
import com.torey.spring.ioc.dao.IUserDao;
import com.torey.spring.ioc.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public UserService() {
System.out.println(this.getClass().getName() + "正在被创建!");
}
@Autowired
private IUserDao userDao;
public void getById(Long id){
System.out.println(this.getClass().getName() + ".getById()");
userDao.getById(id);
}
}
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
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