Tuesday 15 June 2010

java - How to exactly work the Spring Inheritance-based Proxies configuration? -



java - How to exactly work the Spring Inheritance-based Proxies configuration? -

i studying spring core certification , finding doubts related proxying notion.

so on study material find next quiz:

there java configuration class contains next methods:

@bean public accountrepository accountrepository(){ homecoming new jdbcaccountrepository(); } @bean public transferservice transferservice1() { transferserviceimpl service = new transferserviceimpl(); service.setaccountrepository(accountrepository()); homecoming service; } @bean public transferservice transferservice2() { homecoming new transferserviceimpl( new jdbcaccountrepository() ); }

as can see there 2 different implementation of transferservice() respectively named transferservice1() , transferservice2() create , homecoming transferserviceimpl object.

the first 1 creatre new transferserviceimpl object , phone call setaccountrepository() method on it.

the sec 1 create transferserviceimpl passing new jdbcaccountrepository object constructor.

it inquire me **which best implementation brween previous 2 methods?

and reply provided is: prefer phone call dedicated method. think best way first implementation.

it explain accountrepository bean singleton (because standard scope beans in spring) jdbcaccountrepository() called twice or more times (for illustration in previous code snippet called when called methods transferservice1() , transferservice2() , if problem because accountrepository have singleton.

is true? or missing something?

so gather @ startup time foreach configuration class (annotated @configuration) created child class extends configuration class.

for illustration if have next configuration class:

@configuration public class appconfig { @bean public accountrepository accountrepository() { ... } @bean public transferservice transferservice() { ... } }

it automatically created next class extends appconfig:

public class appconfig$$enhancerbycglib$ extends appconfig { public accountrepository accountrepository() { // ... } public transferservice transferservice() { // ... } ... ... ... }

so child class entry point (the method called definied in kid class) , pseudocode this:

public class appconfig$$enhancerbycglib$ extends appconfig { public accountrepository accountrepository() { // if bean in applicationcontext homecoming bean // else phone call super.accountrepository() , store bean in context } public transferservice transferservice() { // if bean in applicationcontext, homecoming bean // else phone call super.transferservice() , store bean in context } }

so appear pretty clear how spring can handle singleton problem: it phone call methods on proxy class extends configuration class , if requested bean exist applicationcontext homecoming beans, else phone call same method on super class create new bean , set application context

is right meaning of inheritance based on proxy pattern or missing something?

yes, you've described how spring handles @configuration classes

all @configuration classes subclassed @ startup-time cglib. in subclass, the kid method checks container first cached (scoped) beans before calls parent method , creates new instance.

if problem in certification question have 1 instance of new jdbcaccountrepository(), then, yes, best utilize accountrepository() @bean method within @configuration class.

java spring design-patterns applicationcontext proxy-pattern

No comments:

Post a Comment