Spring 자동 주입 및 통합 테스트
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 으로 바 꾸 면 된다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Spring에서 DAO가 순환 호출될 때 데이터가 실시간으로 업데이트되지 않는 해결 방법문제를 설명하기 전에 몇 가지 전제 사항을 설명하십시오. Spring의 구성 파일에서 다음과 같은 방식으로 데이터베이스 트랜잭션을 구성했다고 가정하십시오. 현재 UserDao 및 Security Service가 있습...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.