Spring Framework/Configuration via annotations

, Author

br>>

Spring oferece a capacidade de ligar feijões via anotações no código e não via o ficheiro de configuração xml. Podemos tomar o exemplo de uma classe MessageDisplayer que chama outra classe MessageDisplayer2. Esta relação então não precisa de ser especificamente declarada no ficheiro Beans:

<?xml version="1.0" encoding="UTF-8"?><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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config /><bean class="mainPackage.MessageDisplayer"></bean></beans>

Classe principal:

package mainPackage;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.support.ClassPathXmlApplicationContext;@ComponentScan(basePackages = "mainPackage")public class MainClass{public static void main(String args){ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("Beans.xml");MessageDisplayer messageDisplayer = classPathXmlApplicationContext.getBean(MessageDisplayer.class);messageDisplayer.setMessage("test");messageDisplayer.displayMessage();classPathXmlApplicationContext.close();}}

P>Primeira classe de exibição:

package mainPackage;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.ComponentScan;import org.springframework.stereotype.Component;@[email protected]("mainPackage")public class MessageDisplayer{@AutowiredMessageDisplayer2 messageDisplayer2;private String message;public String getMessage(){return message;}public void setMessage(String message){this.message = message;}public void displayMessage(){System.out.println("message from MessageDisplayer : " + message);messageDisplayer2.setMessage("test2");messageDisplayer2.displayMessage();}}

br>>Segunda classe de exibição, chamada pela primeira:

package mainPackage;import org.springframework.stereotype.Component;@Componentpublic class MessageDisplayer2{private String message;public String getMessage(){return message;}public void setMessage(String message){this.message = message;}public void displayMessage(){System.out.println("message from MessageDisplayer2 : " + message);}}

Resultado de exibição:

mai 09, 2018 5:57:39 PM org.springframework.context.support.AbstractApplicationContext prepareRefreshINFOS: Refreshing org[email protected]2328c243: startup date ; root of context hierarchymai 09, 2018 5:57:39 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFOS: Loading XML bean definitions from class path resource message from MessageDisplayer : testmessage from MessageDisplayer2 : test2mai 09, 2018 5:57:40 PM org.springframework.context.support.AbstractApplicationContext doCloseINFOS: Closing org[email protected]2328c243: startup date ; root of context hierarchy

Deixe uma resposta

O seu endereço de email não será publicado. Campos obrigatórios marcados com *