10. Write x86 ALP to find the factorial of a given integer number on a command line by using recursion. Explicit stack manipulation is expected in the code.

;Problem Statement: Write x86 ALP to find the factorial of a given integer ;number on a command line by using
;recursion. Explicit stack manipulation is expected in the code.

%macro scall 4   ;common macro for input/output
 mov rax,%1
 mov rdi,%2
 mov rsi,%3
 mov rdx,%4
 syscall
%endmacro

section .data
 num db 00h
 msg db “Factorial is : “
 msglen equ $-msg
 msg1 db “*****Program to find Factorial of a number***** “,0Ah
   db “Enter the number : “,
 msg1len equ $-msg1

 zerofact db ” 00000001 “
 zerofactlen equ $-zerofact

section .bss
 dispnum resb 16
 result resb 4
 temp resb 3

section .text
global _start
_start:

 scall 1,1,msg1,msg1len
 scall 0,0,temp,3   ;accept number from user
 call convert       ;convert number from ascii to hex
 mov [num],dl

 scall 1,1,msg,msglen

 mov rdx,0        ;RMD
 mov rax,0        ;RMD
 mov al,[num]   ;store number in accumulator
 cmp al,01h 
 jbe endfact
 mov rbx,0        ;RMD
 mov bl,01h
 call factr    ;call factorial procedure
 call display

 call exit
endfact:
 scall 1,1,zerofact,zerofactlen
 call exit

 factr:    ;recursive procedure

   cmp rax,01h
   je retcon1
   push rax 
   dec rax

   call factr

  retcon:
   pop rbx
   mul ebx
   jmp endpr

  retcon1:   ;if rax=1 return
   pop rbx
   jmp retcon
  endpr:

 ret

 display:   ; procedure to convert hex to ascii

   mov rsi,dispnum+15
   mov rcx,0        ;RMD
   mov cl,16

  cont:
   mov rdx,0        ;RMD
   mov rbx,0        ;RMD
   mov bl,10h
   div ebx
   cmp dl,09h
   jbe skip
   add dl,07h
  skip:
   add dl,30h
   mov [rsi],dl
   dec rsi
   loop cont

   scall 1,1,dispnum,16

 ret

 convert:   ;procedure to convert ascii to hex
   mov rsi,temp
   mov cl,02h
   MOV rax,0        ;RMD
   mov rdx,0        ;RMD
  contc:
   rol dl,04h
   mov al,[rsi]
   cmp al,39h
   jbe skipc
   sub al,07h
  skipc:
   sub al,30h
   add dl,al
   inc rsi
   dec cl
   jnz contc

 ret

 exit:    ;exit system call

   mov rax,60
   mov rdi,0
   syscall

ret

OUTPUT:
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ nasm -f elf64 fact.asm
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ ld -o fact fact.o
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$ ./fact
*****Program to find Factorial of a number*****
Enter the number : 03
Factorial is : 0000000000000006
swlab@swlab-Veriton-M200-H81:~/Desktop/dhokane$

11. Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation Also plot the histogram for the data set.

section .data

msg1 db 10,13,’mean is:  ‘
msg1len equ $- msg1

msg2 db 10,13, ‘std deviation is:’
msg2len equ $- msg2

msg3 db 10,13, ‘variance is:’
msg3len equ $- msg3

data dd 9.0,1.0
datacnt dw 02
hdec dq 100

decpt db ‘.’

section .bss

res rest 01
mean resd 01
var resd 01
dispbuff resb 01

%macro disp 2
    mov eax,04
    mov ebx,01
    mov ecx,%1
    mov edx,%2
    int 80h
%endmacro
%macro accept 2
    mov eax,03
    mov ebx,00
    mov ecx,%1
    mov edx,%2
    int 80h
%endmacro

section .text
global _start
_start:

disp msg1,msg1len

       finit
    fldz
    mov rbx,data
    mov rsi,00
    xor rcx,rcx
    mov cx,[datacnt]

bk:    fadd dword [rbx+rsi*4]
    inc rsi
    loop bk

    fidiv word[datacnt]
    fst dword[mean]

    call dispres

    MOV RCX,00
    MOV CX,[datacnt]
    MOV RBX,data
    MOV RSI,00
    FLDZ
up1:    FLDZ
    FLD DWORD[RBX+RSI*4]
    FSUB DWORD[mean]
    FST ST1
    FMUL
    FADD
    INC RSI
    LOOP up1
    FIDIV word[datacnt]
    FST dWORD[var]
    FSQRT

    disp msg2,msg2len
    CALL dispres

    FLD dWORD[var]
    disp msg3,msg3len
    CALL dispres


exit:     mov eax,01
    mov ebx,00
    int 80h

disp8_proc:
    mov rdi,dispbuff
    mov rcx,02
back:    rol bl,04
    mov dl,bl
    and dl,0FH
    cmp dl,09
    jbe next1
    add dl,07H
next1:  add dl,30H
    mov [rdi],dl
    inc rdi
    loop back
    ret

dispres:
    fimul dword[hdec]
    fbstp tword[res]
    xor rcx,rcx
    mov rcx,09H
    mov rsi,res+9
up2:    push rcx
    push rsi
    mov bl,[rsi]
    call disp8_proc
    disp dispbuff,2 
    pop rsi
    dec rsi
    pop rcx
    loop up2
    disp decpt,1

    mov bl,[res]
    call disp8_proc
    disp dispbuff,2 
    ret

OUTPUT:
swlab@swlab-H81-M1:~$ nasm -f elf64 mean.asm
swlab@swlab-H81-M1:~$ ld -o mean mean.o
swlab@swlab-H81-M1:~$ ./mean

mean is:  000000000000000005.00
std deviation is:000000000000000004.00
variance is:000000000000000016.00

12. Write 80387 ALP to find the roots of the quadratic equation. All the possible cas es must be considered in calculating the roots.

section .data
msg1 db 10,13,”Complex Root”
msglen1 equ $-msg1

msg2 db 10,13,” Root1: “
msglen2 equ $-msg2

msg3 db 10,13,” Root2: “
msglen3 equ $-msg3

a dd 1.00
b dd 8.00
c dd 15.00
four dd 4.00
two dd 2.00

hdec dq 100
point db “.”

section .bss

    root1 resd 1
    root2 resd 1
    resbuff rest 1
    temp resb 2
    disc resd 1

%macro write 2            ;macro for display
    mov rax,1
    mov rdi,1
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

%macro read 2            ;macro for input
    mov rax,0
    mov rdi,0
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

%macro exit 0            ;macro for exit
    mov rax,60
    xor rdi,rdi
    syscall
%endmacro

section .text
  global _start
  _start:

  finit                ; initialise 80387 co-processor
  fld dword[b]            ; stack: b

  fmul dword[b]             ; stack: b*b

  fld dword[a]            ; stack: a, b*b

  fmul dword[c]             ; stack: a*c, b*b

  fmul dword[four]        ; stack: 4*a*c,b*b

  fsub                 ; stack: b*b – 4*a*c

  ftst                 ; compares ST0 and 0


  fstsw ax                ;Stores the coprocessor status word ;into either a word in memory or the AX register
  sahf        ;Stores the AH register into the FLAGS register.
  jb no_real_solutions         ; if disc < 0, no real solutions
  fsqrt             ; stack: sqrt(b*b – 4*a*c)

  fst dword[disc]     ; store disc= sqrt(b*b – 4*a*c)

  fsub dword[b]                 ; stack: disc-b

  fdiv dword[a]             ; stack: disc-b/2*a or (-b+disc)/2a

  fdiv dword[two]

  write msg2,msglen2
 call disp_proc
  fldz                ;stack:0
  fsub dword[disc]        ;stack:-disc
  fsub dword[b]             ; stack: -disc – b
  fdiv dword[a]        ; stack: (-b – disc)/(2*a)
  fdiv dword[two]

  write msg3,msglen3
  call disp_proc
  jmp exi

no_real_solutions:
write msg1,msglen1
exi :

mov rax,60
mov rdi,1
syscall

disp_proc:
    FIMUL dword[hdec]
    FBSTP tword[resbuff]
    mov rsi,resbuff+9
    mov rcx,09
  next1:

      push rcx
      push rsi

      mov bl,[rsi]
      call disp

      pop rsi
      pop rcx

       dec rsi
      loop next1
    push rsi
      write point,1
    pop rsi
      mov bl,[rsi]
      call disp
    ret

disp:
        mov edi,temp                ;mov dnum address into edi
        mov ecx,02                    ;initialize ecx with 2
        dispup1:
            rol bl,4                ;rotate bl by 4 bits
            mov dl,bl                ;move bl into dl
            and dl,0fh            ;and of dl with 0fh
            add dl,30h            ;add 30h into dl
            cmp dl,39h            ;compare dl with 39h
            jbe dispskip1            ;jump if below and equal to dispskip1
            add dl,07h            ;add 7h into dl
            dispskip1:
                mov [edi],dl        ;mov dl into dnum
                inc edi            ;increament edi by a byte
            loop dispup1        ;loop dispup1 while ecx not zero
            write temp,2            ;Display dnum by calling macro
          ret                    ;return from procedure

OUTPUT:

swlab@swlab-H81-M1:~$ nasm -f elf64 square.asm
swlab@swlab-H81-M1:~$
 ld -o mean mean.o
swlab@swlab-H81-M1:~$
 ./squareRoot1: 800000000000000003.00
Root2: 800000000000000005.00
swlab@swlab-H81-M1:~$

IEEE 802.15.4 PHY Layer

 

PropertyRange

 

Raw Data Rate868 MHz:20 Kb/s;

915 MHz:40 Kb/s;

2.4 GHz: 250 Kb/s

Range10-20 m
LatencyDown to 15ms
Channels

 

868/915 MHz: 11 Channels, 2.4 GHz: 16 Channels
Frequency BandTwo PHYs: 868 MHz/915 MHz and 2.4 GHz
AddressingShort 8-bit or 64-bit IEEE
Channel AccessCSMA-CA and slotted CSMA-CA
TemperatureIndustrial temperature range -40 to +85 C

 

The Physical (PHY) layer is concerned with the interface to the physical transmission medium (radio, in this case), exchanging data bits with this medium as well as with the layer above (the MAC sub layer). More specifically, its responsibilities towards the physical radio medium include:

1. Channel assessment

2. Bit level communications (bit modulation, bit demodulation, packet synchronization)

The PHY layer also offers the following services to the MAC sub layer:

PHY Data Service: Provides a mechanism for passing data to and from the MAC sub layer.PHY Management Services: Provides mechanisms to control radio communication settings and functionality from the MAC sub layer.

PHY functionalities:

1) Activation and deactivation of the radio transceiver

2) Energy detection within the current channel

3) Link quality indication for received packets

4) Clear channel assessment for CSMA/CA

5) Channel frequency selection

6) Data transmission and reception

The operating frequency bands are depicted in the figure below.

channel-structure
The standard specifies two PHYs:
1) 868 MHz/915 MHz direct sequence spread spectrum (DSSS) PHY (11 channels)
    1 channel (20Kb/s) in European 868MHz band
    10 channels (40Kb/s) in 915 (902-928) MHz ISM band
2) 2450 MHz direct sequence spread spectrum (DSSS) PHY (16 channels)
    16 channels (250Kb/s) in 2.4GHz band
    2.4 GHz PHY can be used to attain higher throughput and lower latency / lower duty cycle.
phy-layer

PHY packet fields:

1) Preamble (32 bits) – synchronization

2) Start of packet delimiter (8 bits) – shall be formatted as “11100101”

3) PHY header (8 bits) – PSDU length

4) PSDU (0 to 127 bytes) – data field

The packet structure of the IEEE 802.15.4 PHY layer is depicted in Figure above. The first field of this structure contains a 32-bit preamble. This field is used for symbol synchronization. The next field represents the start of a packet delimiter. This field of 8 bits is used for frame synchronization. The 8-bit PHY header field specifies the length of the PHY service data unit (PSDU). The PSDU field can carry up to 127 bytes of data.

previous-button-hinext

IEEE 802.15.4: CASE STUDY

The main design objective of the IEEE 802.15.4 open standard is to support the wireless connectivity of a vast number of industrial, home, and medical applications, including automotive monitoring and control, home automation, ubiquitous and pervasive health care, gaming, connecting devices to a PC, and sensor rich environments.

Such applications require a small, low cost, highly reliable technology that offers long battery life, measured in months or even years, and automatic or semiautomatic installation. The IEEE 802.15.4 standard supports these requirements by trading off higher speed and performance for architectures that benefit from low power consumption and low cost.

Most of these applications require only low to medium bit rates (up to some few hundreds of kbps), moderate average delays without too stringent delay guarantees, and for certain nodes it is highly desirable to reduce the energy consumption to a minimum. The physical layer offers bit rates of 20 kbps (a single channel in the frequency range 868 –868.6 MHz), 40 kbps (ten channels in the range between 905 and 928 MHz) and 250 kbps (16 channels in the 2.4 GHz ISM band between 2.4 and 2.485 GHz with 5-MHz spacing between the center frequencies). There are a total of 27 channels available, but the MAC protocol uses only one of these channels at a time; it is not a multi channel protocol.

new-bitmap-image
zigbee
 next

How to inject java.util.Properties into a spring bean

previous-button-hisubmit-hi

 

Que: -How to inject java.util.Properties into a spring bean?

Properties object is similar to Map based collection, only difference is keys also strings and values also strings. In case of Map based collection key can be any object and value also can be any object.

To inject Properties into a spring bean following steps are involved: –

Step 1: –Make java.util.Properties type variable as dependency in the spring bean.

Step 2:-To indicate the spring container to create the Properties object make use of <props> tag.

Step 3: –To specify each element of the Properties object make use of <props> tag.

e.g.: –

<bean name=”h” class=”com.nit.spring.HelloBean”>
 <property name=”properties”>
   <props>
     <prop key=”name”> Rama < /prop>
        <prop key=”address”> Hyderabad < /prop>
   < /props>
 < /property>
< /bean>

Note: –value of the key attribute becomes the key of the properties of Properties object. The content of <prop> tag become value of the property of Properties object.

Que: -Develop an enterprise application in which, all three layers are implemented?

Data Access Layer: –

  1. AccountDAOInterface.java (float getBalance (int accno))
  2. AccountDAO.java
  3. AccountNotFoundException.java
  4. DataAccessException.java
  • AccountDAO class has “javax.sql.DataSource” as dependency.
  • DataSouurce (interface) object provides database connection to the AccountDAO class.
  • DataSource is alternative to DriverManager to providing database connections to java applications.

In a spring application DataSource is used following the given steps: –

Step 1: –Make java.sql.DataSource variable as dependency and write appropriate constructor or setter method for spring container to implement setter injection or constructor injection.

For example: –

public class AccountDAO
{
     private DataSource datasource;
     public void setDataSource (DataSource datasource)
   {
       this.datasource=datasource;
   }
   ---------------------------
}

Step 2: –configure the implementation class DataSource interface as spring bean in configuration file.

e.g.: –

<bean id=”dataSource”
   Class=”org.apache.commons.dbcp.BasicDataSource”>
       <property name=”driverClassName”
          value=”oracle.jdbc.driver.OracleDriver” />
       <property name=”url”
          value=”jdbc:oracle:thin:@localhost:1521:server” />
       <property name=”username” value=”scott” />
       <property name=”password” value=”tiger” />
< /bean>

To make use of BasicDataSource as dependency in a spring application we need to place two jar files in classpath.

  1. commons-dbcp.jar
  2. commons-pool.jar

Note: –As we are using Oracle-Driver we need to place classes12.jar also in classpath.

Step 3: –In DAO methods get the database connection using “datasource”.

e.g.: –

public class AccountDAO
{
     DataSource datasource;
     --------------------------
     Public float getBalance (int accno) throws
AccountNotFoundException, DataAccessException
     {
           Connection con=datasource.getConnetion ();
           -----------------------
           -----------------------
     }
}

previous-button-hisubmit-hi

Map based collection example and Properties example

previous-button-hisubmit-hi

Que: -Map based collection example.

  1. Student.java
  2. College.java
  3. bean.xml
  4. ExecuteBean.java
package com.nit.spring;

public class Student
{
   private int rollno;
   private String name;
   public void setRollno (int rollno)
   {
       this.rollno=rollno;
   }
   public int getRollno ()
   {
       return rollno;
   }
   public void setName (String name)
   {
         this.name=name;
   }
     public String getName ()
     {
           return name;
     }
       public void displayStudentDetails ()
    {
         System.out.println (“Rollno:”+ rollno);
         System.out.println (“Name:”+ name);
    }
}

————————————————————

package com.nit.spring;
import java.util.Map;

public class College
{
     private Map <Integer, Student> students;
     public void setStudents (Map<Integer, Student> students)  //HashMap object is created
     {
         this.students=students;
     }
     public Map <Integer, Student> getStudents ()
   {
         return students;
   }
}

————————————————————

bean.xml

<beans>
 <bean name=”c” class “com.nit.spring.College”>
  <property name=”students”>
   <map> //knowing that HashMap object have to be create
    <entry key-“1001”>
     <bean id=”s1” class=”com.nit.spring.Student”>
      <property name=”rollno” value=”1001”/>
       <property name=”name” value=”Rama” /> 
< /bean>
< /entry>
   <entry key-“1002”>
    <bean id=”s2” class=”com.nit.spring.Student”>
     <property name=”rollno” value=”1002” />
      <property name=”name” value=”Rahim” />
< /bean>
   < /entry>
    <entry key-“1003”>
     <bean id=”s3” class=”com.nit.spring.Student”>
       <property name=”rollno” value=”1003” />
        <property name=”name” value=”David” />
< /bean>
< /entry>
< /map>
< /property>
< /bean>
< /beans>

————————————————————

ExecuteBean

public class ExecuteBean
{
     public static void main (String args[]) throws Exception
     {
BeanFactory beanfactory=new XmlBeanFactory (new FileSystemResource (“collection.xml”));
       College college= (College) beanfactory.getBean (“c”);
       Map<Integer,Student> students=college.getStudents ();
       Set <Integer> keys=students.keySet (); //HashSet object created
               int count=1;
             for (Integer k:keys)
             {
                 Student s=(Student) students.get (k);
                 System.out.println (“Student”+count+”details…”);
                 s.displayStudentDetails ();
                 count++;
             }
     }
}

Que: -Properties example

  1. HelloBean.java
  2. bean.xml
  3. Main.java
package com.nit.spring;
import java.util.Propertis;

public class HelloBean
{
     Properties properties;
     public void setProperties (Properties properties)
     {
         this.properties=properties;
     }
       public Properties getProperties ()
       {
           return properties;
       }
}

————————————————————

bean.xml

<beans>
     <bean name=”h” class=”com.nit.spring.HelloBean”>
           <property name=”properties”>
                 <props> //indicating the container to create the Properties object
                         <prop key=”name”>Rama< /prop>
                         <prop key=”address”>Hyderabad< /prop>
                 < /props>
           < /property>
     < /bean>
< /beans>

————————————————————

public class Main
{
     public static void main (String s []) throws Exception
     {
         BeanFactory beanfactory=new XmlBeanFactory (new FileSystemResource (“collection.xml”));
         HelloBean myBean= (HelloBean) beanfactory.getBean (“h”);
         Properties p=myBean.getProperties ();
         Set (String> keys=p.stringPropertiesNames () ;
         for (String key:keys)
         {
                 System.out.println (“key+”:”+p.getProperties (key));
         }
     }
}

previous-button-hisubmit-hi

set based collection example.

previous-button-hisubmit-hi

Que: -set based collection example.

  1. College.java
  2. bean.xml
  3. ExecuteBean.java
  4. Student.java

College.java

package com.nit.spring;
import java.util.set;

public class College
{
private String name;
private Set <Student>students;
public College (String name)
{
this.name=name;
}
public void setStudents (Set students)
{
this.students=students;
}
public Set getStudents ()
{
return students;
}
}

————————————————————

Student.java

package com.nit.spring;

public class Student
{
private int rollno;
private String name;
public Student (int rollno, String name)
{
this.rollno=rollno;
this.name=name;
}
public void displayStudentDetails ()
{
System.out.println (“Rollno:”+ rollno);
System.out.println (“Name:”+ name);
}
}

————————————————————

bean.xml:

< ? xml version=”1.0” encoding=”UTF-8”?>
<beans>
xmlns= “http://www.springframework.org/shema/beans”
xmlns:xsi= “http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation= “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd”>
<bean id= “s1” class= “com.nit.spring.Student”>
<constructor-arg type= “int” index=”0” value=”1001” />
<constructor-arg value=”Rama” />
< /bean>
<bean id= “s2” class= “com.nit.spring.Student”>
<constructor-arg type= “int” index=”0” value=”1002” />
<constructor-arg value=”Rahim” />
< /bean>
<bean id= “s3” class= “com.nit.spring.Student”>
<constructor-arg type= “int” index=”0” value=”1003” />
<constructor-arg value=”David” />
< /bean>
<bean name= “c” class= “com.nit.spring.Student”>
<constructor-arg value=”Rama” />
<property name=”students”>
<set>
<ref bean=”s1” />
<ref bean=”s2” />
<ref local=”s3” />
<ref local=”s3” />
< /set>
< /property>
< /bean>
< /beans>

————————————————————

public class ExecuteBean
{
public static void main (String args[]) throws Exception
{
BeanFactory beanfactory=new XmlBeanFactory (new FileSystemResource (“collection.xml”));
College college= (College) beanfactory.getBean (“c”);
Set <Student> students=college.getStudents ();
int count=1;
for (Student s:students)
{
System.out.println (“Student”+count+”details…”);
s.displayStudentDetails ();
count++;
}
}
}

previous-button-hisubmit-hi

How to configure setbased collection as dependency to a spring bean

previous-button-hisubmit-hi

 

Que: -How to configure setbased collection as dependency to a spring bean? Give example

Step 1: –In the spring bean make a list based or set based collection variable as dependency.

e.g.: –

public class College
{
List<Student> students;
Or
Set<Student> students;
----------------
----------------
}

Step 2: –Make use of <set> tag in the beans-configuration file.

Note: –In case of list based collection, duplicates are allowed. Whereas in case of set based collections duplicates are not allowed.

e.g.: –

<set>
<ref bean=”s1”/>
<ref local=”s2”/>
<ref local=”s3”/>
<ref local=”s4”/>
< /set>

In a spring application multiple configuration files. While specifying the elements to a collection we make use of <ref> tag. For this tag we can use “local” attribute instead of “bean” attribute if the dependency bean is also configured in the same xml file. If the dependency bean is configured in another configuration file, “local” attribute can’t be used, we must use “bean” attribute only.

Que: -How to inject a map based collection into a spring bean?

Step 1: –In the spring bean class declare “java.util.Map” type variable as dependency.

e.g.: –

public class College
{
Map<Integer, Student> student;
-------------------
-------------------
}

Step 2: –Make use of <map> tag in spring configuration file as given in the example case.

e.g.: –

<map>
<entry key=”1001”>
<bean id=”s1” class=”com.nareshit.Students”>
<property name=”rollno” value=”1001”>
<property name=”name” value=”Rama”>
< /bean>
< /entry>
---------------
---------------
< /map>

previous-button-hisubmit-hi

Spring container call the instanceFactory() of the spring bean

previous-button-hisubmit-hi

 

Que: -Modify the above application in such a way that spring container call the instanceFactory() of the spring bean.

instance Factorymethod example

  1. beans.xml
  2. HelloBean.java
  3. ExecuteBean.java

 

Note: –ExecuteBean.java and HelloBean.java from previous application expect making getInstance () method as non-static method.

beans.xml”

<beans>
<bean name=”hb” class=”HelloBean” >
<constructor-arg value=”hello”>
< /bean>
<bean name=”hb1” class=”HelloBean” factory-bean=”hb” factory-method=”getInstance”/>
< /beans>

Que: -How to specify same user-defined initialization method and destruction method for all the beans of the spring application?

By using “default-init-method” and “default-destroy-method” attributes of <beans> tag we specify custom initialization and destruction methods for the multiple spring beans.

e.g.: -
<beans------default-init-method=”init”
default-destroy-method=”destroy”>

Que: -How to inject a list of based collection into a spring bean?

Step 1: –The spring bean class declare “java.util.List” type variable as dependency.

e.g.: –

public class College
{
List<Student> students;
----------
----------
}

Step 2: –Make use of one of the four collection configuration elements in bean configuration file i.e. use <list> tag.

e.g.: –

<list>
<ref bean=”s1”/>
<ref bean=”s2”/>
<ref bean=”s3”/>
< /list>

list based injection

  1. student.java
  2. college.java
  3. springApplication.java
  4. beans.xml

In this example college is the dependent bean collection of student bean is the dependency.

student.java”

package com.nareshit.spring;
public class student
{
Private int rollno;
Private String name;
public Student(int rollno, String name)
{
this.rollno=rollno;
this.name=name;
}
public void displayStudentDetails()
{
System.out.println(“Rollno:”+rollno);
System.out.println(“Name:”+name);
}//business method
}

College.java”

package com.nareshit.spring;
import java.util.list;
public class College //dependent
{
private String name; //dependency 1
private List<student> students; //dependency 2
private College(String name)
{
this.name=name;
}//for constructor injection
public void setStudents (List<student> students)
{
this.students=students;
}//fpr setter injection
public List<student> getStudents()
{
return students;
}//business method
}

beans.xml”

<beans>
<bean id=”s1” class=”com.nareshit.spring.Student”/>
<constructor-arg value=”101” />
<constructor-arg value=”Rama” />
< /bean>
<bean id=”s2” class=”com/nareshit.spring.Student”/>
<constructor-arg value=”102” />
<constructor-arg value=”Rohini” />
< /bean> 
<bean id=”s3” class=”com/nareshit.spring.Student”/>
<constructor-arg value=”103” />
<constructor-arg value=”David” />
< /bean> 
<bean id=”c” class=”com/nareshit.spring.Student”/>
<constructor-arg value=”NIT” />
<property name=”students” />
<list>
<ref bean=”s1” />
<ref bean=”s2”/>
<ref bean=”s3”/>
< /list>
< /property>
< /bean> 
< /beans>

SpringApplication.java”

import com.nareshit.spring.College;
import com.nareshit.spring.Student;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.FileSystemResource;
import java.util.List;

class SpringApplication
{
public static void main(String [] args)
{
Resource resource=new FileSystemResorce (“beans.xml”);
BeanFactory =new XmlBeanFactory (resource);
Object o=factory.getBean (“c”);
College c1= (college) o;
List <student> listOfStudent=c1.getStudents (); //business method call
int count=1;
for (Student s:listOfStudents)
{
System.out.println(“Student”+count+”details…”);
s.displayStudentDetails ();
count ++;
}
} //main
} //class

previous-button-hisubmit-hi