{"id":166,"date":"2016-02-01T23:50:13","date_gmt":"2016-02-01T23:50:13","guid":{"rendered":"http:\/\/kaizen-koka.com\/?p=166"},"modified":"2018-03-21T14:13:41","modified_gmt":"2018-03-21T14:13:41","slug":"spring-ioc-configuration","status":"publish","type":"post","link":"https:\/\/kaizen-koka.com\/?p=166","title":{"rendered":"Spring IOC &#038; Configuration"},"content":{"rendered":"<p>\n\tWorking Example:&nbsp;<a href=\"https:\/\/github.com\/teachkoka\/spring-ioc-xml-b8\">https:\/\/github.com\/teachkoka\/spring-ioc-xml-b8<\/a>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Spring IOC Container:<\/strong><br \/>\n\t&nbsp;&nbsp;<\/span><\/span><span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.<br \/>\n\t&nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">&nbsp; &nbsp; **In simple terms, passing required parameters to classes through xml configuration.<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Dependency Injection&nbsp;nothing but&nbsp;Inversion&nbsp;of Control (IoC) because the normal control sequence would be the object finds the objects it depends on by itself and then calls them. Here, this is reversed: The&nbsp;dependencies&nbsp;are handed to the object when it&#39;s created<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">The interface&nbsp;<code>org.springframework.context.ApplicationContext<\/code>&nbsp;represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.&nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">&nbsp;org.springframework.beans<br \/>\n\t&nbsp;org.springframework.context<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>2 Types of IOC Containers:<\/strong><br \/>\n\t&nbsp; .Core Container: Bean Factory<br \/>\n\t&nbsp; .J2EE Container: Application Context &nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Container responsibilities:<\/strong><br \/>\n\t1)Create instances<br \/>\n\t2)Dependency Injection<br \/>\n\t3)LifeCycle Management<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Bean Factory(Core) container :<\/strong><br \/>\n\t&nbsp; &nbsp;.At the time of container startup &#8211; Loads the spring xml file and parse the xml file wellformed or not and validates the tags using sax parser .<br \/>\n\t&nbsp; &nbsp;.Creates the instance of the bean when the user request for the bean.<br \/>\n\t&nbsp; &nbsp; By default all the beans configured in are singleton.<br \/>\n\t&nbsp; &nbsp;<\/span><\/span>\n<\/p>\n<pre class=\"brush:java;\">\r\n&nbsp; &nbsp; Resource resource = new ClassPathResource(&quot;beans.xml&quot;);\r\n&nbsp; &nbsp; BeanFactory factory = new XmlBeanFactory(resource);<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Application context(J2EE) container :<\/strong><br \/>\n\t&nbsp; .At the time of container startup &#8211; Loads the spring xml file and parse the xml file well-formed or not and validates the tags using sax parser.<br \/>\n\t&nbsp; .Creates the instance of all the beans, with singleton scopes, at the time of application start-up. If bean scope is prototype, at the time of each request bean insatnce will be created.<br \/>\n\t&nbsp; .By default all the beans configured are singleton.<\/span><\/span><span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">&nbsp;<\/span><\/span>\n<\/p>\n<pre class=\"brush:java;\">\r\n&nbsp;ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&quot;beans.xml&quot;);<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Dependency Injection can be achieved either through setter method or construtors.<br \/>\n\tUsing dependency injection we can inject any datatype<br \/>\n\t1. primitive injection<br \/>\n\t2. reference injection<br \/>\n\t<strong>Points to remember <\/strong>:&nbsp;<br \/>\n\t&nbsp;.IOC container designed to load in such a way that it will load private construtor classes as well.&nbsp;<br \/>\n\t&nbsp;.One setter method will take only one input parameter.<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>&#8211;primitive<\/strong><\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean id=&quot;&quot; class=&quot;&quot; scope=&quot;&quot;&gt;\r\n&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;property name=&quot;&quot; value=&quot;&quot;\/&gt;\r\n&nbsp;&nbsp; &nbsp; &nbsp;&lt;\/bean&gt;<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>&#8211;List (Arraylist)&nbsp;<\/strong><\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean id=&quot;&quot; class=&quot;&quot; scope=&quot;&quot;&gt;\r\n&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;property name=&quot;&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;set&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;value&gt; &lt;\/value&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;value&gt; &lt;\/value&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/set&gt;\r\n&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/property&gt;\r\n&nbsp;&nbsp; &nbsp; &nbsp;&lt;\/bean&gt;&nbsp;&nbsp;<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>&#8211;Map (LinkedHashMap)&nbsp;<\/strong><\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n &nbsp;&lt;bean id=&quot;&quot; class=&quot;&quot; scope=&quot;&quot;&gt;\r\n&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;property name=&quot;&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;map&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;entry key=&quot;&quot; value=&quot;&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;entry key=&quot;&quot; value=&quot;&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/map&gt;\r\n&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/property&gt;\r\n&nbsp;&nbsp; &lt;\/bean&gt;\r\n<\/pre>\n<p>\n\t<strong><span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">&#8211;property<\/span><\/span><\/strong>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">xmlns:util &nbsp; xmlns:util=&quot;http:\/\/www.springframework.org\/schema\/util&quot;<br \/>\n\txsi:schemaLocation &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/util http:\/\/www.springframework.org\/schema\/util\/spring-util-3.0.xsd&quot;&gt;<br \/>\n\t&nbsp; &nbsp; &lt;bean id=&quot;t&quot; class=&quot;com.bellinfo.corejava.Test&quot;&gt;<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;driver&quot;&gt;<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;util:properties location=&quot;classpath:drivers.properties&quot;\/&gt;<br \/>\n\t&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/property&gt;<br \/>\n\t&nbsp; &nbsp; &lt;\/bean&gt;<\/span><\/span>\n<\/p>\n<p>\n\t<strong><span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">.Constructor :<\/span><\/span><\/strong><br \/>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">&nbsp; &nbsp; <\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean id=&quot;&quot; class=&quot;&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;constructor-arg name =&quot;&quot; value=&quot;&quot; \/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;constructor-arg name =&quot;&quot; &nbsp;value=&quot;&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp;&lt;\/bean&gt;\r\n\r\n&nbsp; &nbsp; &nbsp;&lt;bean id=&quot;&quot; class=&quot;&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;constructor-arg value=&quot;&quot; index=&quot;&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;constructor-arg value=&quot;&quot; index=&quot;&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp;&lt;\/bean&gt;\r\n\r\n&nbsp; &nbsp; &nbsp;&lt;bean id=&quot;&quot; class=&quot;&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;constructor-arg value=&quot;&quot; index=&quot;0&quot; type=&quot;java.lang.String&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;constructor-arg value=&quot;&quot; index=&quot;1&quot; type=&quot;int&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp;&lt;\/bean&gt;<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>&nbsp;Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies.<\/strong>&nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<br \/>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">To make setter dependency injection mandatory, using dependency-check. By default, its &#39;none&#39; (simple &#8211; for primitive, objects &#8211; for objects, all- for all kind)<br \/>\n\t<strong>@Required &nbsp;&#8211;<\/strong>&gt; RequiredAnnotationBeanPostProcessor<br \/>\n\t<strong>depends-on<\/strong> &#8211; If class A is depending on class B then<\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&nbsp;&nbsp; &nbsp;&lt;bean id=&quot;a&quot; class=&quot;A&quot; depends-on=&quot;b&quot;&gt;\r\n&nbsp;&nbsp; &nbsp;&lt;bean id=&quot;b&quot; class=&quot;B&quot;&gt;<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">p-namespace &nbsp;&#8211; property<br \/>\n\tc-namespace &nbsp;&#8211; constructor<br \/>\n\t&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n\txmlns:p=&quot;http:\/\/www.springframework.org\/schema\/p&quot;<br \/>\n\txmlns:c=&quot;http:\/\/www.springframework.org\/schema\/p&quot;<\/span><\/span><br \/>\n\t&nbsp;\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">In XML, this behavior is controlled by the lazy-init attribute on the &lt;bean\/&gt; element; for example:<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">&lt;bean id=&quot;lazy&quot; class=&quot;com.foo.ExpensiveToCreateBean&quot; lazy-init=&quot;true&quot;\/&gt;<br \/>\n\t&lt;bean name=&quot;not.lazy&quot; class=&quot;com.foo.AnotherBean&quot;\/&gt;<br \/>\n\tWhen the preceding configuration is consumed by an ApplicationContext, the bean named lazy is not eagerly pre-instantiated when the ApplicationContext is starting up, whereas the not.lazy bean is eagerly pre-instantiated.<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext creates the lazy-initialized bean at startup, because it must satisfy the singleton&rsquo;s dependencies. The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Autowiring :&nbsp;<\/strong><br \/>\n\t&nbsp;.Injecting dependency automatically<br \/>\n\t&nbsp;.Can be applied only to secondary datatypes<br \/>\n\tIt can be done through&nbsp;<br \/>\n\t&nbsp;.byType<br \/>\n\t&nbsp;.byName<br \/>\n\t&nbsp;.constructor<br \/>\n\t&nbsp;.autodetect<br \/>\n\t&nbsp;.no (default)<\/span><\/span><br \/>\n\t&nbsp;\n<\/p>\n<pre class=\"brush:xml;\">\r\n&nbsp;&nbsp;&lt;bean id=&quot;t1&quot; class=&quot;com.bellinfo.corejava.Test&quot; autowire=&quot;byName&quot;&gt;\r\n&nbsp; &lt;bean id=&quot;t1&quot; class=&quot;com.bellinfo.corejava.Test&quot; autowire=&quot;byType&quot;&gt;\r\n&nbsp; &lt;bean id=&quot;t1&quot; class=&quot;com.bellinfo.corejava.Test&quot; autowire=&quot;constructor&quot;&gt;\r\n&nbsp; &lt;bean id=&quot;t1&quot; class=&quot;com.bellinfo.corejava.Test&quot; autowire=&quot;default&quot;&gt;\r\n&nbsp; &lt;bean id=&quot;t1&quot; class=&quot;com.bellinfo.corejava.Test&quot; autowire=&quot;no&quot;&gt;\r\n<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>&#8211;<\/strong>byType, byName, autodectect &#8211; for settertype dependecy injection<br \/>\n\t<strong>&#8211;<\/strong>constructor, autodetect &nbsp;&#8211; for constructor dependency<br \/>\n\t<strong>&#8211;<\/strong>byType ambiguity can be resolved through autowire-candidate=&quot;false&quot;<br \/>\n\t<strong>&#8211;<\/strong>If you want to apply for all the beans that are there in the xml file then use default-autowire=&quot;byType&quot; at root-element&nbsp;<br \/>\n\t&nbsp; &nbsp; &nbsp; <strong>&lt;beans default-autowire=&quot;byType&quot;&gt;&lt;\/beans&gt;<\/strong><br \/>\n\t<strong>&#8211;<\/strong>byName, you don&#39;t encounter any ambiguity<br \/>\n\t<strong>&#8211;<\/strong>constructor, internally it use byType mechanism &nbsp;<br \/>\n\t<strong>&#8211;<\/strong>autoDetect<br \/>\n\t&nbsp; &nbsp; <strong>.<\/strong>if default constructor, parameter constructors and setter Injection then it will choose default constructor for instance creation and setter Injection for dependency Injection.<br \/>\n\t&nbsp; &nbsp; <strong>.<\/strong>if parameter constructors and setter Injection then it will choose default constructor for both instance creation and injection.<br \/>\n\t&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>@Autowired<\/strong>( uses byType), to resolve ambiguity use <strong>@Qualifier<\/strong>(value=&quot;id&quot;);<br \/>\n\tIn order to activate autowired annotation,&nbsp;<\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&nbsp; &nbsp; &nbsp;&lt;context:annotation-config\/&gt;<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Spring reduces the configuration of creating the objects of classes by auto-scanning &amp; stereotype annotations<br \/>\n\t1 <strong>Singleton<\/strong> &nbsp;-(Default) Scopes a single bean definition to a single object instance per Spring IoC container.<br \/>\n\tprototype &#8211; Scopes a single bean definition to any number of object instances.<br \/>\n\t2.<strong>Request<\/strong> Scopes &#8211; A single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.<br \/>\n\t3.<strong>Session<\/strong> Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.<br \/>\n\t4.<strong>GlobalSession<\/strong> Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a Portlet context. Only valid in the context of a web-aware Spring ApplicationContext.<br \/>\n\t5.<strong>Application Scopes<\/strong> &#8211; a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.&nbsp;&nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>Is Java Singleton different from Spring Singleton?<\/strong><br \/>\n\tYES, YES, YES<br \/>\n\tJava considers something a singleton if it cannot create more than one instance of that class within a given class loader, whereas Spring would consider something a singleton if it cannot create more than one instance of a class within a given container\/context.<\/span><\/span>\n<\/p>\n<p>\n\t<br \/>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\"><strong>4 stereotype annotations<\/strong><br \/>\n\t1)@Controller &nbsp;(on Spring MVC for Controller)<br \/>\n\t2)@Repository &nbsp;(on DAO classes)<br \/>\n\t3)@Service (on business logic classes)<br \/>\n\t4)@Component &nbsp;(non MVC classes, for any utility class)<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">in order to auto-scan all the classes, you can use<\/span><\/span><br \/>\n\t&nbsp; &nbsp; &lt;context:component-scan base-package=&quot;com.bellinfo&quot;\/&gt;&nbsp;<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">&nbsp; &nbsp;<br \/>\n\t&nbsp;&nbsp; &nbsp;<br \/>\n\t&#8211; <strong>static<\/strong> dependency injection&nbsp;can achieved through <strong>MethodInvokingFactoryBean<\/strong><br \/>\n\tfor Sun created singleton classes or others &#8211; we need to use factory methods like getInstance() to create an objects<br \/>\n\tfactory-method =&quot;getInsatnce()&quot;<\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean class=&quot;org.springframework.beans.factory.config.MethodInvokingFactoryBean&quot;&gt;\r\n        &lt;property name=&quot;staticMethod&quot; value=&quot;com.bellinfo.corejava.Netflix.setFruits&quot;\/&gt;\r\n        &lt;property name=&quot;arguments&quot;&gt;\r\n            &lt;list&gt;\r\n                &lt;value&gt;apple&lt;\/value&gt;\r\n            &lt;\/list&gt;\r\n        &lt;\/property&gt;\r\n &lt;\/bean&gt;<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">Examples of singleton classess,<br \/>\n\tSessionFactory<br \/>\n\tResourceBundle<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\"><span style=\"font-family:verdana,geneva,sans-serif;\">If you want to create an instance from Factory bean instance, then&nbsp;<\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean id=&quot;sf&quot; class=&quot;SessionFactory&quot;\/&gt;\r\n&lt;bean id=&quot;s&quot; factory-bean=&quot;sf&quot; class=&quot;Session&quot; factory-method=&quot;openSession&quot;\/&gt;<\/pre>\n<p>\n\t<span style=\"font-family:verdana,geneva,sans-serif;\"><span style=\"font-size:14px;\"><strong>Lifecycle methods: <\/strong><\/span><\/span><span style=\"font-family:verdana,geneva,sans-serif;\"><span style=\"font-size:14px;\"><strong>&nbsp;<\/strong>By IOC container we can maintain simple pojo classes life cycle.<br \/>\n\t-In order to achieve this, we need to use ConfigurableApplicationContext(also J2EE) Container.<br \/>\n\t-Bean life cycle can be achieved in 3 ways<br \/>\n\t&nbsp; &nbsp;Programatic approach<br \/>\n\t&nbsp; &nbsp; &nbsp; -implement InitializingBean, DisposableBean&nbsp;<br \/>\n\t&nbsp; &nbsp;Declarative\/xml configuration approach<br \/>\n\t&nbsp; &nbsp; &nbsp; -init-method=&quot;&quot;<br \/>\n\t&nbsp; &nbsp; &nbsp; -destroy-method=&quot;&quot;<br \/>\n\t&nbsp; &nbsp;Annotation approach: to activate inject DI -CommonAnnotationBeanPostProcessor<br \/>\n\t&nbsp; &nbsp; &nbsp; @PostConstruct<br \/>\n\t&nbsp; &nbsp; &nbsp; @PreDestroy<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-family:verdana,geneva,sans-serif;\"><span style=\"font-size:14px;\"><strong>&#8211;<\/strong>Default life cycle for entire configuration then at root element configure&nbsp;<br \/>\n\tdefault-destroy-method=&quot;&quot; default-init-method=&quot;&quot;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-family:verdana,geneva,sans-serif;\"><span style=\"font-size:14px;\"><strong>LookUp methods: <\/strong>A method which required an implementation is called lookup method.<br \/>\n\t&nbsp; Spring internally generates proxyclass and return that object.<\/span><\/span>\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t<span style=\"font-family:verdana,geneva,sans-serif;\"><span style=\"font-size:14px;\">method replacer :<br \/>\n\t&nbsp;-by implements MethodReplacer<br \/>\n\t&nbsp;&#8211; &lt;bean&gt; &lt;replaced-method name=&quot;{methodName}&quot; replacer=&quot;new implemented class ref&quot;\/&gt; &nbsp;&lt;\/bean&gt;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-family:verdana,geneva,sans-serif;\"><span style=\"font-size:14px;\">&nbsp;<br \/>\n\tJDK also provides support for annotations,<br \/>\n\t@Resource(byName) &nbsp;-(same as @Autowired)<br \/>\n\t@Inject(first byName &#8211; if not matches byType) &#8211; (same as @Autowired)<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-family:verdana,geneva,sans-serif;\"><span style=\"font-size:14px;\">J2EE also support stereotype annotations<br \/>\n\t@Named&nbsp;<\/span><\/span>\n<\/p>\n<p>\n\t<span style=\"font-family:verdana,geneva,sans-serif;\"><span style=\"font-size:14px;\"><strong>Properties files data in to classes<\/strong>:<br \/>\n\t&nbsp; &nbsp;Properties dependencies injection using expression<\/span><\/span>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;\r\n&nbsp; &nbsp; &lt;property name=&quot;Location&quot; value=&quot;resources\/drivers.properties&quot;&gt;&lt;\/property&gt;\r\n&nbsp;&lt;\/bean&gt;\r\n&nbsp;\r\n&nbsp;&lt;bean id=&quot;c&quot; class=&quot;beans.DB&quot;&gt;\r\n&nbsp; &nbsp;&lt;property name=&quot;driver&quot; value=${driver}&gt;\r\n&nbsp; &nbsp;&lt;\/property&gt;\r\n&nbsp;&lt;\/bean&gt;<\/pre>\n<p>\n\tI18N<br \/>\n\t-provides language support<br \/>\n\t&#8211; All property file should end with corresponding language (like _en, _te)<br \/>\n\t&#8211; Support 200+ languages<br \/>\n\tImplementation Steps:<br \/>\n\t1)&nbsp;<br \/>\n\t&lt;bean id=&quot;messageSource&quot; class=&quot;org.springframework.context.support.ResourceBundleMessageSource;&quot;&gt;<br \/>\n\t&nbsp; &nbsp; &nbsp; &lt;property name=&quot;basename&quot; value=&quot;resources\/filename&quot;\/&gt;<br \/>\n\t&lt;\/bean&gt;<br \/>\n\t2)<br \/>\n\t&lt;%@page import =&quot;java.util.Locale&quot;%&gt;\n<\/p>\n<p>\n\t&lt;%&nbsp;<br \/>\n\tString lang = request.getHeader(&quot;accept-language&quot;);<br \/>\n\tLocale locale= new Locale(lang);<br \/>\n\tApplicationContext ap = new ClassPathXmlApplicationContext(&quot;spring-config.xml&quot;);<br \/>\n\tString value = ap.getMessage(&quot;label&quot;, null, locale);<br \/>\n\t%&gt;<br \/>\n\t&lt;form action=&quot;\/hello&quot;&gt;<br \/>\n\t&lt;%=value%&gt;: &lt;input type=&quot;text&quot; name=&quot;name&quot;\/&gt;<br \/>\n\t&lt;input type=&quot;submit&quot; value=&quot;submit&quot;\/&gt;<br \/>\n\t&lt;\/form&gt;\n<\/p>\n<p>\n\thttp:\/\/vikku.info\/indian-language-unicode-converter\/telugu-unicode-converter.html\n<\/p>\n<p>\n\tL10N &#8211; validation support and business project\n<\/p>\n<p>\n\tListeners :<br \/>\n\t1) Implement ApplicationListners&lt;&gt;<br \/>\n\t&nbsp; &nbsp; &nbsp;These are the four different events avaliable at Spring<br \/>\n\t&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ContextStartedEvent<br \/>\n\t&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ContextStoppedEvent<br \/>\n\t&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ContextRefreshEvent<br \/>\n\t&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ContextCloseEvent<br \/>\n\t2)Configure your classes in spring config file<br \/>\n\t3)Use Configurable application Context\n<\/p>\n<p>\n\t<br \/>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working Example:&nbsp;https:\/\/github.com\/teachkoka\/spring-ioc-xml-b8 Spring IOC Container: &nbsp;&nbsp;It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[9,2],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-advance","category-technology"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p70lnf-2G","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=166"}],"version-history":[{"count":14,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions\/505"}],"wp:attachment":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}