- The Spring Framework is an open source application framework and inversion of control container for the java platform
- It provides comprehensive infrastructure support for developing java application
- spring handles the infrastructure so you can focus on your application
Why Spring
- Spring is framework like Strut and Hibernate
- Spring is famous in industry
- The learners should focus on learning more of Spring compared to other technologies now. Spring and Hibernate are extremely popular and widely used in the industry
- Spring framework is lightweight as it does not involve installation, start and stop activities associated with a container
Java Framework!!!
- In Java technology, there are so many frameworks that helps the programmers to build complex applications easily
- You can choose these frameworks for building your application
Examples:
- Hibernate : Database access mechanism
- Struts : Web Layer
- EJBs : Services like transactions, security and messaging
- Log4J : Logging
- Problem:
- Application that use these varied frameworks and services become difficult to maintain as it grows including test
Need:
- Application that use a number of frameworks and the services have to remain maintainable
- Codes should be loosely coupled with the frameworks so that testing and re usability become easy
- Spring Framework provides a light weight solution to develop maintainable and reusable enterprise applications
- It provides very simple and rich facilities to integrate various frameworks, technologies, and services in the application
Spring Simplifies Java Development:
To back up its attack on Java complexity, Spring employs four key strategies:
- Light weight and minimally invasive development with Plain Old Java Object (POJOs)
- Loose coupling through dependency injection and interface orientation
- Declarative programming through aspects and common conventions
- Boilerplate reduction through aspects and templates
Spring Featured/Modules:
- Inversion of control : Done through dependency injection
- Aspect Oriented Programming: Enabling implementing cross-cutting concerns
- Data Access : Works with JDBC and Hibernate
- Model View Controller : Provides MVC support through servlets and Struts. This is web framework
- Remote Access Framework : Supports remote access through Remote Access (RMI), web service through SOAP AND REST
- Remote Management : Java Management Extension (JMX) is the technology that is used to manage the system objects and devices like printers
- Messaging : Uses Java Messaging Service (JMS) to communicate through Message Queues
- Testing : Supports classes for writing unit test cases and integration test cases
- Convention over Configuration : Spring Roo is another framework which eases the development. This is Rapid Application Development tool
- Authentication (validity like who you are?) and Authorization (permission like what you are allowed to do) : This is provided by Spring Security Module
Spring Framework - Aspect and Test Layer:
- AOP : Used for Aspect Oriented Programming. This will allow to define intercepts and point cuts to separate them as required
- Aspect : This module helps to integrate with AspectJ. AspectJ is another AOP framework
- Instrumentation : Instrumentation is the ability to monitor the level of products performance to diagnose the errors and write the trace information
- Testing : Supports to test the spring modules using Juint framework
Spring Framework - Core Layer:
This layer has:
- Core : It has dependency injection (DI) and IOC features
- Beans : It implements beanfactory through factory pattern method
- Context : Used for ApplicationContext. It uses Core and Bean module
- Expression Language : Used for querying and manipulating objects at run time
- Objects created by the container are also called managed object or beans
- The container can be configured by loading XML files or detecting specific Java annotations on configuration classes
- Central to the Spring Framework is its inversion of control (Ioc) container, which provides a consistent means of configuring and managing Java objects
- The container is responsible for managing object life cycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together
Spring Containers
- Spring BeanFactory Container
- It is the basic container. All other possible classes that act as containers implement BeanFactory
- It is the root container that loads all the beans and provides dependency injection to enterprise applications
- Spring ApplicationContext Container
- It is widely used as a container in the enterprise applications with a number of features
- It is an advanced container that extends the BeanFactory container with various enterprise level features
Bean Life Cycle
Application Context:
- Spring comes with several flavors of application context
- Some of the important classes that implements the ApplicationContext interface are
- ClassPathXmlApplicationContext : Loads a context definition from an XML file located in the classpath, treating context definition files as classpath resources
- FileSystemXmlApplicationContext : Loads a context definition from an XML file in the system
- XmlWebApplicationContext : Loads context definitions from an XML file contained within a web application
- AnnotationConfigApplicationContext : Creates the context by loading classes annotated with pre-defined annotation
- AnnotationConfigWebApplicationContext : Creates the Web application context by loading classes annotated with pre-defined annotations in the Web Application
Spring Configuration
There are following three important methods to provide configuration metadata to the spring container:
- XML based configuration file
- Annotation based configuration file
- Java based configuration file
Types of Dependency Injection
There are two ways to perform dependency Injection:
- Setter Method Dependency Injection (DI) : the required bean is injected through setter method for that bean
- Constructor Dependency Injection (DI) : the required bean is injected through the constructor argument
Spring Framework Tutorial 2
- Spring Concept of AutoWiring
- Differentiate between the different types of Autowiring with examples
- Signify Bean Inheritance and Spring Expression Language (SpEL)
- Explore most commonly used annotations in Spring application and Automatic discovery of beans
Autowiring
- Spring can automatically resolve dependencies using Autowiring
- Spring offers Autowiring feature that helps cut down the amount of xml configuration that developers have to write
- Spring offers following types of Autowiring
- byName: Spring container looks at the beans on which auto-wire attribute is set to byName in the xml configuration file. Based on the name of a property, a bean with the same name will be injected into this property if it exists
- byType :
- byType specifies autowiring by property type not by name
- Spring container reads the XML configuration file and for the beans on which autowire attribute is set to byType it finds and inject that bean according to type match.
- Constructor : If a bean is set to autowire by constructor in configuration file then spring container tries to match and wire
its constructor's argument with exactly one of the beans whose type matches with type of constructor argument
Loading Multiple Bean Configuration Files
- Generally in a spring based application, we have multiple bean configuration files
- Its good practice to define your beans in different xml file rather than defining all beans in one xml file
Bean Definition Inheritance
- A child bean definition inherits configuration data from a parent definition
- The child definition can override some values, or add others as needed
- Using parent, child bean definition can save a lot of typing that developers have to do otherwise
- For the child bean, father bean have been set as parent bean in xml file. So, child bean can access its last_name property from parent bean
Coding to Interfaces
- When using Spring for Dependency Injection, its a good practice to use interfaces for referencing dependencies
Annotation based Container Configuration
- @Value Annotation: @value annotation defined in JSR 330 which is used to inject string, values into members of a bean (Java Specification Request, JSR)
- Usually we use @value annotation to provide default value to bean's properties
- @Required: This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring
- @Required annotation applies to bean property setter method
- @Autowired : You can apply the annotation to methods with arbitrary names and multiple arguments
- You can apply @Autowired to constructors and fields
- By default, the autowiring fails whenever zero candidate beans are available; the default behavior is to treat annotated methods, constructors, and fields as indicating required dependencies. This behavior can be changed as demonstrated below.
- Because autowiring byType may lead to multiple candidates, it is often necessary to have more control over the selection process. One way to accomplish this is with Springs's @Qualifier annotation
@Qualifier annotation
- @Qualifier annotation is used along with @Autowired to remove the confusion by specifying which exact bean to be wired
@Resource
- @Resource takes a name attribute, and by default spring intercepts that value as the bean name to be injected. In other words, it follows by name semantics, as demonstrated in this example
@PostConstruct and @PreDestroy:
- The init method attribute specifies a method that is to be called on the bean immediately upon instantiation
- You can use @PostConstruct annotation as an alternate of initialization callback and @PreDestroy annotation as an alternate of destruction callback as explained in the below example
Bean Lifecycle Using Annotation:
- We can control the lifecycle of a bean by using annotation also
- So you don't have to write it in xml file and you don't have to implement any interfaces
- You can use @PostConstruct annotation on any method that you want to execute after instantiation and @PreDestroy annotation on any method that you want to execute before bean destruction
Automatically Discovering Beans
- To configure Spring for autodiscovery, use <context:component-scan> instead of <context:annotation-config>:
Annotating Beans for Autodiscovery
By default, <context:component-scan> looks for classes that are annotated with one of a handful of special stereotype annotations:
- @Component - A general purpose stereotype annotation indicating that the class is a spring component
- @Controller - Indicates that the class defines a Spring MVC controller
- @Repository - Indicates that the class defines a data repository
- @Service - Indicates that the class defines a service
No comments:
Post a Comment