Skip to content

This repository contains simple demos of applications using the Java Spring Framework. It provides examples that showcase the features and functionalities of the framework.

Notifications You must be signed in to change notification settings

alimarangoz/Spring-Framework-Works

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Spring-Framework-Works

Bean Scope:

In the spring "Singleton" is using as default scope which means, there is an object created by spring and it uses same object for every operation in the same configuration. Also, "Prototype" is using in order to create new object for every operation in the same configuration.

checking-the-location

This is Singleton, there are 2 objects there and they are in the location in memory which means that they are the same object.

prototype-scope-definition

If scope changes using "scope";

prototype-result

They become different objects in the different memory allocation.


Bean Lifecycle:

Spring has own lifecycle, which means objects are created, used and destroyed. We can implement methods during the create and destroy using by "init-method" for create and "destroy-method" for destory.

init-destroy-Methods

Here, simply created 2 method to check init and destroy method in TrackCoach class.

add-config-file-destroy-and-init

Add to method in configuration file using "init-method" and "destroy-method".

result-of-lifecycle

First init-method displayed then object created, finally destroy method implemented, this is the lifecycle of Spring

Spring Annotation:

Spring has lots of annotation that helps to coding easily. For example; instead of create beans in configuration file, simply scan "Component" in base package and it takes these classes/constructors/setter methods/fields as a bean and directly retrieve data from spring container.

component-scan

This line for scanning component annotation in given package.

component-class

Spring create id here from class name like; class -> HappyFortuneService ; id -> happyFortuneService. If you want to add manually id, you can directly implement like @Component("idName").

Spring Annotation with Injections:

We can also implement Dependency Injections using annotations. Constructor Injection, Setter Injection, Field Injection are the different types of dependency injections that we can implement with annotations. The keyword to implement injection is "@Autowired". !!! You can also have different attributes to implement but if you write @Component for all of them. Application will crush so, "@Qualifier" keyword decide which implementation is comes first.

qualifier-2

In this example; DatabaseFortuneService,HappyFortuneService,RandomFortuneService,RESTFortuneService has @Component keyword so Compailor do not decide which is the first. So in FortuneService there is @Qualifier keyword to solve this problem.


Creating Bean with Annotation(No XML , Just Java Class):

It is easy to create xml file and retrieve bean from spring container. However, there is another way to manage this property which is the implementation without XML file. Configuration can create by using Annotation and Java Classes.

class-bean

Using "Configuration" annotation, class can act as configuration file, and It can contain beans with "Bean" annotation for retrieving data from them. !!!(If you want to work with whole package, you should scan all components with "ComponentScan" annotation.


config-class

When XML file implement in main class, ClassPathXmlApplicationContext class is using. But in this implementation, instead of ClassPathXmlApplicationContext, AnnotationConfigApplicationContext is using.

About

This repository contains simple demos of applications using the Java Spring Framework. It provides examples that showcase the features and functionalities of the framework.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages