引入 jar包
配置文件 applicationContext.xml (主配置文件中,引入了子配置文件,不影响)
Bean对象 Family ,User
测试Demo SpringDemo
可能产生的异常
spring | org.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