Sunday, January 5, 2014

Java Collections

Articles on Collections

http://shivasoft.in/blog/java/complete-java-collection-tutorial-for-the-beginner/

http://www.programcreek.com/2009/02/the-interface-and-class-hierarchy-for-collections/

http://www.learn-java-tutorial.com/Collections.cfm#.UslS3yaIoRg




Legacy Classes are Vector,Stack,HashTable,Properties and are synchronized methods so these are thread safe.

Enumeration can be used on legacy classes only.
Iterator can be used on List and Set
ListIterator can be used on List only


To fetch the Items from Map we convert them into Set .In Map each Key and Value is considered as an entry and this entry is converted into a set using a method entrySet()

Spring FrameWork

Spring 1.x has 7 modules
    1. Spring Core Module
    2. Spring JDBCDAO
    3. Spring ORM Module
    4. Spring JEE Module
    5. Spring AOP Module
    6. Spring Web Module
    7. Spring MVC Module

Spring 2.x and 3.x has 6 modules
  1. Spring Core Module
  2. Spring JDBCDAO
  3. Spring ORM Module
  4. Spring JEE Module
  5. Spring AOP Module
  6. Spring Web MVC Module



Spring 5 has 
1. Spring Core Module (Beans, Core, SpEL, Context)
2. Spring AOP Module (AOP, Aspects, Instrumentation, Messaging)
3. Data Access Layer (JDBC, ORM, Transactions, OXM, JMS)
4. Web Layer (Servlet, WebSocket, Web, Portlet)
5. Test Layer (Unit, Integration, Mock)

Additional modules which we have like 
  Spring Cloud, Spring Data
  Spring Batch, Spring Security
  Spring for Android, Spring Web Flow
  Spring Web Services, Spring LDAP


Spring Core Module is heart of Spring and this is used for developing Business Logic
Spring JDBCDAO module is used for developing DAO classes and it uses JDBC API internally
Spring ORM Module is used for developing DAO classes and it uses Hibernate,JPA,Toplink internally
Spring AOP Module is used to give suggestions for developing Middleware services and it follow Proxy design Pattern


Download Spring jar from below
http://maven.springframework.org/release/org/springframework/spring/

Spring documentation
http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/htmlsingle/


Spring container 
Primary functions 
   Create and manage objects (Inversion of Control)
   Inject object's dependencies (Dependency Injection)


Configuring spring Container have three ways of configuration
   XML configuration file (legacy, but most legacy apps still use this)
   Java Annotations (modern)
   Java Source Code (modern)

Spring Development Process
   1. Configure your Spring Beans
   2. Create a Spring Container
   3. Retrieve Beans from Spring Container 

Configuring your Spring Beans

we have the configuration file name will be like applicationContext.xml
  <beans ..>
     <bean id="myCoach" class="com.luv2code.springdemo.BaseballCoach">
</beans>

Create a Spring Container
   Spring container is generically known as ApplicationContext
   Specialized implementations
      ClassPathXmlApplicationContext
      AnnotationConfigApplicationContext
     GenericWebapplicationContext

Eg: 
   ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml")

Retrieve Beans from Container

Coach theCoach= context.getBean("myCoach", Coach.class)

What is a Spring Bean?
A "Spring Bean" is simply a Java Object. 

When Java Object are created by the Spring Container, then Spring refers to them as "Spring Beans".

Spring Beans are created from normal Java classes... Just like Java objects

Injection Types
There are many types of injection with Spring
The two most common injections are 
   Constructor Injection
   Setter Injection
   Field Injection (for annotation we have third annotation)

Auto-wiring in Annotation 

Development Process - Constructor Injection
1. Define the dependency interface and class
2. Create a constructor in your class for injections
3. Configure the dependency injection in Spring config file 

Development Process - Setter Injection
1. Create setter method(s) in your class for injections
2. Configure the dependency injection in spring config file

Development Process - To read from properties File
1. Create Properties File
2. Load Properties File in spring config file
3. Reference values from Properties File

Bean Scopes
Scope refers to the lifecycle of a bean
How long does the bean live?
How many instances are created?
How is the bean shared?

Default Scope of Spring Bean is Singleton

What is a Singleton?
Spring Container creates only one instance of the bean, by default
It is cached in memory
All requests for the bean will return a SHARED reference to the SAME bean

Spring Bean Scopes 

Singleton          - Create a single shared instance of the bean. Default scope.
Prototype         - Creates a new bean instance for each container request.
request             - Scoped to an HTTP web request. Only used for web apps.
session              - Scoped to an HTTP web session. Only used for web apps.
global-session  - Scoped to global HTTP web session. Only used for Web apps

Example to define the bean scope when mentioning in the xml is 

<beans ...>
    <bean id="myCoach" class="com.luv2code.springdemo.TrackCoach" scope="prototype">
    </bean>
</beans>

Bean Lifecycle
Container Started --> 
    Bean Instantiated --> 
        Dependencies Injected --> 
            Internal Spring Processing --> 
                Your Custom Init method --> 
                    Bean is Ready for Use --> 
                        Container is Shutdown --> 
                            Your Custom Destroy Method --> 
                                    Stop

Bean Lifecycle Methods/Hooks
You can add custom code during bean initialization
   Calling custom business logic methods
   Setting up handles to resources ( db, socket, file etc)

You can add custom code during bean destruction
   Calling Custom business logic method
   Clean up handles to resources ( db, sockets, files etc)

Init:method  configuration & Destroy: method configuration

<beans ...>
    <bean id="myCoach" class="com.luv2code.springdemo.TrackCoacj" 
   init-method="doMyStartupStuff"  destory-method="doMyCleanupStuff">
</beans>

Prototype scoped beans in Spring does not call the destroy method. To handle the destruction of prototype bean we need to implement the interface DisposableBean. This interface has a method destroy() to override.


Java Annotations Spring configurations

What are Java Annotations?
Special labels/marker added to Java classes
Provide meta-data about the class
Processed at compile time or run-time for special processing

Why Spring Configuration with Annotations?
 XML configuration can be verbose
 Configure your spring beans with Annotations
Annotations minimizes the XML configuration

Scanning for Components classes
spring will scan your java classes for special annotations
Automatically register the beans in the spring container 

Spring also supports Default Dean ID
Default bean id : the class name, make first letter lower-case

What is Spring auto Wiring?
For dependency injection, spring can use auto wiring
Spring will look for a class that matches the property
   matches by type: class or interface
Spring will inject it automatically... hence it is known as autowired

If we have multiple implementations, then we will use @Qualifier 


Default Bean Names ... and the Special Case

In general, when using Annotations, for the default bean name, Spring uses the following rule.

If the annotation's value doesn't indicate a bean name, an appropriate name will be built based on the short name of the class (with the first letter lower-cased).

For example:

HappyFortuneService --> happyFortuneService

However, for the special case of when BOTH the first and second characters of the class name are upper case, then the name is NOT converted.

For the case of RESTFortuneService

RESTFortuneService --> RESTFortuneService

No conversion since the first two characters are upper case.


@scope("singleton") //this is default scope in spring
@scope("prototype")
@PostConstruct and @PreDestroy to implement before and during destroy


3 Ways of Configuration of Spring Container 

1. FULL XML config
2. XML Component Scan
3. Java Configuration Class

Java Configuration Class Development Process
a. Create a Java class and annotate as @Configuration
b. Add component scanning support @ComponentScan(optional)
c. Read Spring Java configuration class
d Retrieve bean from Spring container


Annotations in Spring 

@Configuration
@ComponentScan
@Bean

@Component
@Scope
@Autowired
@Qualifier

@PropertySource
@Value

@PostConstruct
@PreDestroy


*****************************************************************

CREATE TABLE student (
  id integer NOT NULL  GENERATED ALWAYS AS IDENTITY(START WITH 1, INCREMENT BY 1, NO MINVALUE, NO MAXVALUE, CACHE 10, ORDER),
  first_name varchar(45) DEFAULT NULL,
  last_name varchar(45) DEFAULT NULL,
  email varchar(45) DEFAULT NULL  
) ;

ALTER TABLE ptummala.student ADD CONSTRAINT "PK_student_id" PRIMARY KEY ("ID")
;