Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring/root-context.xml</param-value>
- </context-param>
- <!-- Creates the Spring Container shared by all Servlets and Filters -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- Processes application requests -->
- <servlet>
- <servlet-name>controller</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring/controller.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>controller</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <?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:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
- <!-- DBCP DataSource -->
- <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost/spring" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </bean>
- <!-- JPA EntityManagerFactory -->
- <bean id="entityManagerFactory"
- class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="jpaVendorAdapter">
- <bean id="jpaVendorAdapter"
- class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
- <property name="showSql" value="true" />
- <property name="generateDdl" value="true" />
- <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
- </bean>
- </property>
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- Persistence Context Annotated -->
- <bean
- class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
- id="persistenceAnnotationBeanPostProcessor" />
- <!-- Transaction -->
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="entityManagerFactory" />
- <property name="dataSource" ref="dataSource" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- </beans>
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
- http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
- version="2.0">
- <persistence-unit name="persist" transaction-type="RESOURCE_LOCAL">
- <properties>
- <property name="hibernate.hbm2ddl.auto" value="create-drop" />
- <property name="hibernate.show_sql" value="true" />
- <property name="hibernate.transaction.flush_before_completion"
- value="true" />
- <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
- </properties>
- </persistence-unit>
- </persistence>
- @Entity
- public class Person {
- private int id;
- private String name;
- @Id
- @GeneratedValue
- public int getId() {
- return id;
- }
- // ..
- }
- @Repository
- @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
- public class PersonDaoImpl implements PersonDao {
- private EntityManager entityManager;
- @PersistenceContext
- public void setEntityManager(EntityManager entityManager) {
- this.entityManager = entityManager;
- }
- @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
- public Person persist(Person person) {
- entityManager.persist(person);
- return person;
- }
- // ...
- }
- @Service
- public class PersonServiceImpl implements PersonService {
- private PersonDao personDao;
- @Inject
- public void setPersonDao(PersonDao personDao) {
- this.personDao = personDao;
- }
- public Person create(Person person) {
- return personDao.persist(person);
- }
- // @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
- public Person save(Person person) {
- return personDao.merge(person);
- }
- // ...
- }
- @Controller
- @Scope("prototype")
- @RequestMapping("/person")
- public class PersonController {
- private PersonService personService;
- @Inject
- public void setPersonService(PersonService personService) {
- this.personService = personService;
- }
- @RequestMapping(value = "/add", method = RequestMethod.POST)
- public ModelAndView add(@RequestParam("username") String name) {
- Person person = new Person();
- person.setName(name);
- System.out.println(name); // i can get the value
- personService.create(person); // nothing happend... throws no Excp
- ModelAndView mav = new ModelAndView();
- mav.setViewName("result");
- mav.addObject("val", person.getName());
- return mav;
- }
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("/test-context.xml")
- public class PersonControllerTest {
- private PersonController controller;
- @Inject
- public void setController(PersonController controller) {
- this.controller = controller;
- }
- // the test passed no matter @Transactional or not, if not, the db get the row
- @Test(/*expected = NullPointerException.class*/)
- @Transactional
- public void testAdd() {
- // PersonController controller = new PersonController();
- assertEquals("test", controller.add("test").getModelMap().get("val"));
- }
- }
- @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement