Advertisement
Guest User

Untitled

a guest
Apr 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>/WEB-INF/spring/root-context.xml</param-value>
  5. </context-param>
  6.  
  7. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11.  
  12. <!-- Processes application requests -->
  13. <servlet>
  14. <servlet-name>controller</servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <init-param>
  17. <param-name>contextConfigLocation</param-name>
  18. <param-value>/WEB-INF/spring/controller.xml</param-value>
  19. </init-param>
  20. <load-on-startup>1</load-on-startup>
  21. </servlet>
  22.  
  23. <servlet-mapping>
  24. <servlet-name>controller</servlet-name>
  25. <url-pattern>/</url-pattern>
  26. </servlet-mapping>
  27.  
  28. <?xml version="1.0" encoding="UTF-8"?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  31. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  32. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
  33.  
  34. <!-- DBCP DataSource -->
  35. <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"
  36. destroy-method="close">
  37. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  38. <property name="url" value="jdbc:mysql://localhost/spring" />
  39. <property name="username" value="root" />
  40. <property name="password" value="root" />
  41. </bean>
  42.  
  43. <!-- JPA EntityManagerFactory -->
  44. <bean id="entityManagerFactory"
  45. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  46. <property name="jpaVendorAdapter">
  47. <bean id="jpaVendorAdapter"
  48. class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  49. <property name="showSql" value="true" />
  50. <property name="generateDdl" value="true" />
  51. <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
  52. </bean>
  53. </property>
  54. <property name="dataSource" ref="dataSource" />
  55. </bean>
  56.  
  57. <!-- Persistence Context Annotated -->
  58. <bean
  59. class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
  60. id="persistenceAnnotationBeanPostProcessor" />
  61.  
  62. <!-- Transaction -->
  63. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  64. <property name="entityManagerFactory" ref="entityManagerFactory" />
  65. <property name="dataSource" ref="dataSource" />
  66. </bean>
  67.  
  68. <tx:annotation-driven transaction-manager="transactionManager" />
  69.  
  70. </beans>
  71.  
  72. <?xml version="1.0" encoding="UTF-8"?>
  73. <persistence xmlns="http://java.sun.com/xml/ns/persistence"
  74. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  75. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
  76. http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  77. version="2.0">
  78.  
  79. <persistence-unit name="persist" transaction-type="RESOURCE_LOCAL">
  80. <properties>
  81. <property name="hibernate.hbm2ddl.auto" value="create-drop" />
  82. <property name="hibernate.show_sql" value="true" />
  83. <property name="hibernate.transaction.flush_before_completion"
  84. value="true" />
  85. <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
  86. </properties>
  87. </persistence-unit>
  88.  
  89. </persistence>
  90.  
  91. @Entity
  92. public class Person {
  93.  
  94. private int id;
  95. private String name;
  96.  
  97. @Id
  98. @GeneratedValue
  99. public int getId() {
  100. return id;
  101. }
  102.  
  103. // ..
  104.  
  105. }
  106.  
  107. @Repository
  108. @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
  109. public class PersonDaoImpl implements PersonDao {
  110.  
  111. private EntityManager entityManager;
  112.  
  113. @PersistenceContext
  114. public void setEntityManager(EntityManager entityManager) {
  115. this.entityManager = entityManager;
  116. }
  117.  
  118. @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
  119. public Person persist(Person person) {
  120. entityManager.persist(person);
  121. return person;
  122. }
  123.  
  124. // ...
  125.  
  126. }
  127.  
  128. @Service
  129. public class PersonServiceImpl implements PersonService {
  130.  
  131. private PersonDao personDao;
  132.  
  133. @Inject
  134. public void setPersonDao(PersonDao personDao) {
  135. this.personDao = personDao;
  136. }
  137.  
  138. public Person create(Person person) {
  139. return personDao.persist(person);
  140. }
  141.  
  142. // @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
  143. public Person save(Person person) {
  144. return personDao.merge(person);
  145. }
  146.  
  147. // ...
  148.  
  149. }
  150.  
  151. @Controller
  152. @Scope("prototype")
  153. @RequestMapping("/person")
  154. public class PersonController {
  155.  
  156. private PersonService personService;
  157.  
  158. @Inject
  159. public void setPersonService(PersonService personService) {
  160. this.personService = personService;
  161. }
  162.  
  163. @RequestMapping(value = "/add", method = RequestMethod.POST)
  164. public ModelAndView add(@RequestParam("username") String name) {
  165. Person person = new Person();
  166. person.setName(name);
  167. System.out.println(name); // i can get the value
  168. personService.create(person); // nothing happend... throws no Excp
  169. ModelAndView mav = new ModelAndView();
  170. mav.setViewName("result");
  171. mav.addObject("val", person.getName());
  172. return mav;
  173. }
  174.  
  175. }
  176.  
  177. @RunWith(SpringJUnit4ClassRunner.class)
  178. @ContextConfiguration("/test-context.xml")
  179. public class PersonControllerTest {
  180.  
  181. private PersonController controller;
  182.  
  183. @Inject
  184. public void setController(PersonController controller) {
  185. this.controller = controller;
  186. }
  187.  
  188.  
  189. // the test passed no matter @Transactional or not, if not, the db get the row
  190. @Test(/*expected = NullPointerException.class*/)
  191. @Transactional
  192. public void testAdd() {
  193. // PersonController controller = new PersonController();
  194. assertEquals("test", controller.add("test").getModelMap().get("val"));
  195. }
  196.  
  197. }
  198.  
  199. @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement