0% found this document useful (0 votes)
3 views8 pages

Spring Core Using XML

Spring is an IoC, AOP, and MVC framework developed by Rod Johnson, primarily known for its core functionality of Dependency Injection. It provides advantages such as reduced code complexity, integration with various frameworks, support for RESTful web services, and easy test case writing. The framework utilizes an IoC container for managing bean lifecycles and dependencies, with various methods for dependency injection including setter, constructor, and field injection.

Uploaded by

Anish Roshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Spring Core Using XML

Spring is an IoC, AOP, and MVC framework developed by Rod Johnson, primarily known for its core functionality of Dependency Injection. It provides advantages such as reduced code complexity, integration with various frameworks, support for RESTful web services, and easy test case writing. The framework utilizes an IoC container for managing bean lifecycles and dependencies, with various methods for dependency injection including setter, constructor, and field injection.

Uploaded by

Anish Roshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Spring Core using XML

What is spring
 It is IOC , AOP and MVC framework
 It is multilayered architecture framework
 It is from Rod Johnson

IOC frameworks
 Spring
 Pico
 ATG

What is the Core Functionality Of Spring?


 Dependency Injection

Advantages of Spring
 Dependency Injection
 Offers layers of abstraction
o It allows you to write less code, instead of writing more code
o Spring JDBC
 Instead of writing Jdbc using 7 steps, It allows you to write in two steps
o Spring ORM
o Spring JMS
o Spring RMI
 Integrate to any framework such as struts, jsf, Servlet and Jsp. Hibernate, jpa, velocity,
freemarker
 Allows you to create restful webservices
 Supports MVC pattern for web applications and provides Front Controller (DispatcherServlet)
 Supports Internationalization
 Supports AOP (Aspect Oriented Programming)
o Seperates Aspect logic and business logic
o Aspect Logic
 Code not related to your application logic
 Logger, Validation, Conversion, Formatting and Authentication
 Easy to write test cases using Spring
o API works on top of Junit.

IOC (Inversion of control)


 It is a pattern to inject the dependencies
 Core functionality of spring is to do Dependency Injection using IOC pattern
 IOC pattern contains Container and it is responsible for creating the instance for the bean and
also for its dependencies and injects the dependencies to the bean
 At the time of demand, Container serves the bean along with its dependencies
Advantages of IOC
 Spring IOC maintains the beans as singleton
 It is responsible for managing the lifecycle of the bean
 At the time of demand, creation time is suspended
 When the dependencies are subjected to change, no need to do the changes in the code.
Instead do the changes in the xml/annotation
 Dependencies are loosely coupled
IOC container in Spring
 BeanFactory
 ApplicationContext
 WebApplicationContext

ApplicationContext
 It works on top of Bean Factory and does extra functionalities
BeanFactory ApplicationContext
It loads the beans Lazily by default Loads the beans eagerly by default
Supports only lazy Supports Lazy and Eager
Can load only single xml file(Spring Multiple xml files
configurations)
Supports Internationalization
Supports annotation, Spring EL

Implementations of ApplicationContext
 ClasspathXmlApplicationContext
o Loads the xml file from classpath and configures the beans
 FileSystemXmlApplicationcontext
o Loads the xml file from filesystem or network and configures the beans
 AnnotationConfigApplicationContext
o Loads the Spring configuration via annotations and configures the beans.

How the dependencies are injected


 Setter
 Constructor
 Field(annotation)

Setter Injection using Eager Initilaization


Lazy and Eager
Lazy  Instance created at the time of first demand(request)
Eager  instance created before any demand

Scope
 Singleton  core app
 Prototype – core app
 Request  core and web app
 Session  core and web app
 Application  core and web app
 Websocket web socket app

Setter vs Constructor
Setter Constructor
It is fairly to use because setter It is complicated to use, if parameters are too lengthy
have only one arg
It is used for mutable class It is used for immutable class
Dependecies are injected after Dependencies are injected while creating the bean
creating the bean
Dependencies are created in the Dependencies are created firstly
order defined in the xml

XML
<bean> represents the bean
Attributes
id Unique Bean ID
name Bean name
class Instance to be created by IOC
Lazy-int True, to load the beans lazily
scope Represents bean scope such as singleton, prototype
Init-method Life cycle custom init method
destroy-method Life cycle custom destroy method

Child elements
o <Property>, <constructor-arg>
<property> represents the setter to inject the dependency
Attributes
name Setter name
ref To inject Bean ref other than string and wrapper
value To Inject String and wrapper
<constructor-arg> represents each constructor argument
index Parameter index
ref To inject Bean ref other than string and wrapper
value To Inject String and wrapper

Child elements of <Constructor-arg> and <property>


<list> Injects ArrayList and array
<set> Injects LinkedHashSet
<map> Injects LinkedHashMap
<props> Injects java.util.Properties

LifeCycle
 Creating the instance
 Populate properties(inject dependencies via setter)
 BeanNameAware
 BeanFactoryAware
 ApplicationContextAware
 Bean is ready to use
 @PreDestroy
 DisposableBean – destroy()
 Custom destroy

FactoryBean
o It is a java class that creates the instance and managed by spring IOC
o If any factory bean is configured then spring manages the bean returned by invoking the
getObject() of factory bean class
o What is a FactoryBean and How it works?
o It creates the instance and that instance is managed by Spring IOC
o Spring IOC maintains the bean returned by the getObject() of FactoryBean
o

Spring EL
 starts with # and expression is enclosed with curly braces
 syntax: #{expr}
The expression is enclosed with curly braces and prefixed with #  #{expr}
<bean id=”mydao” class =”com.cg.dao.JpaDaoImpl”/>
<bean id=”myser” class=”com.cg.service.SerImpl”>
<property name =”dao” value=”#{mydao}”/>
</bean>
Spring EL resolves the bean ref and also uses arithmetic Operators and conditions.

Placeholder in Spring
 start with ${placeholder}
 substitutes the value from properties file

PropertyPlaceHolderConfigurer
 It substitutes the value from properties file using placeholder
o Value attribute in xml
 <property name=”uname” value=”${user}”/>
o @Value annotation
 @Value(“${user}”)

PropertySourcesPlaceHolderConfigurer
 It substitutes the value from properties file or against Environment instance using placeholder
o Value attribute in xml
 <property name=”uname” value=”${user}”/>
o @Value annotation
 @Value(“${user}”)

Can you inject a bean using value attribute in xml?


Yes, Using Spring EL

Can you inject a bean using @Value annotation ?


 Yes, Using Spring EL
 @Value(“#{bean_name}”)

AutoWiring

The dependencies are injected automatically by name, bytype , constructor and none

ByName

 It injects the bean that matches the bean name / ID in the xml against the property name in the
bean class
 Setter name matches the bean name in XML

Bytype

 It injects the bean that matches the bean type defined in the xml.
o Bean type in the xml must match the field type
 It raises the unsatisfiedDependencyException if multiple bean configured for the same type or
same type having multiple implementations
Constructor

 If only one bean of a type is injected via constructor then it matches by constructor arg type

 If multiple beans of same type is injected via constructor then it matches the bean name against
parameter name

Autodetect

It automatically detects and give preferences to constructor and then by type

Which AutoWiring is preferable if there are multiple beans of same type defined in xml?

 byname
 constructor

What will be the output of the following code?


 The above code raises UnsatisfiedDependencyException because for the same type two beans
are defined in beans.xml
 To avoid this , change the constructor argument name into dao1 or dao2 that matches the bean
name in beans.xml

You might also like