May 20 2015

Spring abstract bean

Spring abstract bean

If you want a bean definition that is a template (parent) for other beans and should not be instantiated, it can be marked as an abstract bean. You can make a spring bean as abstract by configuring ‘abstract’ attribute of the ‘<bean> ‘  tag to ‘true’.

The abstract bean definition will not be instantiated by the spring container.

Example

In the below example, ‘employeeService‘  bean is the template (parent) for employeeService1 and employeeService1 beans. ‘employeeService’ is marked as abstract and it will not be instantiated by the spring container. Continue reading

Category: Spring | LEAVE A COMMENT
May 20 2015

Spring prototype component

Spring prototype component

A prototype bean is instantiated every time when a request is made for the bean. Spring IoC container creates one instance and returns the new object reference for each request.

The default scope of the bean is singleton. If you want to configure the bean with prototype scope, you have to update the scope attribute.

Xml   Continue reading

Category: Spring | LEAVE A COMMENT
May 13 2015

Spring Inject Annotation

Spring Inject Annotation

Spring 3.0 provides support for JSR-330 standard annotations and these annotations are similar to spring annotations. If you want to use them you need to include javax.inject jar in your classpath.

Inject

Inject is a J2EE specific annotation used for injecting or autowiring one class into another class. It is similar to Autowired spring annotation. Like Autowired, Inject can also be used at the class-level, field-level, method-level and constructor-argument level.

Difference between Autowired and Inject is Autowired supports required attribute whereas Inject does not support required attribute

@Inject performs injection by type, if you have more than one bean of the specific type, the container will throw ambiguity error. To Solve this problem @Named annotation need to be used. Continue reading

Category: Spring | LEAVE A COMMENT
April 21 2015

Spring singleton bean

Spring singleton bean

A singleton bean is instantiated only once per spring IoC container. Spring IoC container creates one instance of a class for each singleton bean and returns the same object reference for all the requests made for the bean.

The default scope of the bean is singleton. If the scope attribute or annotation is not specified for the bean, container will consider the bean as singleton.

Xml


<bean name="empService1" class="answersz.EmpService" scope="singleton"></bean>

<bean name="empService2" class="answersz.EmpService" ></bean>

Continue reading

Category: Spring | LEAVE A COMMENT
April 16 2015

Spring autowired required false

Spring autowired required false

Autowired Annotation is used to specify bean to automatically identify and wire dependency. @Autowired annotation wires the bean by matching the type. It is equivalent to bean’s autowire attribute in xml. This annotation can be applied to field, constructor or setter method.

Autowired required attribute

Required attribute is used to specify whether the dependency is mandatory or not. The default value of the autowired annotation required attribute is true.

If there is no matching bean found for the dependency class, container throws BeanCreationException. It can be solved by setting the required attribute to false so that container ignores the dependency and proceeds with the bean creation.

@Autowired(required=false)
EmployeeDAO employeeDAO; Continue reading

Category: Spring | LEAVE A COMMENT
April 16 2015

Spring Qualifier Annotation Example

Spring Qualifier Annotation Example

Autowired Annotation is used to specify bean to automatically identify and wire the dependency. Autowired annotation wires the bean by matching the name or type.

If there are multiple beans present for the dependency class and container is not able to wire the bean by matching the name, it throws BeanCreationException. This problem can be solved using @Qualifier annotation.

Qualifier annotation fine-tunes the autowiring. We can configure the qualifier value so the specific bean will be chosen and autowired.

In the below example, spring bean employeeDAO1 bean will be chosen and autowired.

@Autowired
@Qualifier(“employeeDAO1”)
EmployeeDAO employeeDAO;

Example Continue reading

Category: Spring | LEAVE A COMMENT
April 15 2015

Spring Autowired Annotation Example

Spring Autowired Annotation Example

Autowired Annotation is used to specify bean to automatically identify and wire dependency. @Autowired annotation wires the bean by matching the name or type. It is equivalent to bean’s autowire attribute in xml. This annotation can be applied to field, constructor or setter method.

To enable autowired annotation we have to include <context:annotation-config /> or AutowiredAnnotationBeanPostProcessor in configuration file.

If there are multiple beans present for the dependency class, container throws BeanCreationException. This problem can be solved using @Qualifier annotation.

The default value of the Autowired annotation required attribute is true. If there is no bean found for the dependency class, container throws BeanCreationException. It can be solved by setting the required attribute to false.

Spring Autowired Annotation Example

In the below EmployeeService class, @Autowired annotation on the employeeDAO finds the bean of type EmployeeDAO from the context and wires automatically. Continue reading

Category: Spring | LEAVE A COMMENT
April 8 2015

Spring lazy-init

Spring lazy-init

lazy-init is attribute of <bean> tag. It is used to specify whether a spring bean is to be lazily initialized or not.

BeanFactory initializes all the spring beans lazily whereas ApplicationContext initializes all singleton spring beans eagerly. If you want ApplicationContext to initialize the all the spring bean lazily, lazy-init attribute need to be configured to true.

Following are the valid values for lazy-init attribute

true – lazy, IOC container creates the bean when it is first requested

false – eager, IOC container creates the bean at startup Continue reading

Category: Spring | LEAVE A COMMENT
December 27 2014

Spring Autowire by Name

Autowiring by Name

Setter method is used for  Autowiring by Name  and name of a bean should be same as the name of other bean property.

Example:

In the below example we have two classes Employee and Address. Employee class has Address object reference. In the below example, Address is autowired in Employee using ‘Autowiring by Name’ by autowire=”byName”. The object name of Address in Employee class is ‘address’ therefore the bean id for Address class should be ‘address’. Continue reading

Category: Spring | LEAVE A COMMENT
December 27 2014

Autowiring In Spring

Autowiring In Spring

Spring provides autowiring feature for connecting one bean with other bean automatically.We can inject beans using ref in property or constructor-arg by yourself or we can autowiring. To enable autowire for a bean autowire=”autowiretype” attribute should be added inside bean.

There are five types of autowiring in spring

  • no (default)
  • byName
  • byType
  • constructor
  • autodetect

Continue reading

Category: Spring | LEAVE A COMMENT