Skip to content

Commit 5d335c7

Browse files
committed
Add Lesson 11 code and HW solution
1 parent a662e4c commit 5d335c7

File tree

22 files changed

+693
-0
lines changed

22 files changed

+693
-0
lines changed

Lesson11.HWSolution/.gitignore

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Example user template template
3+
### Example user template
4+
5+
# IntelliJ project files
6+
.idea
7+
*.iml
8+
out
9+
gen### macOS template
10+
# General
11+
.DS_Store
12+
.AppleDouble
13+
.LSOverride
14+
15+
# Icon must end with two \r
16+
Icon
17+
18+
# Thumbnails
19+
._*
20+
21+
# Files that might appear in the root of a volume
22+
.DocumentRevisions-V100
23+
.fseventsd
24+
.Spotlight-V100
25+
.TemporaryItems
26+
.Trashes
27+
.VolumeIcon.icns
28+
.com.apple.timemachine.donotpresent
29+
30+
# Directories potentially created on remote AFP share
31+
.AppleDB
32+
.AppleDesktop
33+
Network Trash Folder
34+
Temporary Items
35+
.apdisk
36+
### Linux template
37+
*~
38+
39+
# temporary files which can be created if a process still has a handle open of a deleted file
40+
.fuse_hidden*
41+
42+
# KDE directory preferences
43+
.directory
44+
45+
# Linux trash folder which might appear on any partition or disk
46+
.Trash-*
47+
48+
# .nfs files are created when an open file is removed but is still being accessed
49+
.nfs*
50+
### Java template
51+
# Compiled class file
52+
*.class
53+
54+
# Log file
55+
*.log
56+
57+
# BlueJ files
58+
*.ctxt
59+
60+
# Mobile Tools for Java (J2ME)
61+
.mtj.tmp/
62+
63+
# Package Files #
64+
*.jar
65+
*.war
66+
*.nar
67+
*.ear
68+
*.zip
69+
*.tar.gz
70+
*.rar
71+
72+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
73+
hs_err_pid*
74+
### Windows template
75+
# Windows thumbnail cache files
76+
Thumbs.db
77+
ehthumbs.db
78+
ehthumbs_vista.db
79+
80+
# Dump file
81+
*.stackdump
82+
83+
# Folder config file
84+
[Dd]esktop.ini
85+
86+
# Recycle Bin used on file shares
87+
$RECYCLE.BIN/
88+
89+
# Windows Installer files
90+
*.cab
91+
*.msi
92+
*.msix
93+
*.msm
94+
*.msp
95+
96+
# Windows shortcuts
97+
*.lnk
98+
### Maven template
99+
target/
100+
pom.xml.tag
101+
pom.xml.releaseBackup
102+
pom.xml.versionsBackup
103+
pom.xml.next
104+
release.properties
105+
dependency-reduced-pom.xml
106+
buildNumber.properties
107+
.mvn/timing.properties
108+
109+
# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
110+
!/.mvn/wrapper/maven-wrapper.jar
111+

Lesson11.HWSolution/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.alishev.springcourse</groupId>
8+
<artifactId>HWSolution</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>war</packaging>
11+
12+
<name>spring-ioc-app1 Maven Webapp</name>
13+
<!-- FIXME change it to the project's website -->
14+
<url>http://www.example.com</url>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>1.7</maven.compiler.source>
19+
<maven.compiler.target>1.7</maven.compiler.target>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.11</version>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-core</artifactId>
33+
<version>5.1.4.RELEASE</version>
34+
</dependency>
35+
36+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
37+
<dependency>
38+
<groupId>org.springframework</groupId>
39+
<artifactId>spring-beans</artifactId>
40+
<version>5.1.4.RELEASE</version>
41+
</dependency>
42+
43+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
44+
<dependency>
45+
<groupId>org.springframework</groupId>
46+
<artifactId>spring-context</artifactId>
47+
<version>5.1.4.RELEASE</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<finalName>spring-ioc-app1</finalName>
53+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
54+
<plugins>
55+
<plugin>
56+
<artifactId>maven-clean-plugin</artifactId>
57+
<version>3.1.0</version>
58+
</plugin>
59+
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
60+
<plugin>
61+
<artifactId>maven-resources-plugin</artifactId>
62+
<version>3.0.2</version>
63+
</plugin>
64+
<plugin>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<version>3.8.0</version>
67+
</plugin>
68+
<plugin>
69+
<artifactId>maven-surefire-plugin</artifactId>
70+
<version>2.22.1</version>
71+
</plugin>
72+
<plugin>
73+
<artifactId>maven-war-plugin</artifactId>
74+
<version>3.2.2</version>
75+
</plugin>
76+
<plugin>
77+
<artifactId>maven-install-plugin</artifactId>
78+
<version>2.5.2</version>
79+
</plugin>
80+
<plugin>
81+
<artifactId>maven-deploy-plugin</artifactId>
82+
<version>2.8.2</version>
83+
</plugin>
84+
</plugins>
85+
</pluginManagement>
86+
</build>
87+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.alishev.springcourse;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* @author Neil Alishev
10+
*/
11+
@Component
12+
public class ClassicalMusic implements Music {
13+
private List<String> songs = new ArrayList<>();
14+
15+
// Блок инициализации объекта (англ. Instance initialization block)
16+
// Выполняется каждый раз, когда создается объект класса
17+
{
18+
songs.add("Hungarian Rhapsody");
19+
songs.add("Symphony no. 5 in C Minor, op. 67");
20+
songs.add("Night on Bald Mountain");
21+
}
22+
23+
@Override
24+
public List<String> getSongs() {
25+
return songs;
26+
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.alishev.springcourse;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author Neil Alishev
7+
*/
8+
public interface Music {
9+
// Интерфейс тоже поменяли
10+
List<String> getSongs();
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ru.alishev.springcourse;
2+
3+
/**
4+
* @author Neil Alishev
5+
*/
6+
public enum MusicGenre {
7+
CLASSICAL, ROCK
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.alishev.springcourse;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import java.util.Random;
7+
8+
/**
9+
* @author Neil Alishev
10+
*/
11+
@Component
12+
public class MusicPlayer {
13+
private ClassicalMusic classicalMusic;
14+
private RockMusic rockMusic;
15+
16+
@Autowired
17+
public MusicPlayer(ClassicalMusic classicalMusic, RockMusic rockMusic) {
18+
this.classicalMusic = classicalMusic;
19+
this.rockMusic = rockMusic;
20+
}
21+
22+
public void playMusic(MusicGenre genre) {
23+
Random random = new Random();
24+
25+
// случайное целое число между 0 и 2
26+
int randomNumber = random.nextInt(3);
27+
28+
if (genre == MusicGenre.CLASSICAL) {
29+
// случайная классическая песня
30+
System.out.println(classicalMusic.getSongs().get(randomNumber));
31+
} else {
32+
// случайная рок песня
33+
System.out.println(rockMusic.getSongs().get(randomNumber));
34+
}
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.alishev.springcourse;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* @author Neil Alishev
10+
*/
11+
@Component
12+
public class RockMusic implements Music {
13+
private List<String> songs = new ArrayList<>();
14+
15+
// Блок инициализации объекта (англ. Instance initialization block)
16+
// Выполняется каждый раз, когда создается объект класса
17+
{
18+
songs.add("Wind cries Mary");
19+
songs.add("Paint it black");
20+
songs.add("Can't seem to make you mine");
21+
}
22+
23+
@Override
24+
public List<String> getSongs() {
25+
return songs;
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ru.alishev.springcourse;
2+
3+
import org.springframework.context.support.ClassPathXmlApplicationContext;
4+
5+
/**
6+
* @author Neil Alishev
7+
*/
8+
public class TestSpring {
9+
public static void main(String[] args) {
10+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
11+
"applicationContext.xml"
12+
);
13+
14+
MusicPlayer musicPlayer = context.getBean("musicPlayer", MusicPlayer.class);
15+
16+
musicPlayer.playMusic(MusicGenre.CLASSICAL);
17+
musicPlayer.playMusic(MusicGenre.ROCK);
18+
19+
context.close();
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/context
8+
http://www.springframework.org/schema/context/spring-context.xsd">
9+
10+
<context:component-scan base-package="ru.alishev.springcourse" />
11+
12+
</beans>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE web-app PUBLIC
2+
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3+
"http://java.sun.com/dtd/web-app_2_3.dtd" >
4+
5+
<web-app>
6+
<display-name>Archetype Created Web Application</display-name>
7+
</web-app>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h2>Hello World!</h2>
4+
</body>
5+
</html>

0 commit comments

Comments
 (0)