Api&m Unit 1 JSVGK
Api&m Unit 1 JSVGK
UNIT I
Spring 5 Basics : Why Spring, What is Spring Framework, Spring Framework - Modules,
Configuring IoC container using Java-based configuration, Introduction To Dependency Injection,
Constructor Injection, Setter Injection, What is AutoScanning
Spring 5 Basics
1: Why Spring:
Spring makes programming Java quicker, easier, and safer for everybody. Spring’s focus on
speed, simplicity, and productivity has made it the world's most popular Java framework.
Spring is everywhere
Spring’s flexible libraries are trusted by developers all over the world. Spring delivers delightful
experiences to millions of end-users every day—whether that’s streaming TV, online shopping, or
countless other innovative solutions. Spring also has contributions from all the big names in tech,
including Alibaba, Amazon, Google, Microsoft, and more.
Spring is flexible
Spring’s flexible and comprehensive set of extensions and third-party libraries let developers build
almost any application imaginable. At its core, Spring Framework’s Inversion of Control
(IoC) and Dependency Injection (DI) features provide the foundation for a wide-ranging set of
features and functionality. Whether you’re building secure, reactive, cloud-based microservices for
the web, or complex streaming data flows for the enterprise, Spring has the tools to help.
Spring is productive
Spring Boot transforms how you approach Java programming tasks, radically streamlining your
experience. Spring Boot combines necessities such as an application context and an auto-
configured, embedded web server to make microservice development a cinch. To go even faster,
you can combine Spring Boot with Spring Cloud’s rich set of supporting libraries, servers, patterns,
and templates, to safely deploy entire microservices-based architectures into the cloud, in record
time.
Spring is fast
Our engineers care deeply about performance. With Spring, you’ll notice fast startup, fast
shutdown, and optimized execution, by default. Increasingly, Spring projects also support
the reactive (nonblocking) programming model for even greater efficiency. Developer productivity
2 SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
API & MICROSERVICES Unit-1 Dept. of CSE, JSVG KRISHNA
is Spring’s superpower. Spring Boot helps developers build applications with ease and with far less
toil than other competing paradigms. Embedded web servers, auto-configuration, and “fat jars”
help you get started quickly, and innovations like LiveReload in Spring DevTools mean developers
can iterate faster than ever before. You can even start a new Spring project in seconds, with the
Spring Initializr at start.spring.io.
Spring is secure
Spring has a proven track record of dealing with security issues quickly and responsibly. The
Spring committers work with security professionals to patch and test any reported vulnerabilities.
Third-party dependencies are also monitored closely, and regular updates are issued to help keep
your data and applications as safe as possible. In addition, Spring Security makes it easier for you
to integrate with industry-standard security schemes and deliver trustworthy solutions that are
secure by default.
Spring is supportive
The Spring community is enormous, global, diverse, and spans folks of all ages and capabilities,
from complete beginners to seasoned pros. No matter where you are on your journey, you can find
the support and resources you need to get you to the next
level: quickstarts, videos, meetups, support, or even formal training and certification.
Microservices
Quickly deliver production-grade features with independently evolvable microservices.
Reactive
Spring's asynchronous, nonblocking architecture means you can get more from your computing
resources.
Cloud
Your code, any cloud—we’ve got you covered. Connect and scale your services, whatever your
platform.
Web apps
Frameworks for fast, secure, and responsive web applications connected to any data store.
3 SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
API & MICROSERVICES Unit-1 Dept. of CSE, JSVG KRISHNA
Serverless
The ultimate flexibility. Scale up on demand and scale to zero when there’s no demand.
Event Driven
Integrate with your enterprise. React to business events. Act on your streaming data in realtime.
Batch
Automated tasks. Offline processing of data at a time to suit you.
The Spring Framework is a popular and widely used framework for building
enterprise-level Java applications. It provides comprehensive infrastructure support
for developing robust and maintainable applications. The framework is modular and
provides various modules that cater to different aspects of application development.
Spring enables you to build applications from "plain old Java objects" (POJOs) and
to apply enterprise services non-invasively to POJOs. This capability applies to the
Java SE programming model and to full and partial Java EE.
Examples of how you, as an application developer, can use the Spring platform
advantage:
2. Data Access/Integration:
The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and
Transaction modules.
The JDBC module provides a JDBC-abstraction layer that removes the need to do
tedious JDBC coding and parsing of database-vendor specific error codes.
The ORM module provides integration layers for popular object-relational mapping
APIs, including JPA, JDO, and Hibernate. Using the ORM package you can use all
of these O/R-mapping frameworks in combination with all of the other features
Spring offers, such as the simple declarative transaction management feature
mentioned previously.
6 SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
API & MICROSERVICES Unit-1 Dept. of CSE, JSVG KRISHNA
The OXM module provides an abstraction layer that supports Object/XML mapping
implementations for JAXB, Castor, XMLBeans, JiBX and XStream.
The Java Messaging Service (JMS) module contains features for producing and
consuming messages.
The Transaction module supports programmatic and declarative transaction
management for classes that implement special interfaces and for all your POJOs
(plain old Java objects).
3. Web:
The Web layer consists of the Web, Web-Servlet, WebSocket and Web-Portlet
modules.
Spring’s Web module provides basic web-oriented integration features such as
multipart file-upload functionality and the initialization of the IoC container using
servlet listeners and a web-oriented application context. It also contains the web-
related parts of Spring’s remoting support.
The Web-Servlet module contains Spring’s model-view-controller (MVC)
implementation for web applications. Spring’s MVC framework provides a clean
separation between domain model code and web forms, and integrates with all the
other features of the Spring Framework.
The Web-Portlet module provides the MVC implementation to be used in a portlet
environment and mirrors the functionality of Web-Servlet module.
The @Bean annotation is used to indicate that a method instantiates, configures and
initializes a new object to be managed by the Spring IoC container. For those
familiar with Spring’s <beans/> XML configuration the @Bean annotation plays
the same role as the <bean/> element. You can use @Bean annotated methods with
any Spring @Component, however, they are most often used
with @Configuration beans.
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
8 SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
API & MICROSERVICES Unit-1 Dept. of CSE, JSVG KRISHNA
</beans>
When @Component and JSR-330 classes are provided, they are registered as bean
definitions, and it is assumed that DI metadata such
as @Autowired or @Inject are used within those classes where necessary.
Simple construction
In much the same way that Spring XML files are used as input when instantiating
a ClassPathXmlApplicationContext, @Configuration classes may be
used as input when instantiating
an AnnotationConfigApplicationContext. This allows for completely
XML-free usage of the Spring container:
Declaring a bean
To declare a bean, simply annotate a method with the @Bean annotation. You use
this method to register a bean definition within an ApplicationContext of the
type specified as the method’s return value. By default, the bean name will be the
same as the method name. The following is a simple example of a @Bean method
declaration:
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
10 SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
API & MICROSERVICES Unit-1 Dept. of CSE, JSVG KRISHNA
}
<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>
@Configuration
public class AppConfig {
@Bean
public Foo foo() {
return new Foo(bar());
}
@Bean
public Bar bar() {
return new Bar();
11 SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
API & MICROSERVICES Unit-1 Dept. of CSE, JSVG KRISHNA
}
In the example above, the foo bean receives a reference to bar via constructor
injection.
In the preceding example, setters are declared to match against the properties
specified in the XML file. The following example uses constructor-based DI:
public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
15 SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
API & MICROSERVICES Unit-1 Dept. of CSE, JSVG KRISHNA
6:AutoScanning in Spring.
@Service
public class SimpleMovieLister {
@Repository
public class JpaMovieFinder implements MovieFinder {
// implementation elided for clarity
}
16 SIR C R REDDY COLLEGE OF ENGINEERING, ELURU
API & MICROSERVICES Unit-1 Dept. of CSE, JSVG KRISHNA
To autodetect these classes and register the corresponding beans, you need to
add @ComponentScan to your @Configuration class, where
the basePackages attribute is a common parent package for the two classes.
(Alternatively, you can specify a comma- or semicolon- or space-separated list that
includes the parent package of each class.)
@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig {
// ...
}
The following alternative uses XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-
context.xsd">
<context:component-scan base-package="org.example"/>
</beans>