(二)在一的基础上集成spring 4.0.3 并整合JPA以及spring的单元测试

1)加上sping的依赖包,并在base模块进行引用。


<!-- spring包依赖-->
		<dependency>
		  <groupId>org.springframework</groupId>
		  <artifactId>spring-core</artifactId>
		  <version>${spring.version}</version>
		</dependency>
		<dependency>
		  <groupId>org.springframework</groupId>
		  <artifactId>spring-context</artifactId>
		  <version>${spring.version}</version>
		</dependency>
		<dependency>
		  <groupId>org.springframework</groupId>
		  <artifactId>spring-beans</artifactId>
		   <version>${spring.version}</version>
		</dependency>
			<dependency>
		  <groupId>org.springframework</groupId>
		  <artifactId>spring-test</artifactId>
		   <version>${spring.version}</version>
		</dependency>
		<dependency>
		  <groupId>org.springframework</groupId>
		  <artifactId>spring-web</artifactId>
		  <version>${spring.version}</version>
		</dependency>
		<dependency>
		  <groupId>org.springframework</groupId>
		  <artifactId>spring-aop</artifactId>
		   <version>${spring.version}</version>
		</dependency>
		<dependency>
		  <groupId>org.springframework</groupId>
		  <artifactId>spring-orm</artifactId>
		  <version>${spring.version}</version>
		</dependency>

2)创建spring_base.xml,在spring配置文件中整合jpa


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-3.0.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
		                    http://www.springframework.org/schema/tx 
		                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		                    http://www.springframework.org/schema/aop 
		                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">       
	<!-- 1 
	    1)开启注解自动配置组件的自动扫描 (@Component是所有受Spring管理组件的通用形式)
	      @Repository、@Service和 @Controller则是@Component的细化,
	      用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)
	    2)把use-default-filters属性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class
	      不予註冊為bean,由filter子標籤代勞。
	      Use-dafault-filters=”false”的情况下: <context:exclude-filter>指定的不扫描,
	                                            <context:include-filter>指定的扫描
	 -->
	<context:component-scan base-package="org.smd" use-default-filters="true">
	    <!-- 只扫描Repository标注的类 -->
     	<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
	    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> -->
	</context:component-scan>
	<!--2配置数据源 -->
  	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 配置连接池的初始值 -->
		<property name="initialSize" value="${initialSize}" />
		<!-- 连接池的最大值 -->
		<!-- <property name="maxActive" value="500"/> -->
		<!-- 最大空闲时,当经过一个高峰之后,连接池可以将一些用不到的连接释放,一直减少到maxIdle为止 -->
		<!-- <property name="maxIdle" value="2"/> -->
		<!-- 当最小空闲时,当连接少于minIdle时会自动去申请一些连接 -->
		<property name="minIdle" value="${minIdle}" />
		<property name="maxActive" value="${maxActive}" />
		<property name="maxIdle" value="${maxIdle}" />
		<property name="maxWait" value="${maxWait}" />
	</bean>
	
	<context:property-placeholder location="classpath:jdbc.properties" />

   <!--3 Spring整合jpa -->
   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
			<property name="loadTimeWeaver">
	          <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
	       </property>
	</bean>
  <!--4 事务管理器 -->
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
   </bean>
  <!--5 采用注释的方式管理事务-->
  <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>


3)在base模块创建 dao包,service等包,因为现在是集成阶段,具体搭建,等整合结束,再接着搭建


(二)在一的基础上集成spring 4.0.3 并整合JPA以及spring的单元测试_第1张图片

4)创建操作POJO类 的  productType的服务类ProductServiceImpl


public interface IProductService extends ICommonService {
	
	

	/*****
	 * 保存productType
	 * @param productType
	 */
	public abstract void saveProductType(ProductType productType);

}


@Service
@Transactional
public class ProductServiceImpl implements IProductService {
	

	@PersistenceContext
	EntityManager  entityManager;
	
	/* (non-Javadoc)
	 * @see org.smd.base.service.IProductService#saveProductType(org.smd.base.model.ProductType)
	 */
	public void saveProductType(ProductType productType){
		
		entityManager.persist(productType);
	}
}



5)创建spring单元测试
import org.junit.Test;
import org.junit.runner.RunWith;
import org.smd.base.model.ProductType;
import org.smd.base.service.IProductService;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring_base.xml")
public class ProductTypeSpringTest {
	
	@Inject
	IProductService productService;
	
	@Test
	public void Test01(){
		
		ProductType productType=new ProductType();
		productType.setName("aa");
		productService.saveProductType(productType);
	}

}

运行单元测试,spring整合jpa,成功

(二)在一的基础上集成spring 4.0.3 并整合JPA以及spring的单元测试_第2张图片

(二)在一的基础上集成spring 4.0.3 并整合JPA以及spring的单元测试_第3张图片



你可能感兴趣的:((二)在一的基础上集成spring 4.0.3 并整合JPA以及spring的单元测试)