In this post we will talk about some basic ideas of Spring.

1. How to create a bean?

<bean id=”a” class=”x”>
<property name=”ap”>
<list>
<ref bean=”one”/>
</list>
</property>
<property name=”bp”>
<map>
<entry key=”a” value=”b”/>
</map>
</property>
<property name=”bp”>
<props>
<prop key=”a” value=”b”/>
</props>
</property>

2. How to do constructor injection?

<bean id=”a” class=”x”>
<constructor-arg ref=”other”/>
</bean>

we can use @ConstructorProperties, which is shipped with JDK6, as an annotation approach

3. How to link a bean?

@Autowired
Quilifier(“somename”)
====
@Resource(name=”somename”)

The above two approaches are the same.

4.How to find/activate beans in the current ApplciationContext?

<context:component-scan base-package=”mine”/>

===
<context:annontation-config/>

The above two are different approaches. There is a small difference.The second approach is used to activate beans already registered  in the current application context. The first one can do what the second does and it can also scan the provided package to find and register beans in the current application context.

5. the init attribute

<bean id=”a” class=”x” init-method=”m1″/>

===

Alternatively you can use the @PostConstruct annotation to do the same job.

6. How to create the application context?

ApplcationContext tx=new ClasspathXmlApplciationContext(“xxx.xml”);

7. What is the difference between BeanFactory and ApplicationContext

They are all used to instantiate beans.But ApplicationContext can

a. access resources such as URL and files.

b. it supports multiple contexts. The basic idea is to have one primary application context, usually named as applicationContext.xml, and then many other application contexts like applicationContext-dao.xml,application-ldap.xml.

8. Inheritance in Spring

<bean id=”parent” class=”parent” abstract=”true”/>

<bean id =”child” class=”child” parent=”parent”/>

9. how to load properties file?

<context:property-placeholder location=”a.properties”/>
<bean id=”a” class=”x”>
<property name=”aa” value=”${value.from.property.file”/>
</bean>

In summary, this post just gives some basic idea of Spring. If you find more please comment.Thanks in advance.

Advertisements