Reading Notes for Spring 3 Core Components Tutorial Part III (Chapter 14 to Chapter 16)
This is a reading note from Spring Framework 3.1 Tutorial pdf, created by tutorialspoint. This pdf contains information for Spring 3 Core Basics, which is very useful for understanding defination and practive in Spring programming.
Note: All tips that is written by book will has a sign near it.
All code example in this post comes from book.
This note includes Chapter 14 to Chapter 16 in tutorial.
Chapter 14: Spring Beans Auto-Wiring
This is an example of my own problem: I tried to use jdbctemplate.update()
function to insert data, which is passed from controller by a DAO object.
No matter how hard I try, even using hard-code for sql statement, I kept getting nested exception: java.lang NullPointerException
error. Problem did not get solved until I realize instead of just declare a DAO object and call update method, I should declare a DAO as a class variable, and add @autowired
to it, such as:
|
|
Spring can auto-wire relationships between beans without using <constructor-arg>
or <property>
Auto-wiring Modes
You should be autowire
attribute of the <bean/>
element to specify autowire mode for a bean definition. There are four Auto-wiring modes, which are defined in this following table:
byType
or constructor
autowiring mode can be used to wire arrays and other typed-collections.
Limitations with Autowiring
Autowiring is great to use across a project. However, it do have limitations and you can override it:
Autowiring By Name
- Spring container looks at the beans on which has a
autowirte
attribute inxml
configuration file. - Then it will go to the java bean class for this bean, and find all attributes that have
setter
for this bean. - Then it will come back to
xml
file, check other bean name, until it find one bean that has same class type (i.e. class=”…”) as this bean’s attribute variable name. - Spring will initialize this bean, based on configuration file. Then Spring will finish previous bean’s initialization process.
- If Spring finds more than one bean in
xml
file, it will report a error.
Example:
|
|
|
|
|
|
|
|
Autowiring By Type
- Spring first check
xml
configuration file, find a bean withautowire
attribute that marked asbyType
. - Spring will go to this bean’s java class, then find all its atrribute variables that have
setter
. - Spring comes back to
xml
then go through all the beans. As long as it finds one bean that has same name as java class attribute’s type. Spring will instantiate this specific bean. - If Spring finds more than one bean in
xml
file, it will report a error.
Example:
|
|
|
|
Autowiring By Constructor
- Autowirng by constructor is very similar to autowiring by type. The only difference is it works on constructor.
- So now Spring will check
xml
file withautowire="constructor"
attribute, then go to this java bean class and find this bean’s constructor parameters. - Spring comes back to
xml
and go through all beans. As long as it find one bean that has same name as the type of constructor parameter, Spring will instantiated this bean based on its configuration, then instantiates original bean. - If Spring find more than one bean which hava same name as original bean’s constructor parameter type, it will report an error.
Example:
|
|
|
|
|
|
Chapter 15: Spring Annotation Based Configuration
Instead of using
xml
to do auto-wiring, you can use move the auto-wiring part to bean’s java class
.
Since annotation is performed before xml configuration, auto-writing in xml can override annotation.
To add anonotation, make sure you put <context:annotation-config/>
in <beans></beans>
in xml file, then add your bean configuration after this line.
There are four types of annotation:
@Required
: apply tosetter
methods@Autowired
: apply tosettter
methods ornon-setter
methods orconstructors
orproperties
.@Qualifier
: apply with@Autowirted
@JSO-250 Annotations
: includes@Resource
,@PostConstruct
adn@PreDestroy
@Required Annotation
@Required
applys to setter
. It means a configuration in xml
for this attribute type must be declared. Otherwise it will report BeanInitializationException
error.
Example:
|
|
|
|
@Autowired Annotation
1. @Autowired on Setter Methods
Instead of put <property>
in xml
file, you can use @Autowired
on setter to do a byType autowiring.
Example:
|
|
|
|
|
|
2. @Autowired on Properties
- Instead of creating a
setter
, you can put@Autowired
annotation on the property/attribute declaration. In
xml
, after you configure using<property>
, Spring will automatically create asetter
for this attribute, using the configuration inxml
file for setter’s bean. If you define a value in<property>
, the value will be passed.When Spring search for corresponding setter bean in
xml
file, it will referid
in each beans.
Example:
|
|
|
|
3. @Autowired on Constructors
@Autowired
on constructor means even if no <constructor-arg>
exists, Spring should check if there is any bean that has same id
as constructor’s parameter name.
Example:
|
|
4. @Autowired with (required=false) Option
When you add @Autowired
to setter, you can add use @Autowired(required=false)
to make sure if this setter doesn’t has a bean in xml
file, it is still fine.
5. @Qualifier Annotation
You can use @Qualifier
with @Autowire
to select only one bean if you have many beans with same type. This should be added on properties.
Example:
|
|
|
|
Spring JSP-250 Annotations
1. @PostContruct and @PreDestroy Annotations
@PostConstruct
is to be added on the method that init=XXX
in xml
file.
@PreDestroy
is to be added on the method that destroy=XXX
in xml
file.
2. @Resource
- Follows byName convention, it takes a self-defined name or default property or setter name in java file, then come to
xml
file to find the bean with same name. - It can be added on
properties
orsetters
.
Example:
|
|
Chapter 16: Spring Java Based Configuration
Java
based configuration enables you to write most of your Spring configuration without xml
.
@Configuration and @Bean
@Configuration
applys to aclass
, which means this java class is used to configure Spring beans (i.e. It is aIoC Container
).@Bean
applys toproperties
/methods
inside configuration class. It automatically pick up bean ID and returns actual bean.- a
@Configuration
class can have more than one@Bean
. - Use
AnnotationConfigApplicationContext
to get context. - You can do
context.register();
to load more than one configuration class to one context:
|
|
Example:
|
|
|
|
|
|
Injecting Bean Dependencies
To mark one bean has dependency with another bean, just call this bean from anther bean.
Example:
|
|
|
|
|
|
@Import Annotation
- You can use
@Import
for beans from another configuration class. - When you define class to ApplicationContext, only non-import bean needs to be defined.
Example:
|
|
|
|
|
|
Init and Destroy
You can use initMethod
and destoryMethod
with @Bean
for initialization and destroy of the bean:
|
|
Bean Scope
Default scope is singleton
. You can use @Scope
to define other scopes:
|
|