Commit 0aba61fa by Patryk Czarnik

Wstrzykiwanie na 3 sposoby

parent 5e7df664
package alx.wstrzykiwanie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FabrykaKomponentow {
{ System.out.println("FabrykaKomponentow init"); }
@Bean
public Komponent dajKomponent() {
System.out.println("Zaraz utworzę Komponent w klasie FabrykaKomponentow");
return new Komponent();
}
}
package alx.wstrzykiwanie;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Inject1 {
// 1. sposób wstrzykiwania: pole
@Autowired
private Komponent komponent;
@Autowired
private Repo repo;
{ System.out.println("Inject1 init"); } // initialization block
public Inject1() {
System.out.println("Inject1 constr, komponent = " + komponent); // null
}
@PostConstruct
public void pc() {
System.out.println("inject1 @PostConstruct, komponent = " + komponent);
}
@RequestMapping("/inject1")
@ResponseBody
public String get() {
return repo.getText() + " " + komponent.getValue();
}
}
package alx.wstrzykiwanie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Inject2 {
{ System.out.println("Inject2 init"); }
// 2. sposób wstrzykiwania: setter
private Komponent komponent;
private Repo repo;
public Komponent getKomponent() {
return komponent;
}
@Autowired
public void setKomponent(Komponent komponent) {
System.out.println("SET KOMPONENT");
this.komponent = komponent;
}
public Repo getRepo() {
return repo;
}
@Autowired
public void setRepo(Repo repo) {
this.repo = repo;
}
@RequestMapping("/inject2")
@ResponseBody
public String get() {
return repo.getText() + " " + komponent.getValue();
}
}
package alx.wstrzykiwanie;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Inject3 {
{ System.out.println("Inject3 init"); }
// 3. sposób wstrzykiwania: konstruktor
// To podejście zwykle jest podawane jako najlepsze, bo umożliwia łatwą podmianę komponentów na "mocki" podczas testów
private Komponent komponent;
private Repo repo;
// Zauważmy, że ta klasa nie jest już poprawnym "JavaBean", bo nie posiada konstruktora domyślnego.
// Adnotacja Autowired tym razem nie jest potrzebna.
public Inject3(Komponent komponent, Repo repo) {
System.out.println("KONSTRUKTOR Inject3(komponent, repo)");
this.komponent = komponent;
this.repo = repo;
}
@RequestMapping("/inject3")
@ResponseBody
public String get() {
return repo.getText() + " " + komponent.getValue();
}
}
package alx.wstrzykiwanie;
import java.util.concurrent.atomic.AtomicInteger;
public class Komponent {
{ System.out.println("Komponent init"); }
private AtomicInteger licznik = new AtomicInteger();
public int getValue() {
return licznik.incrementAndGet();
}
}
package alx.wstrzykiwanie;
import org.springframework.stereotype.Repository;
@Repository // na podobnych zasadach: @Component, @Service, @Repository - tworzony jest jeden obiekt "singleton"
public class Repo {
{ System.out.println("Repo init"); }
public String getText() {
return "Ala ma kota";
}
}
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
<li><a th:href="@{/parametry/powtorz(tekst='Ala ma kota',n=10)}">powtorz</a> n = 10</li> <li><a th:href="@{/parametry/powtorz(tekst='Ala ma kota',n=10)}">powtorz</a> n = 10</li>
</ul> </ul>
<h2>Przykłady formularzy</h2> <h2>Kalkulator</h2>
<ul> <ul>
<li><a th:href="@{/kalkulator}">Kalkulator</a> (formlularz)</li> <li><a th:href="@{/kalkulator}">Kalkulator</a> (formlularz)</li>
<li><a th:href="@{/kalkulator/historia}">Historia Kalkulatora HTML</a></li> <li><a th:href="@{/kalkulator/historia}">Historia Kalkulatora HTML</a></li>
...@@ -43,5 +43,12 @@ ...@@ -43,5 +43,12 @@
<li><a th:href="@{liczenie(liczba1=211,liczba2=303,operacja='*')}">Liczenie bezpośrednie</a> w parametrami URL</li> <li><a th:href="@{liczenie(liczba1=211,liczba2=303,operacja='*')}">Liczenie bezpośrednie</a> w parametrami URL</li>
</ul> </ul>
<h2>Wstrzykiwanie na 3 sposoby</h2>
<ul>
<li><a href="/inject1">Wstrzykiwanie 1</a> - pole</li>
<li><a href="/inject2">Wstrzykiwanie 2</a> - setter</li>
<li><a href="/inject3">Wstrzykiwanie 3</a> - konstruktor</li>
</ul>
</body> </body>
</html> </html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment