8. package com.targettrust.spring.extend; public abstract class PessoaService { private String nome ; private String telefone ; private String email ; public PessoaService() {} public String getNome() { return nome ; } public void setNome(String nome) { this . nome = nome; } public String getTelefone() { return telefone ; } public void setTelefone(String telefone) { this . telefone = telefone; } public String getEmail() { return email ; } public void setEmail(String email) { this . email = email; } }
9. package com.targettrust.spring.extend; public class PessoaFisicaService extends PessoaService { private String cpf ; public PessoaFisicaService() {} public boolean validaCpf(){ return CpfUtils. validaCPF ( cpf ); } public void mostraPessoa(){ if (validaCpf()){ System. out .println( "nome: " + getNome() + " telefone: " + getTelefone() + " email: " + getEmail() + " cpf: " + cpf ); } else { System. out .println( "Essa pessoa não tem um CPF Valido." ); } } public String getCpf() { return cpf ; } public void setCpf(String cpf) { this . cpf = cpf; } }
10. package com.targettrust.spring.extend; public class PessoaFisicaService extends PessoaService { private String cpf ; public PessoaFisicaService() {} public boolean validaCpf(){ return CpfUtils. validaCPF ( cpf ); } public void mostraPessoa(){ if (validaCpf()){ System. out .println( "nome: " + getNome() + " telefone: " + getTelefone() + " email: " + getEmail() + " cpf: " + cpf ); } else { System. out .println( "Essa pessoa não tem um CPF Valido." ); } } public String getCpf() { return cpf ; } public void setCpf(String cpf) { this . cpf = cpf; } }
11. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > < bean id = "pessoaService" class = "com.targettrust.spring.extend.PessoaService" abstract = "true" > < property name = "nome" value = "Diego" /> < property name = "email" value = "pacheco@diego.com.spring" /> < property name = "telefone" value = "455-55-55" /> </ bean > < bean abstract = "false" id = "pessoaFisicaService" class = "com.targettrust.spring.extend.PessoaFisicaService" parent = "pessoaService" > < property name = "cpf" value = "47888971210" /> </ bean > </ beans >
12.
13. package com.targettrust.spring.validate; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; public class Pessoa implements Validator{ private String nome ; private Integer idade ; public boolean supports(Class clazz) { return Pessoa. class .equals(clazz); } public void validate(Object target, Errors errors) { ValidationUtils. rejectIfEmpty (errors, "nome" , "nome.vazio" ); Pessoa p = (Pessoa) target; if (p.getIdade() < 0) { errors.rejectValue( "idade" , "valor negativo" ); } else if (p.getIdade() > 120) { errors.rejectValue( "idade" , "velho de mais" ); } } public String getNome() { return nome ; } public void setNome(String nome) { this . nome = nome; } public Integer getIdade() { return idade ; } public void setIdade(Integer idade) { this . idade = idade; } }
14. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" default-lazy-init = "true" > < bean id = "pessoa" class = "com.targettrust.spring.validate.Pessoa" > < property name = "nome" value = "Fulano" /> < property name = "idade" value = "-1" /> </ bean > </ beans >
15. package com.targettrust.spring.validate; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.validation.BeanPropertyBindingResult; import org.springframework.validation.Errors; public class Teste { public static void main(String[] args) { BeanFactory bf = new ClassPathXmlApplicationContext( "/com/targettrust/spring/validate/Spring-beans.xml" ); Pessoa p = (Pessoa) bf.getBean( "pessoa" ); System. out .println( "Nome: " + p.getNome() + " idade: " + p.getIdade()); Errors e = new BeanPropertyBindingResult(p, "Pessoa" ); p.validate(p, e); for (Object es: e.getAllErrors()){ System. out .println(es); } } } Chamada manual !!!
16.
17.
18.
19.
20.
21.
22.
23. package com.targettrust.spring.beanpostprocessors; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class LogCreationBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System. out .println( "Craindo bean: " + beanName); return bean; } }
24. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" default-lazy-init = "false" > < bean id = "a" class = "com.targettrust.spring.beanpostprocessors.ObjetoA" /> < bean id = "b" class = "com.targettrust.spring.beanpostprocessors.ObjetoB" /> < bean id = "c" class = "com.targettrust.spring.beanpostprocessors.ObjetoC" /> < bean class = "com.targettrust.spring.beanpostprocessors.LogCreationBeanPostProcessor” /> </ beans >
25. package com.targettrust.spring.beanpostprocessors; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Teste { public static void main(String[] args) { new ClassPathXmlApplicationContext( "/com/targettrust/spring/" + "beanpostprocessors/Spring-beans.xml" ); } }
26.
27.
28. package com.targettrust.spring.beanfactorypostprocessors; import java.text.SimpleDateFormat; import java.util.Date; public class DataService { private String pattern ; public String getPattern() { return pattern ; } public void setPattern(String pattern) { this . pattern = pattern; } public String showSysDate(){ SimpleDateFormat sdf = new SimpleDateFormat( pattern ); return sdf.format( new Date()); } }
29. package com.targettrust.spring.beanfactorypostprocessors; import java.io.FileInputStream; import java.util.Map; import java.util.Properties; import java.util.Map.Entry; import org.springframework.beans.BeansException; import org.springframework.beans.FatalBeanException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class DatePatternRouterBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { Properties p = new Properties(); try { p.load( new FileInputStream( "./src/com/targettrust/spring/” + beanfactorypostprocessors/patterns.properties" )); System. out .println( "Patterns: " + p); } catch (Exception e) { throw new FatalBeanException( "Erro ao buscar patterns!" ,e); } Map<Object,Object> beans = beanFactory.getBeansOfType(DataService. class ); for (Entry<Object, Object> e: beans.entrySet()){ System. out .println( "Aplicando pattern em service: " + e); DataService service = ((DataService)e.getValue()); service.setPattern(p.getProperty( "pattern." + service.getPattern().replace( "#" , "" ))); } } }
30. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" default-lazy-init = "false" > < bean id = "brasilDateService" class = "com.targettrust.spring.beanfactorypostprocessors.DataService" > < property name = "pattern" value = "#brasil" /> </ bean > < bean id = "usDateService" class = "com.targettrust.spring.beanfactorypostprocessors.DataService" > < property name = "pattern" value = "#us" /> </ bean > < bean class = "com.targettrust.spring.beanfactorypostprocessors.DatePatternRouterBeanFactoryPostProcessor" /> </ beans >
31. package com.targettrust.spring.beanfactorypostprocessors; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Teste { public static void main(String[] args) { ApplicationContext ap = new ClassPathXmlApplicationContext( "/com/targettrust/spring/beanfactorypostprocessors/Spring-beans.xml" ); System. out .println( "Data Brasil pattern : " + ((DataService)ap.getBean( "brasilDateService" )).showSysDate()); System. out .println( "Data Us pattern : " + (DataService)ap.getBean( "usDateService" )).showSysDate()); } }
32.
33.
34.
35.
36.
37. package com.targettrust.spring.propertyeditor; public class Aluno { private String nome ; public Aluno() {} public Aluno(String nome) { super (); this . nome = nome; } public String getNome() { return nome ; } public void setNome(String nome) { this . nome = nome; } @Override public String toString() { return nome ; } }
38. package com.targettrust.spring.propertyeditor; import java.util.List; public class Turma { private String nome ; private List<Aluno> alunos ; public Turma() {} public Turma(String nome, List<Aluno> alunos) { super (); this . nome = nome; this . alunos = alunos; } public String getNome() { return nome ; } public void setNome(String nome) { this . nome = nome; } public List<Aluno> getAlunos() { return alunos ; } public void setAlunos(List<Aluno> alunos) { this . alunos = alunos; } @Override public String toString() { return nome + " alunos: " + alunos .toString(); } }
39. package com.targettrust.spring.propertyeditor; import java.beans.PropertyEditorSupport; public class AlunoEditor extends PropertyEditorSupport{ @Override public void setAsText(String text) throws IllegalArgumentException { Aluno a = new Aluno(text); setValue(a); } }
40. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > < bean id = "turma“ class = "com.targettrust.spring.propertyeditor.Turma" > < property name = "nome" value = "Curso-Spring" /> < property name = "alunos" > < list > < value > Rod </ value > < value > Joe </ value > < value > Bart </ value > < value > Homer </ value > < value > Hammer </ value > </ list > </ property > </ bean > < bean id = "customEditorConfigurer“ class = "org.springframework.beans.factory.config.CustomEditorConfigurer“ > < property name = "customEditors" > < map > < entry key = "com.targettrust.spring.propertyeditor.Aluno" > < bean class = "com.targettrust.spring.propertyeditor.AlunoEditor" /> </ entry > </ map > </ property > </ bean > </ beans >
41.
42.
43. package com.targettrust.spring.event; import org.springframework.context.ApplicationEvent; public class FeriadoEvent extends ApplicationEvent{ private String data ; private static final long serialVersionUID = 1L; public FeriadoEvent(Object source,String data) { super (source); this . data = data; } public String getData() { return data ; } }
44. package com.targettrust.spring.event; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; public class FeriadoListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if (event instanceof FeriadoEvent){ FeriadoEvent fe = (FeriadoEvent)event; System. out .println( "Viva! Dia: " + fe.getData() + " é feriado. Uhhuu!!!" ); } } }
45. package com.targettrust.spring.event; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class RH implements ApplicationContextAware { private ApplicationContext ac ; public void pulicarFeriados(){ ac .publishEvent( new FeriadoEvent( this , "01/01/08" )); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ac = applicationContext; } }
46. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > < bean id = "rh" class = "com.targettrust.spring.event.RH" /> < bean id = "funcionarioEsperto " class = "com.targettrust.spring.event.FeriadoListener" /> </ beans >
47. package com.targettrust.spring.event; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Teste { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "/com/targettrust/spring/event/Spring-beans.xml" ); RH rh = (RH)ac.getBean( "rh" ); rh.pulicarFeriados(); } }
48.
49.
50. package com.targettrust.spring.propertyplaceholderconfigurer; public class Instrutor { private String nome ; private int idade ; private String time ; private float altura ; // getters and Setters } nome = deigo idade = 22 altura = 1.85 Arquivo .properties
51. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > < bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > < property name = "location“ > < value > classpath:com/targettrust/spring/propertyplaceholderconfigurer/config.properties </ value > </ property > < property name = "properties" > < value > time=Grêmio </ value > </ property > </ bean > < bean id = "I” class = "com.targettrust.spring.propertyplaceholderconfigurer.Instrutor" > < property name = "nome" value = "${nome}" /> < property name = "idade" value = "${idade}" /> < property name = "time" value = "${time}" /> < property name = "altura" value = "${altura}" /> </ bean > </ beans >
52.
53. package com.targettrust.spring.singletonbeanfactorylocator; public class ObjetoA {} <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" default-lazy-init = "true" > < bean id = "A1" class = "com.targettrust.spring.singletonbeanfactorylocator.ObjetoA” /> </ beans > + A2 A3 A4
54. <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" default-lazy-init = "false" > < bean id = "beanFactoryBean“ class = "org.springframework.context.support.ClassPathXmlApplicationContext” > < constructor-arg > < list > < value > com/targettrust/spring/singletonbeanfactorylocator/Spring-beans-A.xml </ value > < value > com/targettrust/spring/singletonbeanfactorylocator/Spring-beans-B.xml </ value > < value > com/targettrust/spring/singletonbeanfactorylocator/Spring-beans-C.xml </ value > </ list > </ constructor-arg > </ bean > < bean id = "beanFactoryBean2“ class = "org.springframework.context.support.ClassPathXmlApplicationContext“ > < constructor-arg > < list > < value > com/targettrust/spring/singletonbeanfactorylocator/Spring-beans-D.xml </ value > </ list > </ constructor-arg > </ bean > </ beans >
57. package com.targettrust.spring.i18n; import java.util.Locale; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceAware; public class Pessoa implements MessageSourceAware{ private String nome ; private int idade ; private MessageSource ms ; // Getters e Setters public void setIdade( int idade) { if ( idade<=0 || idade >= 120 ) throw new IllegalArgumentException( ms .getMessage( "idade.invalida" , new Object[]{idade},Locale. getDefault ())); this . idade = idade; } }
58. idade.invalida= A idade: {0} não é aceitável por que é muito velha ou menor do que 1 <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > < bean id = "messageSource“ class = "org.springframework.context.support.ResourceBundleMessageSource“ > < property name = "basenames" > < list > < value > mensagems </ value > </ list > </ property > </ bean > < bean id = "pessoa“ class = "com.targettrust.spring.i18n.Pessoa“ /> </ beans >
59. package com.targettrust.spring.i18n; import java.util.Locale; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Teste { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "/com/targettrust/spring/i18n/Spring-beans.xml" ); Locale. setDefault ( new Locale( "pt" , "BR" )); setIdade (ac); } private static void setIdade(ApplicationContext ac){ Pessoa p = (Pessoa)ac.getBean( "pessoa" ); p.setNome( "Crom" ); try { p.setIdade(200); } catch (RuntimeException re){ re.printStackTrace(); } } }