spring - Why adding @Bean to a method I'll call directly -
i have seen lot of examples spring configuration through @configuration , @bean annotations. release it's common practice add @bean annotation methods called directly populate other beans. example:
@bean public properties hibernateproperties() { properties hibernateprop = new properties(); hibernateprop.put("hibernate.dialect", "org.hibernate.dialect.h2dialect"); hibernateprop.put("hibernate.hbm2ddl.auto", "create-drop"); hibernateprop.put("hibernate.format_sql", true); hibernateprop.put("hibernate.use_sql_comments", true); hibernateprop.put("hibernate.show_sql", true); return hibernateprop; } @bean public sessionfactory sessionfactory() { return new localsessionfactorybuilder(datasource()) .scanpackages("com.ps.ents") .addproperties(hibernateproperties()) .buildsessionfactory();}
so, i'm wondering if it's better declaring hibernateproperties() private without @bean annotation.
i know if bad/unneeded common practice or there reason behind.
thanks in advance!
according spring documentation inter-bean dependency injection 1 approach in order define bean dependencies in simple form. of course if define hibernateproperties()
private work not injected other components in application through spring container.
just decide depending on how many classes depends on bean , if need reuse in order call methods or inject other classes.
Comments
Post a Comment