Skip to content

SR Chapter 3

April 14, 2010
tags:
Neither the bean name nor the bean ID is required for a bean. A bean that has noname defined is called an anonymous bean. p: namespace – for shorthand bean properties : ApplicationContext is a subinterface of BeanFactory for maintaining compatibility.
Two types of IoC container implementation. The basic one is called bean factory.The more advanced one is called application context.
Resource interface and subclasses like : ClassPath, FileSystem, URL ,InputStream Resource exist for loading data inside.
Similarly load data in IOC from a resource for two ways:
For bean factory: XmlBF
for App context :ClassPathXML*AC , FileSystemXML*AC
For removing constructor ambiguity append the type in the definition:
<div><constructor-arg type="int"  index="0" value="10"/></div>
<div>
For bean reference

<property name="da"><ref local="pp">
 

OR

<property name="da"> <ref local="pp"/> 

OR shortcut

<property name="da"  ref="pp"/>

OR BEST shortcut

<p:da-ref="pp"/>

dependency-check attribute of tag modifies IoC behavior. all, none, simple,objects. this feature only works with setters and not constructors for data population inside objects.

To ensure properties are populated – use the annotation.
@Required:
Configuration in beans.xml required in declaration:
/schema/context and .

This in turn registers a RequiredAnnotationBeanPostProcessor which can also check for additional custom annotations. Custom annotations created with :

     @Target, @Retention
     public @interface MyAnnotation

Spring Tutorial
App Fuse application

Autowiring of beans can take place by name or type, or auto detection(using sequence of prior) or constructor. If using the annotation @Autowired then include the tag context:annotation-config
by default auto-wired has required attribute as true – if want it optional set to false.

When using the Type autowiring:use Qualifier . @Autowired annotation can also be added to the setter method.

Use @Qualifier meta for stating which bean name needs to be used for injection/setting. With the name autowiring: you can use the @Resource annotation.Bean configuration and inheritance

—————-

Can make one main parent bean definition and for others need not even mention class attribute. Can override the default values of parent.

:::  bean id parent=”myparent”

If want to use parent as template then write abstract=”true” in parent declaration.Also can omit parent’s class if want to use just data only.The autowire and dependency-check attributes will not be inherited from the parent.

Collections

property name
list | set ,
– value
– bean
– null

property name
map
entry
key
value
key
bean
entry key=”” value=””

==========

<props>
     <prop key="">value</prop> 

For list: If you need to merge with parent set merge=”true”.For a set or map collection, the child elements will overwrite the parent’s if they have the same value. Default every element of the collection is treated as a String. Add type =”int” in the definition for data type. or list value-type=”int”

<bean class="org.springframework.beans.factory.config.SetFactoryBean">
<property name="targetSetClass">
<value>java.util.TreeSet</value>
</property>
<property name="sourceSet">
...........
<util:set set-class="java.util.TreeSet">
........
<property name="suffixes">
<ref local="suffixes" />
</property>

Component Scanning
Spring-managed component is @Component. Other more particular stereotypes include @Repository, @Service, and @Controller. Can give custom name to the detected component – example : @Service(“sequenceService”)
——-

@Component
public class Tester{
@Autowired
private MyBeanDao mybean;
}

Configurations required:
1. context:component-scan
and
2.xmlns:context=”http://www.springframework.org/schema/context
context:component-scan base-package=”com.da.di”

Filtering for scanning: annotation,assignable ,regex,aspectj, context:include-filter type=”regex” expression=”c*.*”

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: