Spring学习(一)
1.spring是什么
Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。Spring 是一个 IOC(DI) 和 AOP 容器框架. 用来管理对象。
七大模块:
2.spring的特点
轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API (侵入式:用户代码需要继承框架提供的类)
依赖注入(DI — dependency injection、IOC):当一个类对另一个类有依赖时,不再该类内部对依赖的类进行实例化,而是之前配置xml文件,告诉容器所依赖的类,在实例化该类时,容器自动注入一个所依赖的类的实例。
面向切面编程(AOP — aspect oriented programming) :
在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程。一般而言,我们管切入到指定类指定方法的代码片段称为切面,而切入到哪些类、哪些方法则叫切入点。(类似于拦截器,在特定位置执行特定方法后继续运行)
容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)
3.spring的下载
依赖的jar包:
1.官网:http://projects.spring.io/spring-framework/
2.maven: http://maven.aliyun.com/nexus/#welcome/(搜索spring-context)
3. 百度“软件镜像库”,每个大学基本都有(北京理工大学开源软件镜像服务mirror.bit.edu.cn/web/)
Spring.xml:## 标题 ##
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd">
</beans>
4.spring对象的创建
实体类与接口:
接口:
public interface IPerson {
public void say();
public void eat();
}
实体类:
public class ChinesePerson implements IPerson {
public void say() {
System.out.println("中国人能说话");
}
public void eat() {
System.out.println("中国人能吃饭");
}
}
1.普通new方法创建对象
public class New {
public static void main(String[] args) {
//如果new了很多个对象,如果类名改变,那么改起来很麻烦,换句话说就是代码耦合性太高
IPerson c = new ChinesePerson();
}
}
2.工厂模式创建
public class Factory {
public static IPerson getObject(){
//相当于将new创建对象封装成一个方法,创建时调用方法即可,更改类名时更改方法,降低了耦合
return new ChinesePerson();
}
}
3.容器创建(spring)
Spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd">
<!-- class指定要实例化的类的路径,id是变量名 -->
<bean id="c" class="spring.ChinesePerson">
</bean>
</beans>
Java:
public class Container {
/**
* 获取容器的方法
* FileSystemXmlApplicationContext 从文件系统寻找xml文件
* ClassPathXmlApplicationContext 从类路径下寻找(常用建议使用)
* ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]
* GenericXmlApplicationContext 通用默认查找类路径
* WebApplicationContext web.xml中实例化
* 前缀:classpath(服务器路径优先) file(本地路径优先,带盘符)
* */
public static void main(String[] args) {
//获取容器对象,指定xml
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//根据id取对象,如果没有id可以用类名.class或者类的群路径,但只适合有一个对象时,不推荐使用
ChinesePerson c =(ChinesePerson)context.getBean("c");
//遍历所有对象名,如果没有id会用“#数字(从0顺序开始)”命名
String [] str = context.getBeanDefinitionNames();
for (String string : str) {
System.out.println(string);
}
}
}
5.注入
A、B、C类代码:
package spring;
public class A {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package spring;
public class B {
private A a;
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}
package spring;
public class C {
private String name;
private B b;
public C(String name, B b) {
this.name = name;
this.b = b;
}
public String getName() {
return name;
}
public B getB() {
return b;
}
}
Spring.xml:
<bean id="a1" class="spring.A">
<!-- 属性注入,name是属性名,value是要赋值的值(基本类型),被实例化的类不能有构造方法 -->
<property name="name" value="zs"></property>
</bean>
<bean id="b" class="spring.B">
<!-- 属性注入,name是属性名,ref是引用的对象,被实例化的类不能有构造方法-->
<property name="A" ref="a1"></property>
</bean>
<!-- 构造器注入1 -->
<bean id="c1" class="spring.C">
<!--索引赋值,index从参数的索引(从0开始),value要赋的值 ,ref是引用的对象-->
<constructor-arg index="0" value="ls"></constructor-arg>
<constructor-arg index="1" ref="b"></constructor-arg>
</bean>
<!-- 构造器注入2 -->
<bean id="c2" class="spring.C">
<!--形参赋值,name是形参名,value要赋的值 ,ref是引用的对象(建议使用)-->
<constructor-arg name="name" value="ww"></constructor-arg>
<constructor-arg name="b" ref="b"></constructor-arg>
</bean>
<!-- 构造器注入3 -->
<bean id="c3" class="spring.C">
<!--形参赋值,type是类型的地址,value要赋的值 ,ref是引用的对象-->
<constructor-arg type="java.lang.String" value="sl"></constructor-arg>
<constructor-arg type="spring.B" ref="b"></constructor-arg>
</bean>
Java代码:
A a = (A)context.getBean("a1");
System.out.println(a.getName());
B b = (B)context.getBean("b");
System.out.println(b.getA().getName());
C c1 = (C)context.getBean("c1");
System.out.println(c1.getB().getA().getName());
C c2 = (C)context.getBean("c2");
System.out.println(c2.getB().getA().getName());
C c3 = (C)context.getBean("c3");
System.out.println(c3.getB().getA().getName());
- XML 配置里的 Bean 自动装配
Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 的 autowire 属性里指定自动装配的模式
byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.
byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同.
constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐使用
spring.xml:
<!-- 自动装配 (根据类型byType),如果需要的类型有多个,那么spring将不知道是哪个对象,会报错,但是如果装载类的属性是一个集合,那么它就可以加载多个实例,不会报错-->
<bean id="b1" class="spring.B" autowire="byType">
</bean>
<!-- 自动装配 (根据类型"byName"),根据对象属性名称寻找,可以有多个实例-->
<bean id="b2" class="spring.B" autowire="byName">
</bean>
Java代码:
B b1 = (B)context.getBean("b1");
System.out.println(b1.getA().getName());
B b2 = (B)context.getBean("b2");
System.out.println(b2.getA().getName());
- Bean 的作用域
在 Spring 中, 可以在 元素的 scope 属性里设置 Bean 的作用域.
默认情况下, Spring 只为每个在 IOC 容器里声明的 Bean 创建唯一一个实例, 整个 IOC 容器范围内都能共享该实例:所有后续的 getBean() 调用和 Bean 引用都将返回这个唯一的 Bean 实例.该作用域被称为 singleton, 它是所有 Bean 的默认作用域.
Spring中bean默认都单实例(singleton),容器初始化时实例化对象,prototype多实例,调用getBean()方法时实例化对象。