Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

jsf 2 - Are there going to be two instances for a bean if I write @managed bean annotation and define same in faces-config.xml

In my application in some places we are using @ManagedBean annoation for Person bean and for the same Person bean we defining in the faces-confing.xml like below at the same time.

@ManagedBean("name=person")
 @SessionScoped
 Public class Person{


}

faces-config.xml

<managed-bean>
     <managed-bean-name>person</managed-bean-name>
     <managed-bean-class>com.test.sample.Person</managed-bean-class>
     <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

my question is does this approach create two instances for the Person bean or it does matter if I do this? Does this have any effect on performance of my application If I do this for every Bean in my application?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a priority defined for this case. @ManagedBean annotation avoids having to configure an entry in faces-config.xml but, if you have both, the <managed-bean> entry overrides the annotation.

In your case, there'll be only one instance configured like your faces-config.xml entry. In your case, both approaches are configured the same way but, should you change your faces-config.xml entry to something like

<managed-bean>
     <managed-bean-name>personBean</managed-bean-name>
     <managed-bean-class>com.test.sample.Person</managed-bean-class>
     <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Your bean will be registered under personBean rather than person (which is the name defined by the annotation).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...