Friday, 17 January 2020

Access attributes in component

NOTE: To access an attribute in a component, use expressions as {! v.<Attribute Name>}.
----------------------------------------------------------------------------------------------------------------------

<aura:component >
    <!--Basic types-->
    <aura:attribute name="ItemName" type="String" default="Bag"/>
    <aura:attribute name="SerialNumber" type="Long" default="1"/>
    <aura:attribute name="Quantity" type="Integer" default="2"/>
    <aura:attribute name="Price" type="Decimal" default="blue"/>
    <aura:attribute name="PurchaseDate" type="Date" default="2018-11-9"/>
    <aura:attribute name="Dispatched" type="Boolean" default="true"/>
    <aura:attribute name="Color" type="String" default="blue"/>
   
    <!--Collection types-->
    <aura:attribute name="ColorArray" type="String[]" default="['red', 'green', 'blue']"/>
    <aura:attribute name="ColorList" type="List" default="['red', 'green', 'blue']"/>
    <aura:attribute name="ColorMap" type="Map" default="{a: 'red', b: 'green', c: 'blue'}"/>
    <aura:attribute name="ColorSet" type="Set" default="['red', 'green', 'blue','red']"/>
   
    <!--Object types-->
    <aura:attribute name="PersonObject" type="Object" default="{'name': 'John', 'age': '25'}"/>
   
    <a><b>My component</b></a>
   
    <h1>This is a test component</h1>
    <p>Your component code goes here.</p>
    <p>
<!--Using collection Types-->
    <strong>Array values</strong>
        <aura:iteration items="{! v.ColorArray}" var="color">
        <div>{! color} </div>
        </aura:iteration>
       
        <strong>List values</strong>
        <aura:iteration items="{! v.ColorList}" var="color">
        <div>{! color} </div>
        </aura:iteration>
       
        <strong>Map values</strong>
       
        <div>{! v.ColorMap.a} </div>
        <div>{! v.ColorMap.b} </div>
        <div>{! v.ColorMap.c} </div>
       
        <strong>Set values</strong>
        <aura:iteration items="{! v.ColorSet}" var="color" indexVar="index">
        <div>{! color} </div>
        </aura:iteration>
    </p>

</aura:component>




Note: To access an attribute in a controller and helper, use expressions as {v.<Attribute Name>}:
=======================================================================

({
doInit : function(component, event, helper) {
//javascript to initialize the component
var color = component.get("v.Color");
        var Price = component.get("v.Price");
        console.log('color is ',color);
        console.log('Price is ',Price);
}

})

Note: When we using the attribute in Component, we must use the exclamatory sign. But when we access the attribute in Controller and Helper, we do not use exclamatory sign.

OUTPUT









How to declare different types of attributes in component

<aura:component >
    
    <!--Basic types-->
    <aura:attribute name="ItemName" type="String" default="Bag"/>
    <aura:attribute name="SerialNumber" type="Long" default="1"/>
    <aura:attribute name="Quantity" type="Integer" default="2"/>
    <aura:attribute name="Price" type="Decimal" default="4578.67"/>
    <aura:attribute name="PurchaseDate" type="Date" default="2018-11-9"/>
    <aura:attribute name="Dispatched" type="Boolean" default="true"/>
    <aura:attribute name="Color" type="String" default="blue"/>
    
    <!--Collection types-->
    <aura:attribute name="ColorArray" type="String[]" default="['red', 'green', 'blue']"/>
    <aura:attribute name="ColorList" type="List" default="['red', 'green', 'blue']"/>
    <aura:attribute name="ColorMap" type="Map" default="{a: 'red', b: 'green', c: 'blue'}"/>
    <aura:attribute name="ColorSet" type="Set" default="['red', 'green', 'blue','red']"/>
    
    <!--Object types-->
    <aura:attribute name="PersonObject" type="Object" default="{'name': 'John', 'age': '25'}"/>
    
    <p>Your component code goes here.</p>
    
</aura:component>



Note :  The type value of an attribute is case insensitive, and also when you use a particular attribute in an expression the name of the attribute should match with the name specified in attribute definition i.e. the name inside expressions is case sensitive.

Parameters of attribute:

Access : Indicates whether the attribute can be used outside of its own namespace. Possible values are public (default), and global, and private.
Name : The name of the attribute. It is required and must be provided.
Type  : Type of the attribute. It could be any of the type that we discussed above like String, Boolean etc.
Default : The default value that you want to provide to the attribute.
Required : It contains boolean value true or false.  The default value is false.

Description : A brief summary of the attribute and its usage.

What is aura attribute and what are the types in lightning?

Aura Attribute

====================================================================
An aura attribute is like a variable that we use in a programming language or in Apex class. These are typed fields and set on a specific instance of a component. These attributes can be used inside the component as expressions and in controller and helper as well.


Types of Attributes

=====================================================================

A: Basic Types: 
---------------------------------

These are the basic attributes that contain values of primitive types like integer, string, boolean etc.
1. String :- It stores simple text values like color, name etc.
2. Boolean :- It stores boolean values like true or false.
3. Decimal :- It stores values with fractional part. Used to store values like price.
4. Integer :- It stores integer/Number values like quantity etc.
5. Double :- It stores fractional values like price, currency values.
6. Date :- It stores date values in yyyy-mm-dd format.
7. DateTime :- It stores date values with timestamp.
8. Long :- It stores non fractional values with wider range than integer.


B: Collection Types:
--------------------------------
Lightning supports the collection type attributes like an array, list and map.
  1. Array : An array of items of a defined type
  2. List: An ordered collection of items.
  3. Map: A collection that maps keys to values. A map can’t contain duplicate keys. Each key can map to at most one value. Defaults to an empty object, {}.
  4. Set: A collection that contains no duplicate elements. The order for set items is not guaranteed. For example, “[‘red’, ‘green’, ‘blue’]” might be returned as blue, green, red.
Attributes of these types are typically used in iterations in lightning to display data.
C: Object Types
-----------------------------------
An attribute can have the type as “Object”. The object is stored in JSON format.

D: Apex Class Types
-----------------------------------
An attribute can have the type as “Apex Class”.

Tuesday, 20 August 2019

Spring - Session-1

Spring - Session-1
Video Link https://youtu.be/3oJ_gAbgDcs
===================================
About Spring Framework:

Developer: "developed by Rod Johnson in 2003"

Org: Spring Organization

Initial Name: Interface 21

Some Important Points About Spring Framework:
========================================================================
1. Spring is a lightweight framework (using Runtime Polyporphism, HAS-A).

It is main alternative of EJB Application which is Heavy Weight and Application Server Dependent.

2. Main advantages of Spring frameword is it provides "Loosly Coupled" approach - while
EJB provides "Tightly Coupled" which is NOT much suitable for programming.

========================================================================

Spring Module/Containers
============================
1. IoC (Inversion of Controlling) Module, contains the following:
---------------------------------------------------------------------------
1. Core Container - Interface name is: BeanFactory ------XmlBeanFactory (Implementation class)
2. J2EE Container - Interface name is: ApplicationContext ------ConfigurableApplicationContext(Child Interface)----ClassPathXmlApplicationContext (Implementation class)

The main job of container is : reads the data from xml file and pass to POJO class.

2. MVC Module, contains the following:
------------------------------------------------------------
1. Web Container - Interface name is: WebApplicationContext  --- WebApplicationContextUtil (Implementation class). It is a Factory class like DriverManager class.

IoC Container:
==============================================================
Same like ServletContainer It performs the following operations:
1. Read xml file
2. create instances of xml POJO
3. It will manage the life cycle of POJO
4. Dynamic parameter supply to POJO - this concept is known as dependency injection


How to start these Container or classes like tomcat container:
=================================================================
Main class or Driver class - that will drive/start the whole application, like:
class Test {
public static void main(String args[]) {
new XmlBeanFactory();// to start core container
new ClasspathXmlApplicationContext();//to start j2ee container
new WebApplicationContextUtil();// to start web container
}
}

========================================================================
First Spring Application:

We need the following components:
========================================
1. POJO class
2. xml file
3. Driver/Main class


Let's start from POJO class:
==============================================
public class Helloworld {
public void sayHello() {
System.out.println("Hello world Spring Application");
}
}
===============================================================


spring.xml - file
===============================================================
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<bean id="hw" class="beans.HelloWorld"/>
</beans>

NOte: Resource r = new ClassPathResource("resources/spring.xml");//It is like File class in IO.
It is used to load the xml file.


================================================================
Main/Driver class
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.HelloWorld;

public class Client2 {

public static void main(String[] args) {
//find and load xml file
//eager loading - pre
ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring.xml");

Object ob1=context.getBean("hw");
Object ob2=context.getBean("hw");
Object ob3=context.getBean("hw");
Object ob4=context.getBean("hw");
System.out.println(ob1);
System.out.println(ob2);
System.out.println(ob3);
System.out.println(ob4);


}

}


=======================================================================

IDE:
-------------------
1. Eclipse
2. MyEclipse
3. STS
4. NetBeans
5. RAD

========================================================================
scope of Object/POJO class object

1. Singleton (bydefault)
2. Prototype - for each request creates new object.
for Prototype approach we need to change the xml file:

Case 1 - If you are using default DTD: then
<bean singleton="true">


Case 2 - If you are using 2.0 or above DTD then
<bean scope="singleton or prototype">


xml files contains 5000 POJO class enteries:
BeanFactory class - User1 - 5 mins
User1 - 5 mins.




ApplicationContext 5000 class object will create at deployment time


https://youtu.be/3oJ_gAbgDcs

Tuesday, 23 July 2019

Java Constructor

Constructors in Java

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.
It is a special type of method which is used to initialize the object.
Every time an object is created using the new() keyword, at least one constructor is called.
It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.
There are two types of constructors in Java: no-arg constructor, and parameterized constructor.
Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any.

Rules for creating Java constructor

There are two rules defined for the constructor.
  1. Constructor name must be the same as its class name
  2. A Constructor must have no explicit return type
  3. A Java constructor cannot be abstract, static, final, and synchronized

Note: We can use access modifiers while declaring a constructor. It controls the object creation. In other words, we can have private, protected, public or default constructor in Java.

Types of Java constructors

There are two types of constructors in Java:
  1. Default constructor (no-arg constructor)
  2. Parameterized constructor
Java Constructors

Java Default Constructor

A constructor is called "Default Constructor" when it doesn't have any parameter.

Syntax of default constructor:

  1. <class_name>(){}  

Example of default constructor

In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.
  1. //Java Program to create and call a default constructor  
  2. class Bike1{  
  3. //creating a default constructor  
  4. Bike1(){System.out.println("Bike is created");}  
  5. //main method  
  6. public static void main(String args[]){  
  7. //calling a default constructor  
  8. Bike1 b=new Bike1();  
  9. }  
  10. }  
Output:
Bike is created

Rule: If there is no constructor in a class, compiler automatically creates a default constructor.

Java default constructor

Q) What is the purpose of a default constructor?

The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.

Example of default constructor that displays the default values

  1. //Let us see another example of default constructor  
  2. //which displays the default values  
  3. class Student3{  
  4. int id;  
  5. String name;  
  6. //method to display the value of id and name  
  7. void display(){System.out.println(id+" "+name);}  
  8.   
  9. public static void main(String args[]){  
  10. //creating objects  
  11. Student3 s1=new Student3();  
  12. Student3 s2=new Student3();  
  13. //displaying values of the object  
  14. s1.display();  
  15. s2.display();  
  16. }  
  17. }  
Output:
0 null
0 null
Explanation:In the above class,you are not creating any constructor so compiler provides you a default constructor. Here 0 and null values are provided by default constructor.

Java Parameterized Constructor

A constructor which has a specific number of parameters is called a parameterized constructor.

Why use the parameterized constructor?

The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.

Example of parameterized constructor

In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
  1. //Java Program to demonstrate the use of the parameterized constructor.  
  2. class Student4{  
  3.     int id;  
  4.     String name;  
  5.     //creating a parameterized constructor  
  6.     Student4(int i,String n){  
  7.     id = i;  
  8.     name = n;  
  9.     }  
  10.     //method to display the values  
  11.     void display(){System.out.println(id+" "+name);}  
  12.    
  13.     public static void main(String args[]){  
  14.     //creating objects and passing values  
  15.     Student4 s1 = new Student4(111,"Karan");  
  16.     Student4 s2 = new Student4(222,"Aryan");  
  17.     //calling method to display the values of object  
  18.     s1.display();  
  19.     s2.display();  
  20.    }  
  21. }  
Output:
111 Karan
222 Aryan

Constructor Overloading in Java

In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.

Example of Constructor Overloading

  1. //Java program to overload constructors  
  2. class Student5{  
  3.     int id;  
  4.     String name;  
  5.     int age;  
  6.     //creating two arg constructor  
  7.     Student5(int i,String n){  
  8.     id = i;  
  9.     name = n;  
  10.     }  
  11.     //creating three arg constructor  
  12.     Student5(int i,String n,int a){  
  13.     id = i;  
  14.     name = n;  
  15.     age=a;  
  16.     }  
  17.     void display(){System.out.println(id+" "+name+" "+age);}  
  18.    
  19.     public static void main(String args[]){  
  20.     Student5 s1 = new Student5(111,"Karan");  
  21.     Student5 s2 = new Student5(222,"Aryan",25);  
  22.     s1.display();  
  23.     s2.display();  
  24.    }  
  25. }  
Output:
111 Karan 0
222 Aryan 25

Difference between constructor and method in Java

There are many differences between constructors and methods. They are given below.
Java ConstructorJava Method
A constructor is used to initialize the state of an object.A method is used to expose the behavior of an object.
A constructor must not have a return type.A method must have a return type.
The constructor is invoked implicitly.The method is invoked explicitly.
The Java compiler provides a default constructor if you don't have any constructor in a class.The method is not provided by the compiler in any case.
The constructor name must be same as the class name.The method name may or may not be same as the class name.

Java Constructors vs. Methods

Java Copy Constructor

There is no copy constructor in Java. However, we can copy the values from one object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in Java. They are:
  • By constructor
  • By assigning the values of one object into another
  • By clone() method of Object class
In this example, we are going to copy the values of one object into another using Java constructor.
  1. //Java program to initialize the values from one object to another object.  
  2. class Student6{  
  3.     int id;  
  4.     String name;  
  5.     //constructor to initialize integer and string  
  6.     Student6(int i,String n){  
  7.     id = i;  
  8.     name = n;  
  9.     }  
  10.     //constructor to initialize another object  
  11.     Student6(Student6 s){  
  12.     id = s.id;  
  13.     name =s.name;  
  14.     }  
  15.     void display(){System.out.println(id+" "+name);}  
  16.    
  17.     public static void main(String args[]){  
  18.     Student6 s1 = new Student6(111,"Karan");  
  19.     Student6 s2 = new Student6(s1);  
  20.     s1.display();  
  21.     s2.display();  
  22.    }  
  23. }  
Output:
111 Karan
111 Karan

Copying values without constructor

We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor.
  1. class Student7{  
  2.     int id;  
  3.     String name;  
  4.     Student7(int i,String n){  
  5.     id = i;  
  6.     name = n;  
  7.     }  
  8.     Student7(){}  
  9.     void display(){System.out.println(id+" "+name);}  
  10.    
  11.     public static void main(String args[]){  
  12.     Student7 s1 = new Student7(111,"Karan");  
  13.     Student7 s2 = new Student7();  
  14.     s2.id=s1.id;  
  15.     s2.name=s1.name;  
  16.     s1.display();  
  17.     s2.display();  
  18.    }  
  19. }  
Output:
111 Karan
111 Karan

Q) Does constructor return any value?

Yes, it is the current class instance (You cannot use return type yet it returns a value).

Can constructor perform other tasks instead of initialization?

Yes, like object creation, starting a thread, calling a method, etc. You can perform any operation in the constructor as you perform in the method.

Is there Constructor class in Java?

Yes.

What is the purpose of Constructor class?

Java provides a Constructor class which can be used to get the internal information of a constructor in the class. It is found in the java.lang.reflect package.

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...