Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
java_weekendowa_20221008
Commits
50f96897
Commit
50f96897
authored
Nov 13, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uzupełnienia: wstrzykiwanie przez konstruktor
parent
c8effb2d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
0 deletions
+83
-0
ParametryController.java
...g/src/main/java/com/example/demo/ParametryController.java
+47
-0
WstrzykiwaniePrzezKonstruktor.java
.../java/com/example/demo/WstrzykiwaniePrzezKonstruktor.java
+36
-0
No files found.
PC26-Spring/src/main/java/com/example/demo/ParametryController.java
0 → 100644
View file @
50f96897
package
com
.
example
.
demo
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
/* Gdy adres (w adnotacji RequestMapping albo GetMapping)
* jest podany na poziomie klasy, a potem na poziomie metody,
* to prawdziwy adres, pod którym działa metoda, jest połączeniem tych dwóch fragmentów.
*
* http://localhost:8080/parametry/witaj?imie=Ala
*/
@Controller
@RequestMapping
(
"/parametry"
)
public
class
ParametryController
{
@RequestMapping
(
"/witaj"
)
@ResponseBody
// Najprostsze podejście, aby odczytać wartość parametru zapytania:
// zadeklarować parametr o takiej samej nazwie w metodzie kontrolera.
// "Spring nam to przekaże".
// Podejście polegające na tym, że framework wywołuje "naszą metodę",
// a gdy my czegoś potrzebujemy, to dodajemy odp. parametr do tej metody,
// nazywa się "inversion of control (IoC)".
public
String
witaj
(
String
imie
)
{
return
"Witaj "
+
imie
;
}
// Dzięki adnotacji @RequestParam mamy większą kontrolę nad szczegółami parametru.
// Można m.in. podać inną nazwę niż nazw zmiennej w Javie, może podać wartość domyślną.
// http://localhost:8080/parametry/powtorz?tekst=Ala%20ma%20kota&n=10
@RequestMapping
(
path
=
"/powtorz"
,
produces
=
"text/plain;charset=utf-8"
)
@ResponseBody
public
String
powtorz
(
@RequestParam
(
defaultValue
=
""
)
String
tekst
,
@RequestParam
(
name
=
"n"
,
defaultValue
=
"1"
)
int
ileRazy
)
{
return
(
tekst
+
"\n"
).
repeat
(
ileRazy
);
}
// Parametry bez adnotacji są opcjonalne - w razie braku Spring wywoła metodę i wstawi nulla.
// Parametry z adnotacją @RequestParam są domyślnie obowiązkowe. Aby był opcjonalny,
// trzeba wpisać required=false (wtedy brak parametru = null)
// lub podać defaultValue.
}
PC26-Spring/src/main/java/com/example/demo/WstrzykiwaniePrzezKonstruktor.java
0 → 100644
View file @
50f96897
package
com
.
example
.
demo
;
import
java.util.stream.Collectors
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@Controller
public
class
WstrzykiwaniePrzezKonstruktor
{
private
LogikaKalkulatora
logikaKalkulatora
;
// Jeśli jedynym dostępnym konstruktorem jest taki, który wymaga parametrów,
// Spring wywoła własnie ten konstruktor i przekaże wymagane parametry, o ile uda mu się takie obiekty znaleźć.
// To też jest forma wstrzykiwania zależności.
// Zauważmy, że działa bez żadnej adnotacji przed tym konstruktorem.
public
WstrzykiwaniePrzezKonstruktor
(
LogikaKalkulatora
logikaKalkulatora
)
{
this
.
logikaKalkulatora
=
logikaKalkulatora
;
}
@GetMapping
(
path
=
"/liczenie"
,
produces
=
"text/plain"
)
@ResponseBody
public
String
liczenie
(
Long
liczba1
,
Long
liczba2
,
String
operacja
)
{
return
String
.
valueOf
(
logikaKalkulatora
.
oblicz
(
liczba1
,
liczba2
,
operacja
));
}
@GetMapping
(
path
=
"/historia.txt"
,
produces
=
"text/plain"
)
@ResponseBody
public
String
dajHistorie
()
{
return
logikaKalkulatora
.
getHistoriaDzialan
()
.
stream
()
.
map
(
s
->
" → "
+
s
)
.
collect
(
Collectors
.
joining
(
"\n"
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment