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)
XML konfigurační soubor a konfigurace anotacemi
V tomto příspěvku se podíváme na XML konfigurační soubor a konfigurace anotacemi. V tomto případě se používá pro konfiguraci XML soubor, ale pro injektování (vytvoření instance a dosazení do požadované proměnné) se používají 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 │ │ │ │ Main.java │ │ │ │ MyApplication.java │ │ │ │ │ │ │ └───service │ │ │ │ MessageService.java │ │ │ │ SecurityService.java │ │ │ │ │ │ │ └───impl │ │ │ MessageServiceImpl.java │ │ │ SecurityServiceImpl.java │ │ │ │ │ └───resources │ │ beans.xml
Vytvoříme si třídu Main.java, ve které vytvoříme kontext Springu. Ve třídě Main.java budeme mít logiku naší aplikace.
beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cz.vitfo"></context:component-scan>
</beans>
Main.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyApplication application = context.getBean("myApplication", 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
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
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 je v souboru beans.xml povolen component scan. To znamená, že Spring projde třídy v projektu a hledá releventní anotace. Např. @Service (specializovaná anotace pro @Component) která označuje Spring komponentu typu service. Pokud takovou najde vytvoří si instanci v kontextu a pokud je pak někde taková třída potřeba (autowiruje se pomocí anotaci @Autowired) dosadí do dané proměnné.