File/ New Project / Maven / new Java Application …
Vyplniť
Project name: SpringDemo1
Ostatné môžete ponechať
Otvoriť pom.xml:
<projekt>: Project Files/pom.xml
a pridať tam:
1 2 3 4 5 6 7 |
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.1.RELEASE</version> </dependency> </dependencies> |
Message Service bean – poskytuje správy
interface MessageServiceIfc
1 2 3 |
public interface MessageServiceIfc { String getMessage(); } |
trieda MessageServiceMock (dummy implementácia)
1 2 3 4 5 6 |
public class MessageServiceMock implements MessageServiceIfc { @Override public String getMessage() { return "Tu je mock!"; } } |
Message Procesor bean – spracovava správy
trieda MessageProcessor
1 2 3 4 5 6 7 8 9 10 11 12 |
public class MessageProcessor { private MessageServiceIfc messageService; public MessageProcessor(MessageServiceIfc messageService) { this.messageService = messageService; } public void processMessage() { System.out.println(messageService.getMessage()); } } |
<project> new/other/other/springXmlConfig -> vyplniť:
FileName: beans
a do vytvoreného beans.xml doplniť:
1 2 3 4 |
<bean id="service" class="asos.springdemo1.MessageServiceMock"/> <bean id="processor" class="asos.springdemo1.MessageProcessor"> <constructor-arg ref="service"/> </bean> |
Pozn. Konfigurácia IoC kontainera definuje graf komponent, z ktorých pozostáva spring aplikácia Môžeme ju definovať aj inými spôsobmi než xml (pomocou anotacií alebo java – téma pre cvičenia).
1 2 3 4 5 6 7 8 9 10 |
public class DemoApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans.xml"}); MessageProcessor mp = context.getBean("processor", MessageProcessor.class); mp.processMessage(); } } |
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.