Spring Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Downloading the Source Code and PDF

Files
Download Source Code:
This only includes the source files, no JAR files. You will need to add JAR files
separately on your own. You have two options for downloading the code.

Option 1: Download zip file


File: spring-and-hibernate-for-beginners-master.zip

Option 2: Download using git command-line


You also have the option to download source code using git command-line.

git clone https://github.com/darbyluv2code/spring-and-hibernate-for-beginners.git

Note: Option 1 and Option 2 both has links to the exact same code. You only need
to choose one of the options.

---
Download PDF Files
All slides which are shown during the course are available also as a reference and
can be downloaded here:

http://www.luv2code.com/download-spring-hibernate-pdfs

---

Download FAQ Notes PDF Files


The course also includes a collection of FAQ Notes. These are available for
download as PDFs:

http://www.luv2code.com/download-spring-hibernate-faq-notes

FAQ: What is a Spring Bean?


FAQ: What is a Spring Bean?
A "Spring Bean" is simply a Java object.

When Java objects 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.

---

Here's a blurb from the Spring Reference Manual

Source: https://docs.spring.io/spring/docs/current/spring-framework-reference/
core.html#beans-introduction
---

In the early days, there was a term called "Java Beans". Spring Beans have a
similar concept but Spring Beans do not follow all of the rigorous requirements of
Java Beans.

---

In summary, whenever you see "Spring Bean", just think Java object. :-)

FAQ: Why do we specify the Coach


interface in getBean()?
Question
Why do we specify the Coach interface in getBean()?

For example:

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


---

Answer
When we pass the interface to the method, behind the scenes Spring will cast the
object for you.

context.getBean("myCoach", Coach.class)   
However, there are some slight differences than normal casting.

From the Spring docs:


Behaves the same as getBean(String), but provides a measure of type safety by
throwing a BeanNotOfRequiredTypeException if the bean is not of the required
type. This means that ClassCastException can't be thrown on casting the result
correctly, as can happen with getBean(String).
Source:  http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/
springframework/beans/factory/BeanFactory.html#getBean-java.lang.String-
java.lang.Class-

FAQ: Why do we use CricketCoach


class instead of Coach Interface?
Question:
For the CricketCoach example with Setter Injection, why do we use the
CricketCoach class instead of the Coach interface?

Answer:
The getTeam() method is only defined in the CricketCoach class. It is not
part of the Coach interface.

As a result, you would need the following code:

     CricketCoach theCricketCoach = context.getBean("myCricketCoach",


CricketCoach.class);  
---

The Coach interface has two methods: getDailyWorkout and


getDailyFortune

The CricketCoach class has four methods: getDailyWorkout,


getDailyFortune, getTeam and setTeam

---

When you retrieve a bean from the Spring container using the Coach
interface:

     Coach theCricketCoach = context.getBean("myCricketCoach", Coach.class);  


You only have access to the methods defined in the Coach interface:
getDailyWorkout and getDailyFortune. Even though the actual
implementation has additional methods, you only have visibility to methods
that are defined at the Coach interface level.

---

When you retrieve a bean from the Spring container using the CricketCoach
class:

     CricketCoach theCricketCoach = context.getBean("myCricketCoach",


CricketCoach.class);  
You have access to the methods defined in the Coach interface:
getDailyWorkout and getDailyFortune.

ALSO, you have access to the additional methods defined in the


CricketCoach class: getTeam, setTeam.

---

The bottom line is it depends on how you retrieve the object and assign
it ... that determines the visibility you have to the methods.

Special Note: Defining init and


destroy methods - Method
Signatures
Special Note about init and destroy Method Signatures
When using XML configuration, I want to provide additional details
regarding the method signatures of the  init-method   and  destroy-method  .
Access modifier
The method can have any access modifier (public, protected, private)
Return type
The method can have any return type. However, "void' is most commonly
used. If you give a return type just note that you will not be able to capture
the return value. As a result, "void" is commonly used.
Method name
The method can have any method name.
Arguments
The method can not accept any arguments. The method should be no-arg.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy