Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 11 ■ Integrating Your Spring Application with External Systems import com.apress.isf.java.model.Document; import com.apress.isf.java.utils.XmlUtils; import com.apress.isf.spring.data.DocumentDAO; @Component public class JMSConsumer implements MessageListener{ } @Autowired DocumentDAO documentDAO; @Override public void onMessage(Message message) { TextMessage textMessage = (TextMessage)message; try{ Document document = XmlUtils.fromXML(textMessage.getText(), Document.class); documentDAO.save(document); }catch(JMSException ex){ ex.printStackTrace(); } } Let’s analyze what you are doing in Listing 11-2. First, you are implementing the javax.jms.MessageListener. The JMS API exposes this interface where you need to implement the onMessage method; this method accepts a javax.jms.Message that will contain the message. Also, you are using a small Utility class, which will help to convert the XML into a document object and be saved into the database using the DocumentDAO implementation. Next, take a look at Listing 11-3, which shows the Utility class that is used in the consumer. Listing 11-3. XmlUtils.java package com.apress.isf.java.utils; import com.thoughtworks.xstream.XStream; public class XmlUtils { public static String toXML(T object){ XStream xstream = new XStream(); xstream.alias(object.getClass().getSimpleName().toLowerCase(), object.getClass()); return xstream.toXML(object); } } @SuppressWarnings({ "unchecked"}) public static T fromXML(String xml, Class _class){ XStream xstream = new XStream(); xstream.alias(_class.getSimpleName().toLowerCase(), _class); return (T)xstream.fromXML(xml); } 151
Chapter 11 ■ Integrating Your Spring Application with External Systems Listing 11-3 uses XStream, a library that serializes/deserializes objects into an XML String back and forth. Because you need to save an actual document object, it is necessary to add this helper to the application. Note in Listing 11-2 that there is a documentDAO.save(document)call. This is a new method that you will add to the DocumentDAO implementation that will save a Document object. See Listing 11-4, the DocumentRepository class. Listing 11-4. DocumentRepository.java package com.apress.isf.spring.data; import java.util.Date; import java.util.List; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.apress.isf.java.model.Document; @Repository("documentDAO") public class DocumentRepository implements DocumentDAO { @Autowired private DataSource dataSource; @Autowired private String query; @Autowired private String insert; @Autowired private String find; @Autowired private String update; public List getAll() { return new JdbcTemplate(dataSource).query(query, new DocumentRowMapper()); } public Document findById(String id) { Document updateDocument = null; JdbcTemplate template = new JdbcTemplate(dataSource); } try { updateDocument = template.queryForObject(find, new Object[] { id }, new DocumentRowMapper()); } catch (EmptyResultDataAccessException ex) {} return updateDocument; 152
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113 and 114: Chapter 9 ■ Adding Persistence to
- Page 115 and 116: Chapter 9 ■ Adding Persistence to
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
- Page 121 and 122: Chapter 9 ■ Adding Persistence to
- Page 123 and 124: Chapter 9 ■ Adding Persistence to
- Page 125 and 126: Chapter 9 ■ Adding Persistence to
- Page 127 and 128: Chapter 9 ■ Adding Persistence to
- Page 129 and 130: Chapter 9 ■ Adding Persistence to
- Page 131 and 132: Chapter 9 ■ Adding Persistence to
- Page 133 and 134: Chapter 10 ■ Showing Your Spring
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
- Page 137 and 138: Chapter 10 ■ Showing Your Spring
- Page 139 and 140: Chapter 10 ■ Showing Your Spring
- Page 141 and 142: Chapter 10 ■ Showing Your Spring
- Page 143 and 144: Chapter 10 ■ Showing Your Spring
- Page 145 and 146: Chapter 11 ■ Integrating Your Spr
- Page 147 and 148: Chapter 11 ■ Integrating Your Spr
- Page 149: Chapter 11 ■ Integrating Your Spr
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
- Page 159 and 160: Chapter 11 ■ Integrating Your Spr
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
- Page 165 and 166: Chapter 11 ■ Integrating Your Spr
- Page 167 and 168: Chapter 12 Exposing a REST API Nowa
- Page 169 and 170: Chapter 12 ■ Exposing a REST API
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
- Page 177 and 178: Chapter 12 ■ Exposing a REST API
- Page 179 and 180: HTTP-DELETE Chapter 12 ■ Exposing
- Page 181 and 182: Chapter 13 Adding E-mail and Schedu
- Page 183 and 184: Chapter 13 ■ Adding E-mail and Sc
- Page 185 and 186: Chapter 13 ■ Adding E-mail and Sc
- Page 187 and 188: Chapter 13 ■ Adding E-mail and Sc
- Page 189 and 190: Chapter 13 ■ Adding E-mail and Sc
- Page 191 and 192: Part III Spring Framework Advanced
- Page 193 and 194: Chapter 14 ■ Using Dynamic Langua
- Page 195 and 196: Chapter 14 ■ Using Dynamic Langua
- Page 197 and 198: Chapter 14 ■ Using Dynamic Langua
- Page 199 and 200: Chapter 14 ■ Using Dynamic Langua
Chapter 11 ■ Integrating Your <strong>Spring</strong> Application with External Systems<br />
Listing 11-3 uses XStream, a library that serializes/deserializes objects into an XML String back and forth.<br />
Because you need to save an actual document object, it is necessary to add this helper to the application. Note<br />
in Listing 11-2 that there is a documentDAO.save(document)call. This is a new method that you will add to the<br />
DocumentDAO implementation that will save a Document object. See Listing 11-4, the DocumentRepository class.<br />
Listing 11-4. DocumentRepository.java<br />
package com.apress.isf.spring.data;<br />
import java.util.Date;<br />
import java.util.List;<br />
import javax.sql.DataSource;<br />
import org.springframework.beans.factory.annotation.Autowired;<br />
import org.springframework.dao.EmptyResultDataAccessException;<br />
import org.springframework.jdbc.core.JdbcTemplate;<br />
import org.springframework.stereotype.Repository;<br />
import com.apress.isf.java.model.Document;<br />
@Repository("documentDAO")<br />
public class DocumentRepository implements DocumentDAO {<br />
@Autowired<br />
private DataSource dataSource;<br />
@Autowired<br />
private String query;<br />
@Autowired<br />
private String insert;<br />
@Autowired<br />
private String find;<br />
@Autowired<br />
private String update;<br />
public List getAll() {<br />
return new JdbcTemplate(dataSource).query(query,<br />
new DocumentRowMapper());<br />
}<br />
public Document findById(String id) {<br />
Document updateDocument = null;<br />
JdbcTemplate template = new JdbcTemplate(dataSource);<br />
}<br />
try {<br />
updateDocument = template.queryForObject(find,<br />
new Object[] { id },<br />
new DocumentRowMapper());<br />
} catch (EmptyResultDataAccessException ex) {}<br />
return updateDocument;<br />
152