Spring is DI or IoC framework.
Create ApplicationContext object by using ClasspathXMLApplicationContext.
In most cases uses annoatations for mapping.
In xml config file use AutoWire with parameter "ByName" i.e. dependencies will be resolved by name of variable. Name of variable should match with other bean's name referred in xml config file.
DI can be done using SETTER & CONSTRUCTOR based approach.
Preferred one is SETTER based approach.
For small bean with less dependencies use constructor based approach.
Use interface ApplicationContextAware for having callback method to get bean name, context reference.
Use interface BeanPostProcessor for having callback method to have common functions implemented for all beans. These functions can be invoked at BeanPreInitialization or BeanPostInitialization.
Bean creation methods
Singleton -- Only once instance per spring container. (Default)
ProtoType -- For each request new instance will be created.
For web based applications, bean creation strategies are
request scope -- one instance pwe request
session scope -- One instance per user per session
Annotations :-
@AutoWired -- will inject dependency "BY TYPE". If multiple instances are present then use QUALIFIER annotation.
Add in spring config xml file following imports
<context:annotation-config />
<context:component-scan />
Beans can be annotated with @component.
ApplicationContext can be used fot getting a) Beans 2) MessageResource properties 3) Event Handling.
Read more: http://javarevisited.blogspot.com/2012/11/difference-between-beanfactory-vs-applicationcontext-spring-framework.html#ixzz36wCl7xqx
Create ApplicationContext object by using ClasspathXMLApplicationContext.
In most cases uses annoatations for mapping.
In xml config file use AutoWire with parameter "ByName" i.e. dependencies will be resolved by name of variable. Name of variable should match with other bean's name referred in xml config file.
DI can be done using SETTER & CONSTRUCTOR based approach.
Preferred one is SETTER based approach.
For small bean with less dependencies use constructor based approach.
Use interface ApplicationContextAware for having callback method to get bean name, context reference.
Use interface BeanPostProcessor for having callback method to have common functions implemented for all beans. These functions can be invoked at BeanPreInitialization or BeanPostInitialization.
Bean creation methods
Singleton -- Only once instance per spring container. (Default)
ProtoType -- For each request new instance will be created.
For web based applications, bean creation strategies are
request scope -- one instance pwe request
session scope -- One instance per user per session
Annotations :-
@AutoWired -- will inject dependency "BY TYPE". If multiple instances are present then use QUALIFIER annotation.
Add in spring config xml file following imports
<context:annotation-config />
<context:component-scan />
Beans can be annotated with @component.
ApplicationContext can be used fot getting a) Beans 2) MessageResource properties 3) Event Handling.
Spring provides two kinds of IOC container, one is BeanFactory and other is ApplicationContext. Syntactically BeanFactory and ApplicationContext both are Java interfaces and ApplicationContext extends BeanFactory. Both of them are configuration using XML configuration file. In short BeanFactory provides basic IOC and DI features while ApplicationContext provides advanced features. Apart from these, Here are few more difference between BeanFactory and ApplicationContext which is mostly based upon features supported by them.
1) BeanFactory doesn't provide support for internationalization i.e. i18n but ApplicationContext provides support for it.
2) Another difference between BeanFactory vs ApplicationContext is ability to publish event to beans that are registered as listener.
3) One of the popular implementation of BeanFactory interface is XMLBeanFactory while one of the popular implementation of ApplicationContext interface is ClassPathXmlApplicationContext. On Java web application we use WebApplicationContext which extends ApplicationContext interface and adds getServletContext method.
4) If you are using auto wiring and using BeanFactory than you need to register AutoWiredBeanPostProcessor using API which you can configure in XML if you are using ApplicationContext. In summary BeanFactory is OK for testing and non production use but ApplicationContext is more feature rich container implementation and should be favored over BeanFactory
Read more: http://javarevisited.blogspot.com/2012/11/difference-between-beanfactory-vs-applicationcontext-spring-framework.html#ixzz36wCl7xqx
No comments:
Post a Comment