Srping 初体验及概念
-
Spring优势
简化代码
降低代码耦合 -
概念
- Ioc(控制反转Inversion of Control) 一种代码思想,代码管理代码
- 实现ioc思想 需要di(依赖注入Dependency Injection)做技术支持
好处:降低耦合
Spring使用方式
-
导包
基础包Beans context core expression
日志包 commons.logging包 -
配置xml文件(名称无要求 一般使用applicationContext.xml)(文件路径在工程目录下任意)
-
创建配置类
1. 创建容器对象(常用容器对象 BeanFactory 与 ApplicationContext )
2. 两个容器区别以及使用位置
- 加载时机(创建时机)
beanFactoy 通过XmlBeanFactory 创建
ApplicationContext 通过ClassPathXmlApplicationContext或者 FileSystemXmlApplicationContext创建
- 使用资源上
a) beanFactoy 只有在自己写好的类先被创建才能使用
b) applicationContext 一创建直接加载所有的类
- 使用地区
a) 一般用ApplicationContext 对象
b) beanFactory做手机端开发资源匮乏可能要用 -
容器对象通过XML配置文件加载自定义类
给bean 一个 属性 name 用于容器好调用它
给他一个calss 属性告诉 他字节码文件在哪调用我的类 -
对象的创建以及初始化
例:
public class Test {
private String name;
private int age;
private HashMap<String,String> hashMap;
private ArrayList<String> arrayList;
public Test() {
}
public Test(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("set 方式属性赋值name:"+name+" ----- <property name=\"name\" value="+name+"></property> ");
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
System.out.println("set 方式属性赋值age:"+age+" ----- <property name=\"age\" value="+age+"></property> ");
this.age = age;
}
public HashMap<String, String> getHashMap() {
return hashMap;
}
public void setHashMap(HashMap<String, String> hashMap) {
this.hashMap = hashMap;
}
public ArrayList<String> getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList<String> arrayList) {
this.arrayList = arrayList;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Test test = (Test) o;
return age == test.age && Objects.equals(name, test.name) && Objects.equals(hashMap, test.hashMap) && Objects.equals(arrayList, test.arrayList);
}
@Override
public int hashCode() {
return Objects.hash(name, age, hashMap, arrayList);
}
@Override
public String toString() {
return "Test{" +
"name='" + name + '\'' +
", age=" + age +
", hashMap=" + hashMap +
", arrayList=" + arrayList +
'}';
}
}
1. 构造方法
<!-- 无参构造方法创建对象 -->
<bean name="test" class="com.entity.Test" >
</bean>
<!-- 带参构造方法创建对象 需要类提供对应构造方法 -->
<bean name="test" class="com.entity.Test" >
<!-- name:对应属性名;
value : 设定属性值;
index : 属性在参数列表第几位
type : 属性类型 -->
<constructor-arg name="name" value="张三"
index="0" type="java.lang.String"></constructor-arg>
<constructor-arg name="age" value="15" index="1"
type="java.lang.Integer"></constructor-arg>
</bean>
2. SET方式
<!-- set 方式赋值 对象必须定义get set方法-->
<bean name="test" class="com.entity.Test" >
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
</bean>
3. 静态工厂方式
1. 首先创建工厂 ,调用静态方法产生
public class TestFactory {
public static Test createTest(){
return new Test();
}
}
<bean name="test2" class="com.entity.AcgFactory" factory-method="createTest">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
</bean>
4. 实例工厂方式
public class TestFactory {
public TestFactory {
}
public Test createTest(){
return new Test();
}
}
<bean name="testFactory " class="com.entity.TestFactory " >
</bean>
<bean name="test" factory-bean="testFactory " factory-method="createTest" >
</bean>
注意factory-method 属性写在 实例对象中而不是工厂中
给出工厂文件即可,不需要给出实例类的路径,把工厂的bean给实例类即可
对象出来了,需要初始加载什么东西吗?,能活多久?作用域?可以有多个象共存吗?
Scope属性 有四个默认值 scope属性都是默认的
singleton(默认 单例) 就只会创建出一个对象,指向一个地址
prototype(多例) 可以创建多的对象
request:web环境下 一般不用 与生命周期有关,跟request一样
session:web环境下 一般不用 与生命周期有关,跟session一样
init-method 属性
加载完类就会执行的初始化方法
Destroy-method 属性
对象销毁前执行,死前遗言