实现spring,并用junit测试了下。

原创
2017/03/02 16:32
阅读数 33

简答的实现了一下spring并用junit测试了一下,还是花了半天时间,晕 1.###通过maven 需要的spring jar 包依赖,其中这些包的作用就不一一介绍了

	<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-lang3</artifactId>
		    <version>3.3.2</version>
	</dependency>
	
	   <dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
	</dependency>
			<!-- Spring Context Jar -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${spring.version}</version>
	</dependency>

	<!-- Spring Test Jar junit测试用到的jar 包主要是这个-->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>
		<version>${spring-test.version}</version>
	</dependency>

		<!-- Spring Core Jar -->
	<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
	</dependency>
	
		<!-- Spring Beans Jar -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
		<!-- Spring Web Jar -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
		<!-- Spring WebMvc Jar -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
		<!-- Spring Expression -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
		<!-- Spring Context Support -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
	
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>${aspectj.version}</version>
	</dependency>
 	<dependency> 
 	  <groupId>log4j</groupId> 
 	  <artifactId>log4j</artifactId>  
 	  <version>1.2.16</version> 
 	</dependency>

 	<dependency>
 	      <groupId>junit</groupId>
 	      <artifactId>junit</artifactId>
 	      <version>4.4</version>
 	      <scope>test</scope>
 	 </dependency>
	<dependency>
	  <groupId>commons-logging</groupId>
	  <artifactId>commons-logging</artifactId> 
	  <version>1.1.3</version>
	</dependency>
<!--ognl 表达式用到的包-->
	<dependency>
		  <groupId>jstl</groupId>
		  <artifactId>jstl</artifactId>
		  <version>1.2</version>
	</dependency>

2.###aplicationContext.xml简单配置配置文件,主要验证是否扫描了 action 包下的userDetail实体类,验证是否成功

<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
        <context:component-scan base-package="action"/>
    </beans>
  1. 完成之后的结构图。。

输入图片说明

  1. 编写JunitTest测试类:


public class JunitTest {
	public void test(){
	
		ApplicationContext CTX2 = new ClassPathXmlApplicationContext(new String[]                            {"classpath*:spring-context.xml","classpath*:aplicationContext.xml"}, true);
		userDetail userVO = (userDetail)CTX2.getBean("userDetail");
		System.out.println(userVO.toString())
	}
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部