Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mamady310 authored Nov 20, 2023
0 parents commit 5e1cf23
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPR-CL-BEANSCOPES

This coding lab covers unit "Spring Core", module "Inversion of Control".

## Instructions
- This project contains the definitions for some beans
- We can use annotations to define the scope of those beans
- Look out for @TODO statements to identify where annotations need to be added/modified

## Shouldn't Modify (But Look at for Context)
- Lab.Beans.ScopeBean.java
- Test files

## Should Modify
- Lab.Application.java
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"unit-mapping" : ["Spring Core", "Spring Boot Basics"],
"required-extensions" : []
}
70 changes: 70 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.revature</groupId>
<artifactId>springlabs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springlabs</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
77 changes: 77 additions & 0 deletions src/main/java/Lab/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package Lab;

import Lab.Beans.ScopedBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
* The @Configuration class is where the beans of your Spring project will be defined.
*/
@Configuration
public class Application {
/**
* A bean's scope determines how the bean behaves when that bean is requested from the ApplicationContext - Spring
* could be instructed to either provide a newly-instantiated object or reuse an existing object. Below are the
* Spring bean scopes:
*
* Singleton - the default scope, any time a specific bean is requested, the existing object is reused
* Prototype - any time a bean is requested a new object is instantiated
* Request - only used in web applications - a new object is instantiated once per web request
* Session - only used in web applications - a new object is instantiated once per web session
* Application - only used in web applications - a new object is instantiated once per web servlet
* Websocket - only used in web applications - a new object is instantiated once per websocket
*/

/**
* The @Scope() annotation here is unnecessary as "singleton" is the default, but this example shows how the
* scope can be set.
*/
@Bean
@Scope("singleton")
public ScopedBean sampleBean(){
return new ScopedBean();
}

/**
* TODO: correct the following code so that a new ScopedBean is instantiated every time the labBean is requested.
*/
@Bean
public ScopedBean labBean(){
return new ScopedBean();
}

/**
* Main method for manual testing of your code
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Application.class);
ScopedBean b1 = (ScopedBean) applicationContext.getBean("sampleBean");
ScopedBean b2 = (ScopedBean) applicationContext.getBean("sampleBean");

System.out.println("sampleBean is singleton-scoped, so any sampleBean retrieved from the app context ");
System.out.println("should be the same object. Let's test it with ==, are b1 and b2 the same object?");
/*
remember that == performs a shallow comparison, and can only check if two objects are literally the same
object in memory. This came in useful here!
*/
if(b1 == b2){
System.out.println("b1 and b2 are the same object.");
}else{
System.out.println("b1 and b2 are not the same object.");
}
ScopedBean b3 = (ScopedBean) applicationContext.getBean("labBean");
ScopedBean b4 = (ScopedBean) applicationContext.getBean("labBean");

System.out.println("You'll need to scope labBean such that b3 and b4 are different objects.");
System.out.println("Let's test it with ==, are b1 and b2 the same object?");
if(b3 == b4){
System.out.println("failure: b3 and b4 are the same object.");
}else{
System.out.println("success: b3 and b4 are not the same object.");
}
}

}
9 changes: 9 additions & 0 deletions src/main/java/Lab/Beans/ScopedBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Lab.Beans;

/**
* This class will be used in two different Beans, defined in the class marked @Configuration (Application.class).
* No need to modify anything in this class.
*/
public class ScopedBean {

}
20 changes: 20 additions & 0 deletions src/test/java/ScopedBeanTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Lab.Application;
import Lab.Beans.ScopedBean;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ScopedBeanTest {
/**
* Requesting the "labBean" multiple times should result in multiple instances. This is the expected behavior
* of one of the bean scopes. This test will pass when the bean is set to the proper scope.
*/
@Test
public void scopedBeanTest(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Application.class);
ScopedBean b1 = (ScopedBean) applicationContext.getBean("labBean");
ScopedBean b2 = (ScopedBean) applicationContext.getBean("labBean");
Assertions.assertTrue(b1!=b2);
}
}

0 comments on commit 5e1cf23

Please sign in to comment.