6. package com.targettrust.spring.email; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.MailException; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class TesteEnviaEmail { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "/com/targettrust/spring/email/Spring-beans.xml" ); SimpleMailMessage msg = (SimpleMailMessage)ac.getBean( "simpleMailMessage" ); MailSender ms = (MailSender)ac.getBean( "mailSender" ); try { ms.send(msg); } catch (MailException ex) { ex.printStackTrace(); } } }
7. package com.targettrust.spring.email; import java.io.File; import javax.mail.internet.MimeMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; public class TesteMimeMessageHelperAtt { public static void main( String [] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "/com/targettrust/spring/email/Spring-beans.xml" ); JavaMailSender ms = (JavaMailSender)ac.getBean( "mailSender" ); try { MimeMessage message = ms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true ); helper.setFrom( "from@server.com.br" ); helper.setTo( "to@server.com" ); helper.setText( "Email MimeMessageHelper" ); FileSystemResource file = new FileSystemResource( new File( "." ).getCanonicalPath() + "/build.xml" ); helper.addAttachment( "build.xml" , file); ms.send(message); } catch (Exception e){ e.printStackTrace(); } } }
8. package com.targettrust.spring.email; import java.io.File; import javax.mail.internet.MimeMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; public class TesteMimeMessageHelperAttImg { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "/com/targettrust/spring/email/Spring-beans.xml" ); JavaMailSender ms = (JavaMailSender)ac.getBean( "mailSender" ); try { MimeMessage message = ms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true ); helper.setFrom( "form@server.com.br" ); helper.setTo( "dest@server.com" ); helper.setText( "<html><body><img src='cid:img1'><br>Email MimeMessageHelper com suporte a imagems em linha!</body></html>" , true ); FileSystemResource file = new FileSystemResource( new File( "." ).getCanonicalPath() + "/imagem.jpg" ); helper.addInline( "img1" , file); ms.send(message); } catch (Exception e){ e.printStackTrace(); } } }
9.
10. package com.targettrust.spring.jdktask; import java.util.Date; import java.util.TimerTask; public class HoraCertaService extends TimerTask{ @Override @SuppressWarnings ( "deprecation" ) public void run() { System. out .println( new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds() ); } }
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 = "horaCertaService" class = "com.targettrust.spring.jdktask.HoraCertaService" /> < bean id = "scheduledTask" class = "org.springframework.scheduling.timer.ScheduledTimerTask" lazy-init = "false" > <!-- Espera 0 ms antes de iniciar --> < property name = "delay" value = "0" /> <!-- roda de 1 em 1 segundo --> < property name = "period" value = "1000" /> <!-- Ira executar a TimerTask horaCertaService --> < property name = "timerTask" ref = "horaCertaService" /> </ bean > </ beans >
12. < bean id = "timerFactory“ class = "org.springframework.scheduling.timer.TimerFactoryBean” > < property name = "scheduledTimerTasks" > < list > < ref bean = "scheduledTask" /> </ list > </ property > </ bean >
13. <? 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 = "horaCertaServiceNaoAcoplada" class = "com.targettrust.spring.jdktask.HoraCertaServiceNaoAcoplada“ /> < bean id = "scheduledTask“ lazy-init = "false" class = "org.springframework.scheduling.timer.ScheduledTimerTask” > <!-- Espera 0 ms antes de iniciar --> < property name = "delay" value = "0" /> <!-- roda de 1 em 1 segundo --> < property name = "period" value = "1000" /> <!-- Ira executar a TimerTask horaCertaService --> < property name = "timerTask" ref = "executor" /> </ bean > < bean id = "executor" class = "org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean“ > < property name = "targetObject" ref = "horaCertaServiceNaoAcoplada" /> < property name = "targetMethod" value = "showTime" /> </ bean > < bean id = "timerFactory" class = "org.springframework.scheduling.timer.TimerFactoryBean“ > < property name = "scheduledTimerTasks" > < list > < ref bean = "scheduledTask" /> </ list > </ property > </ bean > </ beans >
14.
15.
16.
17. package com.targettrust.spring.aop; public interface Service { public void fazAlgo(); } package com.targettrust.spring.aop; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ServiceA implements Service{ private static final Log log = LogFactory. getLog (ServiceA. class ); @Override public void fazAlgo() { log .info( "Fiz algo do tipo A" ); } } package com.targettrust.spring.aop; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ServiceB implements Service{ private static final Log log = LogFactory. getLog (ServiceB. class ); @Override public void fazAlgo() { log .info( "Fiz algo do tipo B" ); } }
18. package com.targettrust.spring.aop; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ServiceC implements Service{ private static final Log log = LogFactory. getLog (ServiceC. class ); @Override public void fazAlgo() { log .info( "Fiz algo do tipo C" ); } }
19. <? 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:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" xsi:schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" > < aop:aspectj-autoproxy /> < bean id = "aspecto“ lazy-init = "false" class = "com.targettrust.spring.aop.Aspecto“ /> < bean id = "sa“ class = "com.targettrust.spring.aop.ServiceA“ /> < bean id = "sb“ class = "com.targettrust.spring.aop.ServiceB“ /> < bean id = "sc“ class = "com.targettrust.spring.aop.ServiceC“ /> < bean id = "services“ class = "java.util.ArrayList“ > < constructor-arg index = "0" > < list > < ref bean = "sa" /> < ref bean = "sb" /> < ref bean = "sc" /> </ list > </ constructor-arg > </ bean > </ beans >
20. package com.targettrust.spring.aop; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class Aspecto { private static final Log log = LogFactory. getLog (Aspecto. class ); @Before ( "execution(* com.targettrust.spring.aop.Service.*(..))" ) public void execucaoDeFazAlgoAntes() { log .info( "To sabendo antes da execu ç ão de Service" ); } @After ( "execution(* com.targettrust.spring.aop.Service.*(..))" ) public void execucaoDeFazAlgoDepois() { log .info( "To sabendo depois da execu ç ão de Serice" ); } @Around ( "execution(* com.targettrust.spring.aop.ServiceB.faz*(..)))" ) public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { Object retVal = pjp.proceed(); log .info( "To sabendo around SericeB" ); return retVal; } }
21.
22.
23. package com.targettrust.spring.testing; import java.util.Date; import junit.framework.Assert; import org.springframework.test.AbstractDependencyInjectionSpringContextTests; public class TestDataService extends AbstractDependencyInjectionSpringContextTests { private DataService dataService ; public void setDataService(DataService dataService) { this . dataService = dataService; } @Override protected String[] getConfigLocations() { return new String[]{ "classpath:com/targettrust/spring/testing/Spring-beans.xml" }; } @SuppressWarnings ( "deprecation" ) public void testDataDoDataService(){ Date d = dataService .getSysDate(); Date l = new Date(); Assert. assertEquals (d.getDay(),l.getDay()); Assert. assertEquals (d.getMonth(),l.getMonth()); Assert. assertEquals (d.getYear(),l.getYear()); } }
24.
25. package com.targettrust.spring.remoting; import java.util.Date; public interface HoraService { public Date getDate(); } package com.targettrust.spring.remoting; import java.util.Calendar; import java.util.Date; public class HoraServiceImpl implements HoraService{ public Date getDate() { return Calendar. getInstance ().getTime(); } } <? 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 = "horaService" class = "com.targettrust.spring.remoting.HoraServiceImpl” /> < bean class = "org.springframework.remoting.rmi.RmiServiceExporter” > < property name = "serviceName" value = "Target-HoraService" /> < property name = "service" ref = "horaService" /> < property name = "serviceInterface“ value = "com.targettrust.spring.remoting.HoraService" /> < property name = "registryPort" value = "1199" /> </ bean > </ beans >
26. <? 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 = "horaService” class = "org.springframework.remoting.rmi.RmiProxyFactoryBean" > < property name = "serviceUrl" value = "rmi://localhost:1199/Target-HoraService" /> < property name = "serviceInterface" value = "com.targettrust.spring.remoting.HoraService" /> </ bean > </ beans >