Spring Framework Stuff

Overview

  • What is it
    • Lightweight Framework for Java applications which lets you rapidly develop applications without creating dependencies on any J2EE framework stuff.
  • Main Concept
    • Dependency injection and inversion of control to stop your apps having a bunch of stuff you don't need just because it comes with a framework.
  • Martin Fowler's article on Inversion of Control/Dependency Injection

Getting Started with Spring MVC (OUTDATED)

Examples

  • Below is a link to an article about the Spring Framework 2.0 XML Schema. This basically allows the use of standard tags instead of everything being a <bean> tag.
Eg
        <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/petclinic"/>
instead of
        <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
         <property name="jndiName" value="jdbc/MyDataSource"/>
        </bean>

Spring Mail

This is a simple mail runner that uses spring classes to programmatically send a mail. There's no config via xml or otherwise. Stay tuned for an xml config version.

Main Class

    package springmailtest;

    /**
     * Test of sending mail using Spring
     * Using example in ProSpring p521
     * @author Darren
     */
    public class Main {
        private static final String TO = "youraddress@gmail.com";
        private static final String TEXT = "this is a call";

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            SimpleMailSender sender = new JavaMailSimpleMailSender();

            sender.sendMessage(TO, TEXT);
        }

    }

Abstract class for mail sending


    package springmailtest;

    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;

    /**
     *
     * @author Darren
     */
    public abstract class SimpleMailSender {
        protected abstract MailSender getMailSender();

        public final void sendMessage(String to, String text){
            SimpleMailMessage msg = new SimpleMailMessage();
            msg.setTo(to);
            msg.setSubject("Test Message");
            msg.setFrom("test@test.net");
            msg.setText(text);

            MailSender sender = getMailSender();
            sender.send(msg);
        }
    }

Implementation class that uses the Spring Java mail class


    package springmailtest;

    import java.util.Properties;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.javamail.JavaMailSenderImpl;

    /**
     *
     * @author Darren
     */
    public class JavaMailSimpleMailSender extends SimpleMailSender{
        protected MailSender getMailSender(){
            JavaMailSenderImpl sender = new JavaMailSenderImpl();

            // Set the java mail properties
            Properties p = new Properties();
            p.put("mail.smtp.auth", "true");
            sender.setJavaMailProperties(p);

            sender.setHost("mail.smtpServer.net");
            sender.setPort(26);
            sender.setUsername("user@yourdomain.net");
            sender.setPassword("THe passsword");
            return sender;
        }
    }

SpringBoot on Heroku

Spring examples

Mappings in web.xml

<listener>

    <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<servlet>

    <servlet-name> springrest</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup> 1</load-on-startup>

</servlet>

<servlet-mapping\>

    <servlet-name> springrest</servlet-name>
    <url-pattern> /spring/*</url-pattern>

</servlet-mapping>

Mapping in @Annotated Controller

@Controller
public class SpringRestController {

    @RequestMapping("/displayDog")
    public @ResponseBody SmallDog displayDog(Model model) {

        System.out.println("Display Dog called......wooof");

        SmallDog roddy = new SmallDog();
        roddy.setName("Roddy Boland");
        roddy.setHungry(true);
        roddy.setNumberofLegs(4);

        return roddy;
    }
}

This setup allows us to have both GWT and Spring apps deployed in the same server

Paralleling requests in Spring

Consuming REST service from Spring Server Example

@Autowired vs @Inject and Constructor injection annotations

Same thing just Spring vs JSR

Constructor Injection example

In this case the SearchService must be provided for the bean or it will fail at startup

private SearchService service;

@Inject public MyClass(SearchService service){
    this.service = service;
}

Spring Boot

Testing MVC in Spring Boot

@WebMvcTest(RoomReservationWebController.class)
class RoomReservationWebControllerTest {
  @MockBean
  private ReservationService reservationService;

  @Autowired
  private MockMvc mockMvc;

  @Test
  public void getReservations() throws Exception{
    String dateString = "2020-01-01";
    Date date = DateUtils.createDateFromDateString(dateString);
    List<RoomReservation> roomReservations = new ArrayList<>();
    RoomReservation roomReservation = new RoomReservation();
    roomReservation.setLastName("Unit");
    roomReservation.setFirstName("Junit");
    roomReservation.setDate(date);
    roomReservation.setGuestId(1);
    roomReservation.setRoomId(100);
    roomReservation.setRoomName("Junit Room");
    roomReservation.setRoomNumber("J1");
    roomReservations.add(roomReservation);
    given(reservationService.getRoomReservationsForDate(date)).willReturn(roomReservations);

    this.mockMvc.perform(get("/reservations?date=2020-01-01"))
        .andExpect(status().isOk())
        .andExpect(content().string(containsString("Unit, Junit")));
  }
}