Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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
javab_20230617
Commits
c8e66e4c
Commit
c8e66e4c
authored
Jul 16, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Waluty, depenedency injection: @Component i @Autowired
parent
ae8c0b00
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
2 deletions
+25
-2
PobieranieXML.java
.../src/main/java/com/example/demo/waluty/PobieranieXML.java
+7
-0
WalutyController.java
...c/main/java/com/example/demo/waluty/WalutyController.java
+15
-1
WalutyRest.java
...ing/src/main/java/com/example/demo/waluty/WalutyRest.java
+3
-1
No files found.
PC25-Spring/src/main/java/com/example/demo/waluty/PobieranieXML.java
View file @
c8e66e4c
...
...
@@ -14,11 +14,18 @@ import javax.xml.xpath.XPathConstants;
import
javax.xml.xpath.XPathExpressionException
;
import
javax.xml.xpath.XPathFactory
;
import
org.springframework.stereotype.Component
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
/* Adnotacje @Component, @Service, @Repository, a także @Controller i @RestController
* powodują, że:
* 1) podczas startu aplikacji Spring tworzy obiekt tej klasy (pojedynczą sztukę, czyli "singleton")
* 2) ten obiekt będzie dostępny dla innych jako "komponent" ("bean") i będzie go wstrzykiwać
*/
@Component
public
class
PobieranieXML
{
public
TabelaWalut
pobierzTabele
(
String
data
)
{
...
...
PC25-Spring/src/main/java/com/example/demo/waluty/WalutyController.java
View file @
c8e66e4c
package
com
.
example
.
demo
.
waluty
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
@Controller
public
class
WalutyController
{
private
PobieranieXML
pobieracz
=
new
PobieranieXML
();
/* @Autowired oznacza, że Spring wstawi do tej zmiennej referencję do obiektu tego typu.
* Gdyby nie potrafił tego zrobić, to aplikacja się nie uruchomi. Pole nie zostanie pozostawione z nullem.
*
* To się nazywa "wstrzykiwanie zależności" / dependency injection.
* Są 3 sposoby wstrzykiwania , o 2 pozostałych później.
* Wstrzykiwanie zal. działa tylko gdy obiekt tej klasy jest inicjalizowany przez Springa.
*
* Wstrzykiwać można obiekty, które są typu:
* - komponent (bean) istniejący w tej samej aplikacji (tak jest tutaj)
* - klasa o specjalnym znaczeniu, którą Spring "zna", np. ServletContext
*/
@Autowired
private
PobieranieXML
pobieracz
;
@GetMapping
(
"/waluty"
)
public
String
wyswietlWaluty
(
String
data
,
Model
model
)
{
...
...
PC25-Spring/src/main/java/com/example/demo/waluty/WalutyRest.java
View file @
c8e66e4c
package
com
.
example
.
demo
.
waluty
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
...
...
@@ -8,7 +9,8 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
(
"/waluty.rest"
)
public
class
WalutyRest
{
private
PobieranieXML
pobieracz
=
new
PobieranieXML
();
@Autowired
private
PobieranieXML
pobieracz
;
@GetMapping
public
TabelaWalut
wyswietlWaluty
()
{
...
...
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