-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReferenceMaterial.txt
More file actions
3087 lines (2290 loc) · 80.2 KB
/
Copy pathReferenceMaterial.txt
File metadata and controls
3087 lines (2290 loc) · 80.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Spring | Spring Framework:
..........................
Objective:
->Spring makes it easy to create Java enterprise applications
Early 2000, java started becoming more popular in distributed application developement.
J2EE /JEE Spec:
JEE Platform spec : containers /app containers/tools
WebTechnology spec
Java servlet
JSP
JSF
Websocket
JSTL
EL
Enterprise Application spec
EJB
JPA -ORM
JMS
JTA
DI
Context and DI
Concurrency Util
Web Services
SOAP based -JAX-WS
REST Based -JAX-RS
Management and Security technologies
Java EE Security spec
Java Auth
Java authorzation
JAVA EE additional spec
JMX -Jmeter,Spring Actuator
////////////////////////////////////////////////////////////////////////////
JEE enterprise application:
............................
Client Layer Web Layer Biz layer Data Layer/Persistency
Browser Servlet/JSP EJB Entity Beans
EJB :Enterprise Java Bean
Java Object can support distributed biz operations
eg: incase of shopping cart
I have a bean(java object) which has three apis addToCart,Checkout,Payment)
Api classifications:
1.Session Bean - to represent biz logic---jdbc--dml
2.Enitity bean -ORM - Object to Table mapping-First orm solution---Hibernate
3.Message Driven Bean - Message driven systems-RabbitMQ,Kaffka,JMS
Rod was working in enterprise banking application with jee, he faced lot of complexity in designing ejb driven systems, so he decided to move ejb from JEE application, he wrote book called expert J2EE ONE ON ONE design and development.
He proposed a model that model later on came as The project in "Spring Season" - Spring Framework.
Client Layer Web Layer Biz layer Data Layer/Persistency
Browser Spring Spring Spring-orm
Spring development objective is "POJO".
Plain Old Java Object
When you build enterprise java apps, no plumbing(bolier plate code),Spring framework takes care that plumbing.
Enterprise application challanges:
1.Object Management
IOC : Inversion of Control
Inversion -Do opposit
of Control -Management
In enterprise application object creation and linking objects are so complex that should be removed from the developer.
Someone should take care object creation and linking=
=>This process is called as IOC.
The IOC suggest the automation program,that program called as IOC Container.
The first IOC Container
1.PICO Container
2.Spring container : It was born as enterprise container.
Spring container takes care of object creations and object linking .
Which is other wise called as "Dependency Injection" .
Dependency - HAS-A
injection - linking objects
Spring Application:
The application is written in java technology, executed on Spring Container which is runtime for spring applications.spring container runs on jvm
Spring Application
|
Spring Container
|
JVM
what is framework?
One Stop Solution
Collection of tools and technologies provided in one place to build end to end applications mostly.
What spring framework offers?
Spring offers collection of many projects
1.Core:
IoC container,
Events,
Resources,
i18n,
Validation,
Data Binding,
Type Conversion,
SpEL,
AOP.
Core Project
1.XML driven
2.Java Config Driven
3.Boot Driven
2.Web
Web Servlet
Spring MVC, WebSocket, SockJS, STOMP messaging.
Web Reactive
Spring WebFlux, WebClient, WebSocket.
3.Data Access
SQL, NOSQL
SQL:
Transactions, DAO support, JDBC, ORM, Marshalling XML.
NOSQL
Mongodb....
Integration
Remoting, JMS, JCA, JMX, Email, Tasks, Scheduling, Cache.
Languages
Kotlin, Groovy, Dynamic languages.
//////////////////////////////////////////////////////////////////////////////
How to start Spring Project?
1.Plain java project
Core
2.Web Project
MVC
Create Project:
1.maven based
2.gradle based
..............................................................................
Software Management tools:
1.ANT,MAVEN,Gradle
Maven:
What is maven?
->Apache Maven is a software project management and comprehension tool.
- Based on the concept of a project object model (POM).
Software Management Task:
1.Development
->Project layouts
->Project dependencies-libs(jars)
-one.jar1.0
---->thirdparty.jar1.1
->compile,jars/wars--->deploy on to the runtime
.....
2.Testing
-If you want to run automated testing
3.Build/Production
-if you want to prepare final deployable applications
-app.jar , app.war, app.ear
fat jar
//////////////////////////////////////////////////////////////////////////
How to start with maven?
->COMMAND Line -install maven
->Eclipse Maven Integration
///////////////////////////////////////////////////////////////////////////
How maven has been implemented?
Maven is java Program works with configuration file written in xml.
MAVEN-------XMLfile
XML file contains information about automation task.
xml file named pom.xml -Project Object Model
pom.xml----is fed into java Program(maven)---extract xml elements---runs automation.
///////////////////////////////////////////////////////////////////////////
pom.xml basic structure:
.........................
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hellomaven</groupId>
<artifactId>hellomaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hellomaven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
.............................................................................
What is dependencies?
Project jar dependency
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
Maven will download all project libs(jars) from a site called "https://mvnrepository.com/"
////////////////////////////////////////////////////////////////////////////
Spring Project Dependency:
.........................
1.junit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
2.log4j - Required
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
3.JDBC -MYSQL CONNECTOR
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
4.COMMONS DBCP :Connection Pools implementation lib
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
5.Spring Framework :Spring core lib
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
Spring Versions and features:
2004 - Spring Framework 1.0 released
– Champions dependency injection
– Encourages POJOs
– Uses XML files to describe application
configuration
– Becomes popular quickly as an EJB
alternative
• Spring 2.0 (2006):
– XML simplification, async JMS, JPA, AspectJ support
• Spring 2.5 (2007, last release 2.5.6)
– Requires Java 1.4+ and supports JUnit 4
– Annotation DI, @MVC controllers, XML namespaces
• Spring 3.x (3.2.17 released July 2013)
– Environment & Profiles, @Cacheable, @EnableXXX …
– Requires Java 1.5+ and JUnit 4.7+
– REST support, JavaConfig, SpEL, more annotations
• Spring 4.x (released Dec 2016)
– Support for Java 8, @Conditional, Web-sockets
• Spring 5.x (2017)
– Reactive programming focus
//////////////////////////////////////////////////////////////////////////////
Spring core concept:
1.Spring Beans:
It is java class, based Java Bean standards
-class should be public
-properties should be private
-set and get methods
-constructors
How to create simple Helloworld bean?
Architecture:
POJO Classes(Bean)
|
Spring-Application <====>SpringContainer
config file |
Fullyconfigured System
Spring-Application configuration Management:
...........................................
Spring application can be configured in many ways
1.XML files - old and legacy.
2.Java Config - latest via Annotations
2.1.AutoConfig
->Spring boot
3.Mix and Match is good idea.(XML + java config)
Note : Config-file name can be anything. beans.xml
basic beans.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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.xsd">
<!-- more bean definitions go here -->
</beans>
////////////////////////////////////////////////////////////////////////////
<bean id="hello" class="com.ae.spring.beans.HelloWorld"></bean>
com.ae.spring.beans.HelloWorld(Bean)
|
beans.xml <====>SpringContainer
|
Fullyconfigured System
SpringContainer:
Spring Container is Java Program.
Spring Container is written based on Design pattern called Factory Design Pattern
Factory------XML
|
new HelloWorld();
|
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml")
HelloWorld.java
package com.ae.spring.beans;
public class HelloWorld {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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.xsd">
<!-- more bean definitions go here -->
<bean id="hello" class="com.ae.spring.beans.HelloWorld"></bean>
</beans>
App.java
package com.ae.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ae.spring.beans.HelloWorld;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
// without Spring
// HelloWorld hello = new HelloWorld();
// hello.setMessage("Hello Spring World");
// System.out.println(hello.getMessage());
// With Spring
System.out.println("Spring is Ready Use");
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
// Get The Bean from Factory
HelloWorld hello = factory.getBean("hello", HelloWorld.class);
hello.setMessage("Hello Spring Bean");
System.out.println(hello.getMessage());
}
}
////////////////////////////////////////////////////////////////////////////
org.springframework.beans.factory.BeanFactory:
which is super interface in spring framework
org.springframework.context.ApplicationContext
ApplicationContext factory =new ClassPathXmlApplicationContext("beans.xml");
// Get The Bean from Factory
HelloWorld hello = factory.getBean("hello", HelloWorld.class);
hello.setMessage("Hello Spring Bean");
System.out.println(hello.getMessage());
///////////////////////////////////////////////////////////////////////////
Dependency Injection:
....................
Dependency injection means setting class properties during runtime.
eg: customer has a address
class Address {
}
class Customer {
private Address addr;
set
get
Customer(){}
Customer(Address addr){
}
}
Address addr=new Address();
Customer customer =new Customer(addr)
or
customer.setAddr(addr)
/////////////////////////////////////////////////////////////////////////////
Spring bean definitions
<property name="message" value="Spring dependency Injection"/> -Setter Injection
<constructor-arg name="message" value="Hello Constructors"/> -Constructor
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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.xsd">
<!-- more bean definitions go here -->
<bean id="hello" class="com.ae.spring.beans.HelloWorld">
<property name="message" value="Hello Spring Dependency"/>
</bean>
<bean id="helloNew" class="com.ae.spring.beans.HelloWorld">
<constructor-arg name="message" value="Hello Constructors"/>
</bean>
</beans>
HelloWorld.java
package com.ae.spring.beans;
public class HelloWorld {
private String message;
public HelloWorld() {
// TODO Auto-generated constructor stub
}
public HelloWorld(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
App.java
package com.ae.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ae.spring.beans.HelloWorld;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
// without Spring
// HelloWorld hello = new HelloWorld();
// hello.setMessage("Hello Spring World");
// System.out.println(hello.getMessage());
// With Spring
System.out.println("Spring is Ready Use");
//BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
ApplicationContext factory =new ClassPathXmlApplicationContext("beans.xml");
// Get The Bean from Factory :via setters
//HelloWorld hello = factory.getBean("hello", HelloWorld.class);
//Get the Bean from Factory : via constructors
HelloWorld hello = factory.getBean("helloNew", HelloWorld.class);
//hello.setMessage("Hello Spring Bean");
System.out.println(hello.getMessage());
}
}
//////////////////////////////////////////////////////////////////////////////
Lab:Create User Bean class, properties are userid,userName,password,email,mobileno,location.
Create Bean and set User bean properties via setter and constructors.
////////////////////////////////////////////////////////////////////////////
When to use Setter vs Constructor?
Note:
Constructor properties are initlized during object creations
Settter properties are initlized after object creations
Spring supports both.You can mix and match.
Constructors:
->Enforce mandatory dependencies
Setters:
1.Allow optional dependenices
//////////////////////////////////////////////////////////////////////////////Bean dependencies:
.................
If you set Object references
<property name="address" ref="address" />
<constructor-arg ref="address"/>
entities:
com.ae.spring.entity
package com.ae.spring.entites;
public class Customer {
private int customerId;
private String name;
//dependancy
private Address address;
public Customer() {
}
public Customer(int customerId, String name, Address address) {
super();
this.customerId = customerId;
this.name = name;
this.address = address;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
package com.ae.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ae.spring.entites.Customer;
public class App {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("beans.xml");
Customer customer = factory.getBean("customer", Customer.class);
System.out.println("Customer Id :" + customer.getCustomerId());
System.out.println("Customer Name :" + customer.getName());
System.out.println("Customer City :" + customer.getAddress().getCity());
}
}
Create Order Bean: orderid
Customer has a an order...
//////////////////////////////////////////////////////////////////////////
Repository:
..........
com.ae.spring.repo
com.ae.spring.repo.impl
CustomerServiceImpl
----CustomerInMemoryRepositoryImpl
|
CustomerRepository-------> |---CustomerJdbcRepositoryImpl
|
----CustomerJPARepositoryImpl
package com.ae.spring.repo;
import java.util.List;
import com.ae.spring.entites.Customer;
public interface CustomerRepository {
List<Customer> findAll();
}
package com.ae.spring.repo.impl;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ae.spring.entites.Customer;
import com.ae.spring.repo.CustomerRepository;
public class CustomerInMemoryRepositoryImpl implements CustomerRepository {
final Logger logger = LoggerFactory.getLogger(CustomerInMemoryRepositoryImpl.class);
public List<Customer> findAll() {
logger.info("CustomerInMemory-findAll");
return null;
}
}
package com.ae.spring.repo.impl;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ae.spring.entites.Customer;
import com.ae.spring.repo.CustomerRepository;
public class CustomerJPARepositoryImpl implements CustomerRepository {
final Logger logger = LoggerFactory.getLogger(CustomerJdbcRepositoryImpl.class);
public List<Customer> findAll() {
logger.info("CustomerJPARepositoryImpl-findAll");
return null;
}
}
package com.ae.spring.repo.impl;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ae.spring.entites.Customer;
import com.ae.spring.repo.CustomerRepository;
public class CustomerJPARepositoryImpl implements CustomerRepository {
final Logger logger = LoggerFactory.getLogger(CustomerJdbcRepositoryImpl.class);
public List<Customer> findAll() {
logger.info("CustomerJPARepositoryImpl-findAll");
return null;
}
}
package com.ae.spring.service;
import java.util.List;
import com.ae.spring.entites.Customer;
public interface CustomerService {
List<Customer> findAll();
}
package com.ae.spring.service.impl;
import java.util.List;
import com.ae.spring.entites.Customer;
import com.ae.spring.repo.CustomerRepository;
import com.ae.spring.service.CustomerService;
public class CustomerServiceImpl implements CustomerService {
private CustomerRepository repository;
public CustomerServiceImpl() {
}
public CustomerRepository getRepository() {
return repository;
}
public void setRepository(CustomerRepository repository) {
this.repository = repository;
}
public CustomerServiceImpl(CustomerRepository repository) {
super();
this.repository = repository;
}
public List<Customer> findAll() {
return repository.findAll();
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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.xsd">
<bean id="address" class="com.ae.spring.entites.Address">
<property name="doorNo" value="103" />
<property name="area" value="WhiteField" />
<property name="city" value="Banaglore" />
</bean>
<bean id="customer" class="com.ae.spring.entites.Customer">
<constructor-arg name="customerId" value="111" />
<constructor-arg name="name" value="Subramanian" />
<constructor-arg ref="address" />
</bean>
<bean id="inMemoryimpl"
class="com.ae.spring.repo.impl.CustomerInMemoryRepositoryImpl">
</bean>
<bean id="jdbcimpl"
class="com.ae.spring.repo.impl.CustomerJdbcRepositoryImpl">
</bean>
<bean id="jpaimpl"
class="com.ae.spring.repo.impl.CustomerJPARepositoryImpl">
</bean>
<bean id="customerService" class="com.ae.spring.service.impl.CustomerServiceImpl">
<constructor-arg ref="jpaimpl"/>
</bean>
</beans>
App.java
package com.ae.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ae.spring.entites.Customer;
import com.ae.spring.service.CustomerService;
public class App {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("beans.xml");
CustomerService service = factory.getBean("customerService", CustomerService.class);
service.findAll();
}
}
////////////////////////////////////////////////////////////////////////////
DI Values:
You can inject primtives / scaller such as int,sting,char,boolean
You can inject Objects(reference types) - Customer,Order,Address,Repository
You can inject Collection types - such as List,Map,Set...
Proerty can have child elements:List
<property name="shippingAddress">
<list>
<ref bean="address" />
<ref bean="address" />
<ref bean="address" />
<ref bean="address" />
<ref bean="address" />
<ref bean="address" />
</list>
</property>
or via constructor
<constructor-arg name="orders">
<list>
<ref bean="orderA" />
<ref bean="orderB" />
<ref bean="orderC" />
</list>
</constructor-arg>
<list> -ArrayList
List<Address> shippingAddress=new ArrayList<Address>
shippingAddress.add(address);
/////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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.xsd">
<bean id="address" class="com.ae.spring.entites.Address">
<property name="doorNo" value="103" />
<property name="area" value="WhiteField" />
<property name="city" value="Banaglore" />
</bean>
<bean id="orderA" class="com.ae.spring.entites.Order">
<property name="orderId" value="1"></property>
<property name="orderValue" value="1000"></property>
</bean>
<bean id="orderB" class="com.ae.spring.entites.Order">
<property name="orderId" value="2"></property>
<property name="orderValue" value="6000"></property>
</bean>
<bean id="orderC" class="com.ae.spring.entites.Order">
<property name="orderId" value="3"></property>
<property name="orderValue" value="10000"></property>
</bean>
<bean id="customer" class="com.ae.spring.entites.Customer">
<constructor-arg name="customerId" value="111" />
<constructor-arg name="name" value="Subramanian" />
<constructor-arg ref="address" />
<constructor-arg name="orders">
<list>
<ref bean="orderA" />
<ref bean="orderB" />
<ref bean="orderC" />
</list>
</constructor-arg>
</bean>
<bean id="inMemoryimpl"
class="com.ae.spring.repo.impl.CustomerInMemoryRepositoryImpl">
</bean>
<bean id="jdbcimpl"
class="com.ae.spring.repo.impl.CustomerJdbcRepositoryImpl">
</bean>
<bean id="jpaimpl"
class="com.ae.spring.repo.impl.CustomerJPARepositoryImpl">
</bean>
<bean id="customerService"
class="com.ae.spring.service.impl.CustomerServiceImpl">
<constructor-arg ref="jpaimpl" />
</bean>
</beans>
Order.java
package com.ae.spring.entites;
public class Order {
private int orderId;
private float orderValue;
public Order() {
}
public Order(int orderId, float orderValue) {
super();
this.orderId = orderId;
this.orderValue = orderValue;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public float getOrderValue() {
return orderValue;
}
public void setOrderValue(float orderValue) {
this.orderValue = orderValue;
}
}
Customer.java
package com.ae.spring.entites;
import java.util.List;
public class Customer {
private int customerId;
private String name;
//dependancy
private Address address;
//one to many : one customer can have many orders
private List<Order> orders;
public Customer() {
}
public Customer(int customerId, String name, Address address, List<Order> orders) {
super();
this.customerId = customerId;
this.name = name;
this.address = address;
this.orders = orders;
}
public List<Order> getOrders() {