Spring má dva hlavní způsoby konfigurace (XML a Javu), ale tři hlavní přístupy ke konfiguraci a každý z těchto přístupů má několik názvů.
- Konfigurace využívající XML (XML based configuration)
- XML konfigurační soubor a XML konfigurace (XML config and XML-driven/based configuration)
- XML konfigurační soubor a konfigurace anotacemi (XML config and annotation-driven/based configuration)
- Konfigurace využívající Javu
- Java konfigurační soubor a konfigurace anotacemi (Java config and annotation-driven/based configuration | Java based configuration | JavaConfig)
Java konfigurační soubor a konfigurace anotacemi
V tomto příspěvku se podíváme na Java konfigurační soubor a konfigurace anotacemi. V tomto případě se používá pro konfiguraci Java třída a anotace (@Autowired, @Service, …).
Naše jednoduchá aplikace bude mít dvě rozhraní MessageService a SecurityService. Rozhraní MessageService bude mít metodu String getMessage() a SecurityService String encode(String text). Funkčnost aplikace je tato: nejdříve získá řetězec (metoda getMessage()) a ten potom zašifruje (metoda encode()). Vlastní způsob šifrování, implementovaný v SecurityServiceImpl, bude triviální. Všechny písmena e v textu změníme za € a všechna písmena s za §.
Struktura projektu
project ├───src │ ├───main │ │ ├───java │ │ │ └───cz │ │ │ └───vitfo │ │ │ │ Config.java │ │ │ │ Main.java │ │ │ │ MyApplication.java │ │ │ │ │ │ │ └───service │ │ │ │ MessageService.java │ │ │ │ SecurityService.java │ │ │ │ │ │ │ └───impl │ │ │ MessageServiceImpl.java │ │ │ SecurityServiceImpl.java │ │ │ │ │ └───resources
Vytvoříme si třídu Main.java, ve které vytvoříme kontext Springu. Ve třídě Main.java budeme mít logiku naší aplikace. Nepoužijeme konfigurační soubor beans.xml, ale místo něj budeme mít třídu Config.java.
Config.java
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import cz.vitfo.service.MessageService; import cz.vitfo.service.SecurityService; import cz.vitfo.service.impl.MessageServiceImpl; import cz.vitfo.service.impl.SecurityServiceImpl; @Configuration public class Config { @Bean public MyApplication myApplication() { return new MyApplication(); } @Bean public MessageService messageService() { return new MessageServiceImpl(); } @Bean public SecurityService securityService() { return new SecurityServiceImpl(); } }
Main.java
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); MyApplication application = context.getBean(MyApplication.class); application.start(); context.close(); } }
MyApplication.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import cz.vitfo.service.MessageService; import cz.vitfo.service.SecurityService; @Component public class MyApplication { @Autowired private MessageService messageService; @Autowired private SecurityService securityService; public void start() { String message = messageService.getMessage(); String encoded = securityService.encode(message); System.out.println(message); System.out.println(encoded); } }
MessageService.java
public interface MessageService { String getMessage(); }
MessageServiceImpl.java
import org.springframework.stereotype.Service; import cz.vitfo.service.MessageService; @Service("messageService") public class MessageServiceImpl implements MessageService { public String getMessage() { return "message"; } }
SecurityService.java
public interface SecurityService { String encode(String text); }
SecurityServiceImpl.java
import org.springframework.stereotype.Service; import cz.vitfo.service.SecurityService; @Service("securityService") public class SecurityServiceImpl implements SecurityService { private static final String e = "€"; private static final String s = "§"; public String encode(String text) { text = text.replaceAll("e", e); text = text.replaceAll("s", s); return text; } }
Výsledek
message m€§§ag€
V tomto projektu chybí soubor beans.xml. Definice jsou v Java třídě Config.java. Všimněte si anotací @Bean, které odpovídají xml elementům …. a také anotace @Configuration, která třídu Config.java označuje za třídu s konfigurací. Používají se stejné anotace (@Component, @Service, @Autowired, ..) jako v případě xml konfiguračního souboru s component scan.