And pre hooks dependencies are needed the classpath
Spring
®
Framework
|
---|
Section 1.2: Showcasing Core Spring Features by example ..................................................................................... 3
Section 1.3: What is Spring Framework, why should we go for it? ........................................................................... 6
Section 3.1: Syntax Reference .................................................................................................................................... 12
Chapter 4: Obtaining a SqlRowSet from SimpleJdbcCall ..................................................................... 13
Section 5.2: Basic annotation autowiring ................................................................................................................. 17
Section 5.3: Using FactoryBean for dynamic bean instantiation ........................................................................... 18
Chapter 6: Bean scopes ........................................................................................................................................... 25
Section 6.1: Additional scopes in web-aware contexts ............................................................................................ 25
Section 7.2: Condition annotations ............................................................................................................................ 30
Chapter 8: Spring JSR 303 Bean Validation .................................................................................................. 32
Section 9.1: Autowiring ................................................................................................................................................ 37
Section 9.2: Bootstrapping the ApplicationContext ................................................................................................. 37
Section 10.2: Setting headers on Spring RestTemplate request ............................................................................ 43
Section 10.3: Generics results from Spring RestTemplate ...................................................................................... 44
Section 11.2: Cron expression ...................................................................................................................................... 47
Section 11.3: Fixed delay .............................................................................................................................................. 49
Section 12.3: Lazy initialization in the configuration class ....................................................................................... 51
Chapter 13: Property Source ................................................................................................................................. 52
Section 14.2: Autowiring a dependency through XML configuration ..................................................................... 53
Section 14.3: Injecting a dependency manually through XML configuration ........................................................ 54
Section 15.3: SQLRowSet ............................................................................................................................................. 58
Section 15.4: Batch operations ................................................................................................................................... 58
Section 17.1: Spring Profiles allows to configure parts available for certain environment .................................. 61
Chapter 18: Understanding the dispatcher-servlet.xml .......................................................................... 62
About
Please send feedback and corrections to
Chapter 1: Getting started with Spring
2015-07-31
2014-09-14
2007-12-25
2006-10-04
Steps to create Hello Spring:
1. Investigate Spring Boot to see if that would better suit your needs.
6. Include (and its transitive dependencies!) as a dependency.
Employee.java:
return name;
|
---|
System.out.println(name);
}
2 |
---|
|
---|
Customer.java:
publicstaticvoid main(String[] args){
|
---|
Description
This is a self-contained running example including/showcasing: minimum dependencies needed, Java Configuration, Bean declaration by annotation and Java Configuration, Dependency Injection by Constructor and by Property, and Pre/Post hooks.
Starting from the end, this is our Main class that serves as a placeholder for the main() method which initialises the Application Context by pointing to the Configuration class and loads all the various beans needed to showcase particular functionality.
3 |
//initializing the Application Context once per application.
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(AppConfig.class);//showcasing PreConstruct / PostDestroy hooks
BeanPostConstructPreDestroy beanPostConstructPreDestroy =
applicationContext.getBean(BeanPostConstructPreDestroy.class); beanPostConstructPreDestroy.sayHello();
}
}
Application Configuration file
|
---|
|
---|
|
---|
Notice that we don't need to annotate or otherwise mark our POJO since the bean declaration/definition is
happening in the Application Configuration class file.
Constructor Injection
|
---|
|
---|
Property Injection
Notice that the @Autowired annotation demarcates the setter method whose name follows the JavaBeans standard.
|
---|
System.out.println("Hello, World from BeanPropertyInjection !"); }
}
PostConstruct / PreDestroy hooks
@Component
publicclass BeanPostConstructPreDestroy {@PostConstruct
publicvoid pre(){
System.out.println("BeanPostConstructPreDestroy - PostConstruct"); }
For Example in Simple JDBC Application programmer is responsible for
1. Loading the driver class
6. Executing query
7. Closing the connection
So Why to choose Spring as struts is there
Strut is a framework which provide solution to web aspects only and struts is invasive in nature. Spring has many
3. Spring provides end to end project development: That means we can develop all the modules like business
layer, persistence layer.
Application, Enterprise Application.
7. Spring supports Aspect oriented Programming for cross cutting concerns.
2. Spring JDBC
3. Spring AOP
7 |
---|
Section 2.1: Introduction to Spring Core
Spring is a vast framework, so the Spring framework has been divided in several modules which makes spring lightweight. Some important modules are:
What is a dependency:
From project point of view, in a project or application multiple classes
are there with different functionality. and each classes required some
functionality of other classes.
Example:
|
---|
Here class Engine is required by class car so we can say class engine is dependent to class Car, So instead of we managing those dependency by Inheritance or creating object as fallows.
By Inheritance:
|
---|
By creating object of dependent class:
|
---|
|
---|
So instead of we managing dependency between classes spring core takes the responsibility dependency
management. But Some rule are there, The classes must be designed with some design technique that is Strategy
DTDC or Blue Dart courier service , So let me design a application which shows complete loosely coupled. The
Eclipse Directory as fallows:
|
|
---|---|
9 |
|
---|
|
---|
|
---|
|
---|
|
---|
//properties file
|
---|
|
---|
importcom.sdp.service.FlipKart;
|
---|
11 |
---|
Literal expressions
Supported types include strings, dates, numeric values (int, real, and hex), boolean and null.
Inline list
Invoking Methods
I am working with an Oracle database, I've attempted to create an example that should work for other databases, my Oracle example details issues with Oracle.
Section 4.1: SimpleJdbcCall creation
// Autowire your configuration, for example
@Value("${db.procedure.schema}")
String schema;
|
---|
withoutProcedureColumnMetaDataAccess() needed if you have overloaded procedure names or just don't want SimpleJdbcCall to validate against the database.
withReturnValue() if procedure has a return value. First value given to declareParameters defines the return value. Also, if your procedure is a function, use withFunctionName and executeFunction when executing.
Assuming your procedure output parameter is ref cursor, you will get this exception.
java.sql.SQLException: Invalid column type: 2012
Invalid SQL type for column; nested exception is java.sql.SQLException: Invalid SQL type for column
So we need to create a ResultSetExtractor that supports Oracle types. I will explain the reason for password after this code.
*
* Also, types such as {@link oracle.sql.TIMESTAMPTZ} require a Connection when processing * the ResultSet; {@link OracleCachedRowSet#getConnectionInternal()} requires a JNDI * DataSource name or the username and password to be set.*
* For now I decided to just set the password since changing SpringBoot to a JNDI DataSource * configuration is a bit complicated.
this.oraclePassword= oraclePassword;
|
---|
Certain Oracle types require a Connection to obtain the column value from a ResultSet. TIMESTAMPTZ is one of these types. So when rowSet.getTimestamp(colIndex) is called, you will get this exception:
Caused by: java.sql.SQLException: One or more of the authenticating RowSet properties not set at oracle.jdbc.rowset.OracleCachedRowSet.getConnectionInternal(OracleCachedRowSet.java:560) at oracle.jdbc.rowset.OracleCachedRowSet.getTimestamp(OracleCachedRowSet.java:3717) at org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet.getTimestamp
Chapter 5: Creating and using beans
Section 5.1: Autowiring all beans of a specific type
|
---|
|
---|
|
---|
|
---|
|
---|
You can now autowire these validators individually or together into a class.
Class:
|
---|
|
---|
|
---|
idea of a much easier validation method look up how Spring does validation with annotations.
Section 5.2: Basic annotation autowiring
Class:
|
---|
|
---|
You can gain access to this bean in any class that instantiated by the Spring IoC container using the @Autowired
annotation.
This annotation can be applied in several different ways.
Constructor injection:
17 |