Annotation injection is performed before XML injection.
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--
<alias name="" alias=""/>
<bean id="targetBean" class="class"/>
<constructor-arg ref="" type="" index=" " value=""></constructor-arg>
<property name="" ref=" " value=""></property>
<property name="driverBean">
<value type="">
key=value
</value>
</property>
<description ></description>
<lookup-method bean="" name=""/>
<meta key="" value="" />
<qualifier value="" type="org.springframework.beans.factory.annotation.Qualifier">
</qualifier>
<replaced-method name=" " replacer=""></replaced-method>
</bean>
<bean id="clientBean" class="">
<property name="targetName">
<bean abstract="true" autowire="default" class=""
autowire-candidate="default" dependency-check="default"
depends-on="" destroy-method="" factory-bean=""
factory-method="" init-method="" lazy-init="default"
name="" parent="" primary="true" scope=" " id="chk" >
</bean>
<idref bean="targetBean" local="en"/>
<list merge="default" value-type=""></list>
<map key-type="" merge="default" value-type=""></map>
<meta key="" value=""/>
<null></null>
<props merge="default" value-type=""></props>
<ref bean="targetBean" local="en" parent="targetBean"/>
<set merge="default" value-type="" ></set>
<value type=""></value>
</property>
</bean>
-->
</beans>
Inheritance:
<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
<!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <-- bean name is the same as the parent bean -->
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <!-- notice how we refer to the parent bean -->
</property>
<!-- insert other configuration and dependencies as required here -->
</bean>
Abstract:
<bean id="inheritedTestBean" abstract="true" class="org.springframework.beans.TestBean" >
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithDifferentClass" class="org.springframework.beans.DerivedTestBean" parent="inheritedTestBean" init-method="initialize">
<property name="name" value="override"/>
<!-- the age property value of 1 will be inherited from parent -->
</bean>
Case 2: If the parent definition does not specify a class, explicitly marking the parent bean definition as abstract is required.
<bean id="inheritedTestBeanWithoutClass" abstract="true">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>
<bean id="inheritsWithClass" lass="org.springframework.beans.DerivedTestBean"
parent="inheritedTestBeanWithoutClass" init-method="initialize">
<property name="name" value="override"/>
<!-- age will inherit the value of 1 from the parent bean definition-->
</bean>
Inner bean:
<bean id="outer" class="...">
<!-- instead of using a reference to a target bean, simply define the target bean inline -->
<property name="target">
<bean class="com.example.Person"> <!-- this is the inner bean -->
<property name="name" value="Fiona Apple"/>
<property name="age" value="25"/>
</bean>
</property>
</bean>
Collection
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
Collection Merging:
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.com</prop>
<prop key="support">support@example.com</prop>
</props>
</property>
</bean>
<bean id="child" parent="parent">
<property name="adminEmails">
<!-- the merge is specified on the *child* collection definition -->
<props merge="true">
<prop key="sales">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
</beans>
Null and empty string values
<bean class="ExampleBean">
<property name="email" value=""/>
</bean>
<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>
p-namespace
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="foo@bar.com"/>
</bean>
<bean name="p-space" class="com.example.ExampleBean"
p:email="foo@bar.com"/>
show the Dependency on a single bean
<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />
show the Dependency on a single bean
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean>
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />
Lazy Initialized Bean
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.
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
You can also control lazy-initialization at the container level by using the default-lazy-init
attribute on the <beans/>
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>
Autowiring collaborators @Autowired
The Spring container can autowire relationships between collaborating beans.
autowiring functionality has five modes:
1. No
2. byname
3. byType
4. constructor
Arbitrary method replacement
replaced-method element to replace an existing method implementation with another, for a deployed bean.
Eg public class MyValueCalculator{
public String computeValue(String input) { // some real code...}
// some other methods...
}
A class implementing the org.springframework.beans.factory.support.MethodReplacer interface provides the new method definition.
public class ReplacementComputeValue implements MethodReplacer {
public Object reimplement(Object o, Method m, Object[] args) throws Throwable {
// get the input value, work with it, and return a computed result
String input = (String) args[0];
...
return ...;
}
}
<bean id="myValueCalculator" class="x.y.z.MyValueCalculator">
<!-- arbitrary method replacement -->
<replaced-method name="computeValue" replacer="replacementComputeValue">
<arg-type>String</arg-type>
</replaced-method>
</bean>
<bean id="replacementComputeValue" class="a.b.c.ReplacementComputeValue"/>
Bean scopes: 5 scopes
Singletone, prototype, request, session, global session.
Interfaces used in Spring:
Lifecycle, Phased, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, BootstrapContextAware, LoadTimeWeaverAware, MessageSourceAware, NotificationPublisherAwaSrpering, PortletConfigAware, PortletContextAware, ResourceLoaderAware, ServletContextAware, ServletConfigAware, BeanPostProcessor, Ordered.
Note:
1. the Spring IoC container instantiates a bean instance and then BeanPostProcessor interfaces do their work. BeanPostProcessor interfaces are scoped per-container.
Controller/Interfaces/Classes used in Spring:
1. org.springframework.beans.factory.config.CustomScopeConfigurer
2. org.springframework.context.support.SimpleThreadScope
3. org.springframework.context.support.DefaultLifecycleProcessor
4. org.springframework.context.ApplicationContextAware
5. org.springframework.beans.DerivedTestBean
6. org.springframework.beans.factory.config.BeanPostProcessor
7. org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
8. org.springframework.beans.factory.FactoryBean
9. org.springframework.jdbc.support.lob.OracleLobHandler
org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor
org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
org.springframework.web.servlet.view.InternalResourceViewResolver
org.springframework.web.servlet.mvc.ParameterizableViewController
used in Spring
1.
Listener used in Sring
1. org.springframework.web.context.request.RequestContextListener
2.
<filter> <filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping><filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
PropertyPlaceholderConfigurer: At runtime, a PropertyPlaceholderConfigurer is applied to the metadata that will replace some properties of the DataSource. The values to replace are specified as 'placeholders' of the form
${property-name} which follows the Ant / Log4J / JSP EL style.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Annotation
1. @Required: The @Required annotation applies to bean property setter methods.
@Required
public void setMovieFinder(MovieFinder movieFinder) {this.movieFinder = movieFinder;}
2. @Autowired: to use with constructors and fields.
3. @Qualifier: annotation can only be applied as a meta-annotation unlike Spring's @Qualifier which takes a string property to discriminate among multiple injection candidates and can be placed on annotations as well as types, fields, methods, constructors, and parameters.
public class MovieRecommender { @Autowired@Qualifier("main")private MovieCatalog movieCatalog;}
<context:annotation-config/>
<bean class="example.SimpleMovieCatalog"><qualifier value="main"/><!-- inject any dependencies required by this bean --></bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
<qualifier type="Genre" value="Action"/> use with @Genre("Action")
Update Coming Soon ...
4.