当前位置:网站首页>@Lookup
@Lookup
2022-04-21 10:40:00 【PS cool tutorial】
scene
In the use of Spring when , This may happen : A single example of Bean Depending on another non singleton Bean. If you simply use automatic assembly to inject dependencies , There may be some problems , As shown below :
The singleton Class A
@Component
public class ClassA {
@Autowired
private ClassB classB;
public void printClass() {
System.out.println("This is Class A: " + this);
classB.printClass();
}
}
Not a single case Class B
@Component
@Scope(value = SCOPE_PROTOTYPE)
public class ClassB {
public void printClass() {
System.out.println("This is Class B: " + this);
}
}
here Class A The default singleton is used scope, And depend on Class B, and Class B Of scope yes prototype, Therefore, it is not a single case , At this time, run a test to see the problem written like this :
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {
ClassA.class, ClassB.class})
public class MyTest {
@Autowired
private ClassA classA;
@Test
public void simpleTest() {
for (int i = 0; i < 3; i++) {
classA.printClass();
}
}
}
The output is zero :
This is Class A: ClassA@282003e1
This is Class B: ClassB@7fad8c79
This is Class A: ClassA@282003e1
This is Class B: ClassB@7fad8c79
This is Class A: ClassA@282003e1
This is Class B: ClassB@7fad8c79
You can see , Two kinds of Hash Code It's the same in all three outputs .Class A It is understandable that the value of is unchanged , Because it is singleton , however Class B Of scope yes prototype But also keep Hash Code unchanged , It seems to have become a single case ?
The reason for this is ,Class A Of scope By default singleton, therefore Context Only create Class A Of bean once , So there's only one chance to inject , The container can't give... Every time Class A Offer a new Class B.
A less good solution
To solve the above problems , It can be done to Class A Make some changes , Let it realize ApplicationContextAware.
@Component
public class ClassA implements ApplicationContextAware {
private ApplicationContext applicationContext;
public void printClass() {
System.out.println("This is Class A: " + this);
getClassB().printClass();
}
public ClassB getClassB() {
return applicationContext.getBean(ClassB.class);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
In this way, you can get to... Every time you need it Class B Go manually when Context Find a new one in bean. Run again and get the following output :
This is Class A: com.devhao.ClassA@4df828d7
This is Class B: com.devhao.ClassB@31206beb
This is Class A: com.devhao.ClassA@4df828d7
This is Class B: com.devhao.ClassB@3e77a1ed
This is Class A: com.devhao.ClassA@4df828d7
This is Class B: com.devhao.ClassB@3ffcd140
You can see Class A Of Hash Code Remain unchanged in three outputs , and Class B But it's different every time , It shows that the problem has been solved , Each call uses a new instance .
But this way of writing is similar to Spring Strong coupling ,Spring Provides another way to reduce invasiveness .
@Lookup
Spring A file named @Lookup Annotations , This is a comment on the method , The method marked by it will be rewritten , Then according to the type of its return value , Container call BeanFactory Of getBean() Method to return a bean.
@Component
public class ClassA {
public void printClass() {
System.out.println("This is Class A: " + this);
getClassB().printClass();
}
@Lookup
public ClassB getClassB() {
return null;
}
}
You can find a lot of simplicity , And no longer with Spring Strong coupling , Running the test again can still get the correct output .
The return value of the marked method is no longer important , Because the container will dynamically generate a subclass and then rewrite the annotated method / Realization , The final call is the subclass method .
The use of @Lookup The method needs to comply with the following signature :
<public|protected> [abstract] theMethodName(no-arguments);
ScopedProxyMode
You can also use a proxy by specifying
@Component
@Scope(value = "prototype",proxyMode= ScopedProxyMode.TARGET_CLASS)
public class ClassB {
public void printClass() {
System.out.println("This is Class B: " + this);
}
}
版权声明
本文为[PS cool tutorial]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211032149136.html
边栏推荐
- Mieux que SQL, un autre langage de base de données domestique est né
- 塔米狗知识|上市公司收购的基本原则
- MKL and vs2019 configuration method
- JS初练——弹弹球与墙壁碰撞处理实例
- Jeecgboot: the difference between online form control drop-down box and drop-down search box
- Digital economy - new economy index (2017-2022) & two dimensional indicators of digital economy calculation of 31 provinces (2013-2020)
- torch. autograd. Function customization
- Talk about your GC tuning ideas?
- 比SQL还好用,又一门国产数据库语言诞生了
- Impact of AOT and single file release on program performance
猜你喜欢

Filebeat收集日志数据传输到Redis,通过Logstash来根据日志字段创建不同的ES索引

Can Jingdong Logistics, ririshun supply chain and Shunfeng find the optimal solution of logistics under the "epidemic"?

SAP ABAP FOR ALL ENTRIES 的用法

基于润和大禹开发板的导购系统项目方案

Opencv -- template matching

Impact of AOT and single file release on program performance

Tami dog knowledge | basic principles of acquisition of listed companies

AcWing 1749. 阻挡广告牌 II(分类讨论+枚举)

IDEA和PyCharm启动时进入欢迎界面

摩尔线程与Ampere Computing达成合作
随机推荐
[software test series IX] description of matters to be provided for pressure test application
信号与系统2022春季学期:作业内容与参考答案
Enter the welcome interface when idea and pycharm are started
比SQL還好用,又一門國產數據庫語言誕生了
Where is London gold safe to open an account?
Uniapp wechat applet clicks the button to call wechat payment
移动通信平台的搭建(可语音可视屏可收发短信)
uniapp 微信小程序 点击按钮调用微信支付
TypeError: The view function did not return a valid response. The function either returned none
zsh: segmentation fault 解决方法
Nanny level tutorial on building personal home page (I)
Android learning ① various reasons why Android cannot connect to MySQL database and Solutions
torch. autograd. Function customization
以用户体验五要素的思路,如何编写产品需求文档(PRD)
C语言中使用scanf函数时应注意的问题
比SQL还好用,又一门国产数据库语言诞生了
递归函数 C语言 题型
2022 information and future preparation 1 new online judge 1112: plane segmentation
canvas 学习笔记
2022 information and future preparation 3 new online judge 1059: string sorting