服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - 深入理解java的spring-ioc的使用

深入理解java的spring-ioc的使用

2021-07-21 11:46张占岭(仓储大叔,Lind) Java教程

这篇文章主要介绍了java的spring-ioc的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

spring-ioc的使用

ioc容器在很多框架里都在使用,而在spring里它被应用的最大广泛,在框架层面上,很多功能都使用了ioc技术,下面我们看一下ioc的使用方法。

  1. 把服务注册到ioc容器
  2. 使用属性注入反射对应类型的实例
  3. 多态情况下,使用名称反射类型的实例

把服务注册到ioc容器

@bean注册组件
使用@bean注解进行类型的注册,默认你的ioc容器里类型为bean的返回值,名称为bean所有的方法名,与你的包名称没有直接关系,如果你的接口有多种实现,在注册时可以使用@bean("lind")这种方式来声明。

@component,@configuration,service,repository注册组件

这几个注解都是在类上面声明的,而@bean是声明在方法上的,这一点要注意,这几个注解一般是指对一个接口的实现,在实现类上加这些注解,例如,一个数据仓储接口userrepository,它可以有多种数据持久化的方式,如sqluserrepositoryimpl和mongouserrepositoryimpl,那么在注册时你需要为他们起一个别名,如@repository("sql-userrepositoryimpl) qluserrepositoryimpl,默认的名称是类名,但注意类名首字母为小写

?
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
41
public interface emaillogservice {
 void send(string email, string message);
}
 
@component()
public class emaillogservicehttpimpl implements emaillogservice {
 private static final logger logger = loggerfactory.getlogger(emaillogservicehttpimpl.class);
 
 @override
 public void send(string email, string message) {
 assert.notnull(email, "email must not be null!");
 logger.info("send email:{},message:{}", email, message);
 }
}
@component("email-socket")
public class emaillogservicesocketimpl implements emaillogservice {
 private static final logger logger = loggerfactory.getlogger(emaillogservicesocketimpl.class);
 
 @override
 public void send(string email, string message) {
 assert.notnull(email, "email must not be null!");
 logger.info("send email2:{},message:{}", email, message);
 }
}
// 看一下调用时的测试代码
 @resource(name = "email-socket")
 emaillogservice socketemail;
 @autowired
 @qualifier( "emaillogservicehttpimpl")
 emaillogservice httpemail;
 
 @test
 public void testioc2() {
 socketemail.send("ok", "ok");
 }
 
 
 @test
 public void testioc1() {
 httpemail.send("ok", "ok");
 }

在程序中使用bean对象

1.使用resource装配bean对象
在通过别名调用bean时,你可以使用@resource注解来装配对象

2.使用@autowired装配bean对象
也可以使用 @autowired
@qualifier( "emaillogservicehttpimpl")两个注解去实现程序中的多态

使用场景

在我们有些相同行为而实现方式不同的场景中,如版本1接口与版本2接口,在get方法实现有所不同,而这
两个版本都要同时保留,这时我们需要遵守开闭原则,扩展一个新的接口,而在业务上对代码进行重构,
提取两个版本相同的方法到基类,自己维护各自独有的方法,在为它们的bean起个名字,在装配时,通过
bean的名称进行装配即可。

写个伪代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class api_version1(){
@autowired
@qualifier("print-version1")
printservice printservice;
}
 
class api_version2(){
@autowired
@qualifier("print-version2")
printservice printservice;
}
 
class baseprintservice{}
 
interface printservice{}
 
@service("print-version1")
class printserviceimplversion1 extends baseprintservice implements printservice{}
 
@service("print-version2")
class printserviceimplversion2 extends baseprintservice implements printservice{}

以上所述是小编给大家介绍的java的spring-ioc的使用详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/lori/p/10512402.html

延伸 · 阅读

精彩推荐