我的百度云盘:https://pan.baidu.com/s/14Jzl4ov0bkOP01wYsMSl2Q
Spring开发一般用的包+xsd-config.html:https://pan.baidu.com/s/1sbsK7SYVofJ2inE14128vA
也可以去官网下载
spring-framework-3.2.0.RELEASE-dist.zip ---Spring开发包
* docs :spring框架api和规范
* libs :spring开发的jar包
* schema :XML的约束文档.
spring-framework-3.0.2.RELEASE-dependencies.zip ---Spring开发中的依赖包
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
开发的日志记录的包:
com.springsource.org.apache.commons.logging-1.1.1.jar --- 用于整合其他的日志的包(类似Hibernate中slf4j)
com.springsource.org.apache.log4j-1.2.15.jar
在src下创建一个applicationContext.xml
引入XML的约束:
* 找到xsd-config.html.引入beans约束:
http://www.springframework.org/schema/beans/spring-beans.xsd">
id:装配Bean的名称
class:装配Bean所在的路径
public class Bean1 {
}
public class Bean2 {
}
public class Bean2Factory {
public static Bean2 getBean2(){
System.out.println("静态工厂获得实例化的方法....");
return new Bean2();
}
}
public class Bean3 {
}
public class Bean3Factory {
public Bean3 getBean3(){
System.out.println("实例工厂化实现Bean实例化....");
return new Bean3();
}
}
id遵守XML约束的id的约束.id约束保证这个属性的值是唯一的,而且必须以字母开始,可以使用字母、数字、连字符、下划线、句 话、冒号
name没有这些要求
如果bean标签上没有配置id,那么name可以作为id.
scope属性 :
* singleton :单例的.(默认的值.)
* prototype :多例的.
* request :web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();
* session :web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();
* globalSession :一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;
public class Customer {
}
//Customer测试 scope="singleton"
public void demo1(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Customer customer1 = (Customer) applicationContext.getBean("customer");
System.out.println(customer1);
Customer customer2 = (Customer) applicationContext.getBean("customer");
System.out.println(customer2);
applicationContext.close();
//cn.itcast.demo3.Customer@bdb19
//cn.itcast.demo3.Customer@bdb19
}
public class Customer {
}
//Customer测试scope="prototype"
public void demo2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Customer customer1 = (Customer) applicationContext.getBean("customer");
System.out.println(customer1);
Customer customer2 = (Customer) applicationContext.getBean("customer");
System.out.println(customer2);
//cn.itcast.demo3.Customer@6be70a
//cn.itcast.demo3.Customer@bdb19
}
配置Bean的初始化和销毁的方法:
配置初始化和销毁的方法:
* init-method=”setup”
* destroy-method=”teardown”
执行销毁的时候,必须手动关闭工厂,而且只对scope=”singleton”有效.
public class BeanImpl implements Bean{
private String name;
public void init(){
System.out.println("初始方法被调用...");
}
public void destroy(){
System.out.println("手动调用销毁方法...");
}
}
public void SpringTest{
@Test
public void demo1(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Bean bean = (Bean)applicationContext.getBean("bean");
applicationContext.close();
}
}
//结果
//初始方法被调用...
//手动调用销毁方法...
Bean的生命周期的11个步骤:
1.instantiate bean对象实例化
2.populate properties 封装属性
3.如果Bean实现BeanNameAware 执行 setBeanName
4.如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext
5.如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization
6.如果Bean实现InitializingBean 执行 afterPropertiesSet
7.调用
8.如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
9.执行业务处理
10.如果Bean实现 DisposableBean 执行 destroy
11.调用
public class Car {
private String nameString;
private Double moneyDouble;
public Car() {
super();
}
public Car(String nameString, Double moneyDouble) {
super();
this.nameString = nameString;
this.moneyDouble = moneyDouble;
}
}
1.注入普通属性
public class Car2 {
private String nameString;
private Double moneyDouble;
public void setNameString(String nameString) {
this.nameString = nameString;
}
public void setMoneyDouble(Double moneyDouble) {
this.moneyDouble = moneyDouble;
}
}
2.注入对象属性
public class Person {
private String nameString;
private Car2 car2;
public void setNameString(String nameString) {
this.nameString = nameString;
}
public void setCar2(Car2 car2) {
this.car2 = car2;
}
}
3.集合属性的注入
童童
小凤
杜宏
如花
root
123