Spring 자동 주입 및 통합 테스트

5275 단어 DAOspringxmlJUnitjpa
새 프로젝트 는 Spring 2.5 의 @ Autowired 방식 을 사용 하여 설정 파일 을 크게 절약 하 였 습 니 다.다음은 해 보 겠 습 니 다.
Dao 층, 여 기 는 모든 getter 와 setter 방법 을 줄 였 습 니 다. 앞으로 이렇게 오래 끌 필요 가 없습니다.)

@Repository
public class UserDaoImpl extends GenericDaoJpa<User, Long> implements UserDao {

	public UserDaoImpl() {
		super(User.class);
	}
}

Service 층, 여 기 는 모든 getter 와 setter 방법 을 줄 였 습 니 다. 앞으로 이렇게 오래 끌 필요 가 없습니다.)

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private GroupDao groupDao;
}

XML 설정

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<!--        -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>${DB.DRIVERCLASSNAME}</value>
		</property>
		<property name="url">
			<value>${DB.URL}</value>
		</property>
		<property name="username">
			<value>${DB.USERNAME}</value>
		</property>
		<property name="password">
			<value>${DB.PASSWORD}</value>
		</property>
	</bean>

	<bean id="placeholderProperties"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<!--
				<value>classpath:config.properties</value>
				-->
				<value>file:${user.home}/config.properties
				</value>
			</list>
		</property>
		<property name="ignoreResourceNotFound" value="false" />
	</bean>

	<!-- JPA EntityManagerFactoryBean for EntityManager -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="true" />
				<property name="generateDdl" value="true" />
				<property name="databasePlatform" value="org.hibernate.dialect.DB2Dialect" />
			</bean>
		</property>
	</bean>

	<!-- Transaction manager for JPA -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory">
			<ref bean="entityManagerFactory" />
		</property>
	</bean>

	<tx:annotation-driven transaction-manager="transactionManager" />

	<!-- Spring Annotation -->
	<context:annotation-config />

	<!-- Spring auto scan base package -->
	<context:component-scan base-package="com.test" /> <--      ,        ,       -->

</beans>

이곳 의 spring 설정 이 얼마나 간단 한 지 보 세 요. 정말 많은 설정 을 줄 여 주 었 습 니 다.
그리고 우리 의 테스트 클래스 를 다시 봅 시다.

@RunWith(SpringJUnit4ClassRunner.class)   
@ContextConfiguration(locations = {"classpath:core-context.xml"})   
public class ChatTest extends TestCase {
	
	
	
	@Autowired
	private IUserService userService;
	
	@Test
	private void testSpring(){
		List<User> userList = userService.getAllUser();
		if(userList!=null){
			System.out.println(userList.size());
		}
	}

이렇게 하면 됩 니 다. 테스트 클래스 도 주입 할 수 있 고 context. getBean () 방식 으로 찾 지 않 아 도 됩 니 다. 얼마나 편리 합 니까?그러나 여기 서 주의해 야 할 점 은 JUnit test 가 낮은 버 전이 라면 4.6 으로 업그레이드 해 야 한 다 는 점 이다. 그리고 나의 4.8 은 안 되 고 나중에 나 는 4.6 으로 바 꾸 면 된다.

좋은 웹페이지 즐겨찾기