KEMBAR78
Adv Java 2.5 Exercise Guide - Annotations | PDF | Constructor (Object Oriented Programming) | Programming
0% found this document useful (0 votes)
8 views22 pages

Adv Java 2.5 Exercise Guide - Annotations

This document provides a detailed exercise guide for creating a Spring Boot application using annotations, including steps for setting up a Maven project, creating classes, and configuring beans. It covers the creation of a 'Book' class with attributes and methods, as well as a 'Laptop' class, and demonstrates how to use annotations like @Component and @Bean. The guide is structured into parts, detailing the process of building a web application and managing dependencies in Eclipse IDE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

Adv Java 2.5 Exercise Guide - Annotations

This document provides a detailed exercise guide for creating a Spring Boot application using annotations, including steps for setting up a Maven project, creating classes, and configuring beans. It covers the creation of a 'Book' class with attributes and methods, as well as a 'Laptop' class, and demonstrates how to use annotations like @Component and @Bean. The guide is structured into parts, detailing the process of building a web application and managing dependencies in Eclipse IDE.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Module 2 Exercise Guide

Lab 2: Using Annotations


Advanced Java Programming

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
Introduction
This lab is designed to give you an introduction to creating a Web application and incorporating some
useful functionalities.

In this exercise, you will create a Spring Boot application without dependencies by doing the
following:
• Create a Book class under com.example package with Id, Title, and Author attributes
• Add setters, getters, and constructors
• Use @Bean and @Component annotation to create a Book bean and execute display()
method
• Add a package under src/java named org.learnquest
• Create a new class Laptop under org.learnquest with Id and Brand attributes
• Use @ComponentScan to scan to scan the package org.learnquest and create bean of
Computer and print brand of Laptop

Exercise 1:
Part 1: Create a Maven Project and Import into Eclipse
1. Open your lab.

2. On the desktop, double-click the Firefox Web Browser icon to open it.

3. When Firefox opens, type https://start.spring.io/ in the URL and hit Enter to go to the Spring
Initializr webpage.

4. Once the Spring Initializr page opens, edit the following properties:

a. For Project, select Maven. These types are used to generate the project’s package
structure and naming conventions.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
b. Under Project Metadata, leave the default of com.example for Group.

c. Change the Artifact name to Annotation. (Name will automatically default to this).

d. For Description, enter Use of Annotation.

e. For Package name, use com.example. (remove .annotation)

f. Select Jar file for Packaging.

g. Select Java version 11.

5. Click Generate at the bottom of the screen. .

6. In the pop up box, leave the default of Open with unzip (default), then click OK.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
7. At the upper right of your window, click the Downloads icon.
8. Click the Annotation.zip file folder icon at the right to open the file location.

9. A window will open displaying the zip file. Right-click in the white space in the window and select
Open Terminal Here.

10. A Terminal window will open.

11. In the Terminal, type in unzip annotation.zip and hit Enter.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
12. You will see that an Annotation folder appears in the location.

13. Close all open windows.

14. Now we need to import the project to our IDE in Eclipse as a Maven project. On the desktop,
double-click the Eclipse icon to open Eclipse IDE.

15. In the Eclipse IDE Launcher window, click Launch to select the default workspace.

16. When Eclipse opens, click File > Open Projects from File System…

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
17. In the Import Projects window, click Directory… at the upper right.

18. Double-click to open the mozilla_coder0 folder. (Note: if it isn’t listed under Recent, navigate to
Other Locations > Computer> tmp)

19. Select the annotation folder and click Open.

20. Click Finish in the Import window.

21. Once the project is created, you’ll see the Annotation project in the Project Explorer on the left
side of the window.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
Part 2: Create a Web Application using JSP
1. Expand Annotation. Expand src/main/java. Then expand the com.example package.

The AnnotationApplication.java file is your execution point, and the file that we will run several
times throughout this lab.

2. Double-click the AnnotationApplication.java file to open it.

3. Right-click on the AnnotationApplication.java file and select Run As > Java Application to run
your Spring Boot application. Eclipse will start the application and display the logs in the Run view.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
4. Resize the bottom pane (if necessary) and select the Console tab (if necessary) to see your
application.

5. Now we will make some changes and add some packages and classes. In your Project Explorer
pane, right-click the com.example package and select New > Package from the menu.

6. In the New Java Package Window, name the package com.example.model and click Finish.

7. Eclipse will create the model package within the com.example package.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
8. Right-click on your new model package and select New > Class.

9. Enter Book as the class name. Click Finish.

10. Eclipse will generate the new Java class named Book.java in the model package.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
11. The Book.java file should be open in your middle pane. (If not, double-click it in the Project
Explorer pane to open it) In the Book.java file, declare the private attributes ID, title, and author
by adding the following code beginning on line 4:

private int id;


private String title;
private String author;

12. Click the Save icon in the ribbon.

13. Hit Enter a few times after line 6 (author;) to go to line 8 (but before the closing bracket).

14. Right-click on the blank line and select Source > Generate Getters and Setters…

15. Check the Select All button to select author, id, and title, then click Generate.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
16. Now you should see all the Getters and Setters listed in your code.

17. Now we will add the Constructor. Right-click on a blank line (before the last closing bracket) and
select Source > Generate Constructor using Fields…

18. Ensure all of the fields are selected and click Generate.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
19. The Constructor has been added:

20. Let’s add a Constructor without any parameters. Right-click the blank line before the last closing
bracket and select Source > Generate Constructors from Superclass…

21. Ensure Object is selected and click Generate.

22. The constructor has been added to your code.

23. Click the Save icon in the ribbon.

24. Annotate the Book class with @Component by adding @Component before the public class (top
of the document).

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
25. Note that you get an error! Hover your mouse over @Component until a tip box appears. In the
tip box, select Import ‘Component’ (org.springframework.stereotype).

26. The code will update and add the import line automatically.

27. Click the Save icon in the ribbon.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
Part 3: Adding Objects, Beans, and Classes
1. In the Project Explorer view, right click src/main/java and select Refresh to update the structure.
Expand the two packages.

2. Go back to your AnnotationApplication.java class file. (From the Project Explorer pane or the tab
at the top.)

3. Modify the code on line 10 to add ApplicationContext before the existing SpringApplication code
so it reads as follows:

ApplicationContext context= SpringApplication.run(Annotatio…

4. An error will appear. Hover over ApplicationContext and select Import


‘ApplicationContext’ (org.springframework.context).

5. Move to the end of the line of code and hit enter to add a new line before the closing bracket.

6. Now we will create an Object of Book and use the get Bean method to retrieve it:

Book book=context.getBean(Book.class);

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
7. Hover over Book and select Import ‘Book’ (com.example.model).

8. Now let’s set the book title. Hit Enter at the end of the previous line to add a blank line. Type the
following code to set the title of the book and display it:

book.setTitle(“Tell Me Your Dreams”);


System.out.println(“The title of the book is ”+book.getTitle());

9. In the ribbon, click Save All.

10. Your full AnnotationApplication.java code should look like the following:

11. Right-click on your AnnotationApplication.java file in your Project Explorer pane and select Run
As > Java Application.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
12. Expand your console pane at the bottom of the window and view the results. You will see that the
title of the book is displayed:

Exercise 2:
Part 1: Create a Package and use AppConfig file
1. In the Project Explorer, right click your com.example package and select New > Class.

2. Name it AppConfig and click Finish. It will open in your window.

3. Go to the blank line 2 and hit Enter. Type in @Configuration, hit Enter to add a blank line, then
hover over @Configuration and select Import ‘Configuration’
(org.springframework.context.annotation).

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
4. The import code is automatically added for you on line 3.

5. On the next line, type @ComponentScan(“com.example”). Hover over ComponentScan


and select Import ‘ComponentScan’ (org.springframework.context.annotation).

6. Next, we will create a Bean for Book, however, because we already have one in our Book.java file,
we need to comment that one out first. Go to your Book.java file and comment out the
@Component line:

7. Swap back to your AppConfig.java file to add the bean there as follows (on line 9):

@Bean
public Book getBook()
{
return new Book();
}

8. Hover over @Bean and select Import ‘Bean’ (org.springframework.context.annotation), then


hover over Book on line 11 and select Import ‘Book’ (com.example.model).

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
9. Your final code for AppConfig.java should look like the following:

10. In the ribbon, click Save All.

11. Run your AnnotationApplication.java file as a Java Application. You should get the exact same
results, even though we commented out the @Component in the Book.java file. This is because we
moved the Bean to the AppConfig.java file.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
Part 2: Create a New Class and a Bean from It
1. Create a new package under java named org.learnquest by right-clicking on src/main/java > New
> Package. Name it org.learnquest and click Finish.

2. Right-click the new package, click New > Class and name it Laptop. Click Finish.

3. In the Laptop.java file, add attributes of id and brand:

private int id;


private String brand;

4. Next, we’ll add setters, getters, and constructor as needed. Go to the end of the line and hit Enter
to go to the next line. Right-click the blank line and select Source > Generate Getters and
Setters…

5. Click Select All to select your fields, then click Generate. Your Getters and Setters are
automatically added.

6. On the blank line, right-click, select Source > Generate Constructor using Fields…

7. Ensure the fields are selected and click Generate.

8. Right-click on the blank line and select Source > Generate Constructors from Superclass…

9. Ensure the Object is selected and click Generate. It is automatically added to the code.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
10. The Laptop.java code should look like the following:

11. Go to your AppConfig.java class file to create a bean for Laptop class (after the bean for Book) as
follows:

@Bean
public Laptop getComputer(){
return new Laptop();
}

12. Hover over Laptop and import it to fix the errors.

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
13. Go back to the top of the AppConfig.java file. We need to update it to scan the package
org.learnquest. Hit Enter after the first Component Scan. Type the following code on the next
line:

@ComponentScan(“org.learnquest”)

14. Your AppConfig.java file should look like the following:

15. Go to your AnnotationApplication.java file and edit the code to use the Laptop bean, and print
the laptop. At the end of the System.out… line hit Enter twice, then type the following:

Laptop comp=context.getBean(Laptop.class);
comp.setBrand(“Dell”);
System.out.println(“The brand of laptop is: ”+comp.getBrand());

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.
16. Your AnnotationApplication.java file should look like the following:

17. In the ribbon, click Save All.

18. In the Project Explorer pane, right-click AnnotationApplication.java and select Run As > Java
Application.

19. View the results in the console. You should now see that the brand of laptop is printed along with
the book title.

**End of lab

© Copyright LearnQuest 2023.


This document may not be reproduced in whole or in part without the prior written permission of LearnQuest.

You might also like