独立测试spring框架依赖使用

引入 jar包

配置文件 applicationContext.xml (主配置文件中,引入了子配置文件,不影响)

Bean对象  Family ,User

测试Demo  SpringDemo

可能产生的异常

 

springorg.springframework.beans-3.0.5.RELEASE.jar必要
 org.springframework.core-3.0.5.RELEASE.jar必要
 org.springframework.aop-3.0.5.RELEASE.jar不必要
 org.springframework.context-3.0.5.RELEASE.jar必要
 org.springframework.transaction-3.0.5.RELEASE.jar不必要
 org.springframework.test-3.0.5.RELEASE.jar不必要
 org.springframework.expression-3.0.5.RELEASE.jar必要
 org.springframework.orm-3.0.5.RELEASE.jar不必要
 org.springframework.jdbc-3.0.5.RELEASE.jar不必要
 org.springframework.context.support-3.0.5.RELEASE.jar 
 org.springframework.asm-3.0.5.RELEASE.jar必要

                        aopalliance.jar (与hibernate结合时加入)

 

applicationContext配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context">
    
    <import resource="/spring_xml/applicationContext_default.xml"/>

		
</beans>

 

 

 

子配置spring配置文件 spring_default.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/data/jpa 
	http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
	"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context">
    
    <context:component-scan base-package="com.web.wp"/>
		
	<bean id="user"  class="com.web.wp.domain.User"/>
		
		
</beans>

 

 

Bean对象 

 

import org.springframework.stereotype.Component;

/**
 * 家庭Bean原型
 * */

@Component
public class Family {
	
	private Long id;
	private String address;
	private int number;
	/**用string对象替换list*/
	private String userids;

 

 

import org.springframework.stereotype.Component;

/**
 * 家庭Bean原型
 * */

@Component
public class Family {
	
	private Long id;
	private String address;
	private int number;
	/**用string对象替换list*/
	private String userids;

 

测试SpringDemo

package com.web.wp.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.web.wp.domain.Family;
import com.web.wp.domain.User;

/**
 * 独立测试spring框架
 * */
public class SpringDemo {
	/**
	 * 获取applicationCOntext
	 * 分辨 ClassPathXmlApplicationContext和FileSystemXmlApplicationContex区别
	 * classpath:前缀是不需要的,默认就是指项目的classpath路径下面;
	 * 默认表示的是两种:
	 * 1.没有盘符的是项目工作路径,即项目的根目录;
	 * 2.有盘符表示的是文件绝对路径.
	 * */
	
	
	public static void main(String[] args) {
		ApplicationContext ac = new  ClassPathXmlApplicationContext("applicationContext.xml");
		System.out.println(ac!=null);
		Family fa=ac.getBean(Family.class);
		System.out.println(fa!=null);
		User us=ac.getBean(User.class);
		System.out.println(us!=null);
	}
}

 

可能产生的异常

 Caused by: java.lang.ClassNotFoundException: org.springframework.expression.PropertyAccessor
结果 缺少引入 org.springframework.expression-3.0.5.RELEASE.jar 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值