Spring Container
Techically this is known as ApplicationContext. Specialized implementations are of this1. ClassPathXmlApplicationContext
2. AnnotationConfigApplicationContext
3. GenericWebApplicationContext
......
Primary responsiblities of spring container are
1. Inversion of Control
2. Dependency injection
Configuring Spring Container
1. XML configuration (sample)2. Java Annotations (sample)
3. Java Source Code (sample)
Inversion Of Control
A process of externalizing the construction and mangement of objects. Means you outsource control of objects to an object factory.Creation of objects are configurable not hard-coded.
Dependency Injection
Decouple conventional depedency relationship between objects. Basically you outsource the construction of object with necessary to third party component. This third party component will inject necessary "dependencies" and construct your object and send back to you.For example if you want to have a car you simply ask for car manufacturing company. Then company will "inject" necessary "dependencies" for car like engine, tires, body, .. and build the car for you.
Spring annotations
To enable the annotation is spring, we need to define followings in configuration file.1. namespace , schema and xsd location for context
2. enable annotation
3. where to scan annotations
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
XML:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.rahal"/>
</beans>
Steretyping Annotations
These annotations are used to stereotype classes with regard to the application tier that they belong to. Classes that are annotated with one of these annotations will automatically be registered in the Spring application context if <context:component-scan> is in the Spring XML configuration.1. @Component - generic stereotype for any Spring-managed component
2. @Service - stereotype for persistence layer
3. @Repository - stereotype for service layer
4.@Controller - stereotype for presentation layer (spring-mvc)
1. @scope
Declare scope for the beans. Five types of scopes.- singleton (default) : Return a single bean instance per Spring IoC container
- prototype : Return a new bean instance each time when requested
- request : Return a single bean instance per HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
- session : Return a single bean instance per HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
- globalSession : Return a single bean instance per global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
public class CustomerService
{
}
2. @Autowired
Spring container can autowire relationships without using complex configurations .Autowiring Modes (Xml Configuration)
ByType : If bean has autowire="byType" in bean configuration then container tries to match and wire property, if its type match with exactly one bean defined in configuration. If more than such beans defined in configuration fatal exception will be thrown.ByName : Applies to property setter method. If bean has autowire="byName" in bean configuration then container looks for the property names of the bean. Then it tries to match and wire its properties with beans defined by the same name in configuration file.
Constructor : Similar to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, fatal error will be thrown.
Autowiring annotation (@Autowired)
When spring container finds @Autowired it will scan though stereotype annotation and tries to bind appropriately.- On Properties ( ByType )
i.e @Autowired
private CustomerRepository customerRepository;
- On Setters ( ByType )
- On Constructors (Constructor)
3. @Qualifier
As in @autwired, when spring use 'byType' mode, dependencies are looked for property class types. But if there are two or more beans for same type class spring will throw an error. To resolve this confilict we use @Qualifier.(sample)
4. @Configuration and @Bean
These two annotations help to enable java-base configuration (without using any xml). Annotation a class with @Configuration imply to the spring container that the class is used to define beans. When spring container find @Bean defined under the @Configuration , it will create Object and register in spring context.(sample)
Property Files
Spring provide handy way of injecting properties defined in property file to bean properties. To do that add following configurations into configuration xml1. <context:annotation-config/>
2. <context:property-placeholder location="filename"/>
Use @Value annotation to inject values
i.e @Value("${dbUsername}")
private String dbUserName;
Property file should be in classpath and should have key=value structure.
No comments:
Post a Comment