单元测式ssh 项目 跟spring注角功能的应用

第一步搭建ssh 项目 添加三个框架的支持;
第二步添加注解功能
在spring配制文件中加如下代码

	<!-- 启用注解 -->
	<context:annotation-config />
	<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
	<context:component-scan base-package="com.*" />


整个spring文件内容

<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-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/jee 
	http://www.springframework.org/schema/jee/spring-jee-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/context 
	http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byName" default-lazy-init="true">
	<!-- 启用注解 -->
	<context:annotation-config />
	<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
	<context:component-scan base-package="com.*" />
	<!-- 定义受环境影响易变的变量 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<!-- 标准配置 -->
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>

	<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->
		<property name="initialSize" value="5" />
		<property name="maxActive" value="100" />
		<property name="maxIdle" value="30" />
		<property name="maxWait" value="500" />
		<property name="defaultAutoCommit" value="false" />
	</bean>

	<!-- Hibernate配置 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
			</props>
		</property>
		<property name="packagesToScan" value="com.cpsp.pojo.*" />
	<!--  	<property name="mappingResources">
		<list>
		<value>resource/hibernate/CpwebUser.hbm.xml</value>
		</list>
		</property>
		-->
		<property name="mappingLocations" value="classpath:resource/hibernate/*.hbm.xml"/>
	</bean>

	<!-- 事务管理器配置,单数据源事务 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<!--
		<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true"/>
	-->

	<aop:config proxy-target-class="true">
		<aop:advisor pointcut="execution(* com.cpsp.service..*Service.*(..))"
			advice-ref="txAdvice" />
		<aop:advisor pointcut="execution(* com.cpsp.dao..*Dao.*(..))"
			advice-ref="txAdvice" />
	</aop:config>
	<tx:advice id="txAdvice">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="is*" read-only="true" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>


</beans>


dao层的实现类的类名上面加上代码如下
@Repository("起的别名service跟据该名字找到此类生成实例对像")

service层实现类的类名上面加上代码如下
@Service("别名action层据该名调用")

声明dao接口
 @Resource(name = "dao层的别名")
    private  dao层的接口  别名;

service 层据别名调用接口的方法
如:
 
 别名.dao接口方法


action层 类前面的声名
@Controller(value="actin别名")
@Scope(value="prototype")

action层的方法调用跟service层一样

单元测式
public class UserActionTest  {
    //@Resource(name = "userServiceImpl") //自动装配注入  
    public static UserService userService;
    
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("before class");
        ApplicationContext context=new ClassPathXmlApplicationContext("resource/spring/applicationContext.xml");
      userService=(UserService) context.getBean("userServiceImpl");

    }
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("after class");
    }
}



你可能感兴趣的:(spring)