一.spring是什么
1.Spring 为简化企业级应用开发而生. 使用Spring可以使简单的JavaBean实现以前只有EJB才能实现的功能
2.Spring 是一个 IOC(DI)(控制反转,依赖注入) 和 AOP(面向切面编程)容器框架
3.具体描述Spring:
轻量级:Spring是非侵入性的-基于Spring开发的应用中的对象可以不依赖于Spring的API
依赖注入(DI--- dependency injection、IOC)
面向切面编程(AOP--- aspect oriented programming)
容器:Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
框架:Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring中可以使用XML和 Java注解组合这些对象
一站式:在IOC和 AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了展现层的SpringMVC和持久层的 Spring JDBC)
二.搭建spring开发环境
创建本地maven项目,在pom.xml中加载架包
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- 加载spring的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
</project>
创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!-- 自动实例化类
id是名称必须是唯一的在 IOC 容器中必须是唯一的
若 id 没有指定,Spring 自动将限定性类名作为 Bean 的名字
class是类路径
-->
<bean id="b1" class="less01.autowire.A">
<property name="name" value="李四"></property>
</bean>
<bean id="c" class="less01.autowire.A">
<property name="name" value="张三"></property>
</bean>
<!--属性注入-->
<bean id="b" class="less01.autowire.B">
<property name="c" ref="b1"></property>
</bean>
<!-- 自动装配 将当前实列中所有的对象属性 全部进行填充
byType对象属性是所在的类在容器中只能拥有一个实列
多个实列是相对的 如果b中是个集合 可以装配多个A
byName根据当前对象属性的名称来查找对应的实列
-->
<bean id="a" class="less01.autowire.B" autowire="byType"></bean>
</beans>
简单的测试,创建几个测试类
package less01.autowire;
public class A {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package less01.autowire;
import java.util.List;
public class B {
private List<A> c;
public List<A> getC() {
return c;
}
public void setC(List<A> c) {
this.c = c;
}
}
package less01.autowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**FileStstemXmlApplicationContext 从文件系统寻找xml文件 带盘符
* ClassPathXmlApplicationContext 从类路径下寻找 常用 建议使用
*
* 就近原则 所有ApplicationContext指令都致辞字符串定义查找方式
* classpath:
* file:
*/
public class TestCotainer {
public static void main(String[] args) {
//创建容器
ApplicationContext context= new ClassPathXmlApplicationContext("classpath:/less01/autowire/spring.xml");
//获取容器实例
B b=(B)context.getBean("a");
System.out.println(b.getC().size());
//获取所有的bean的名字
String[] str=context.getBeanDefinitionNames();
context.close();
}
}
属性注入和构造器注入,配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<bean id="b1" class="less01.ioc.A">
<property name="name" value="李四"></property>
</bean>
<!-- 引用 ref -->
<bean id="b" class="less01.ioc.B">
<!-- value只适合基本数据类型 数字 字符 日期
ref表示对象的引用,property标签实际调用setName指定名字首字母大小的方法
-->
<property name="a" ref="b1"></property>
</bean>
<!-- 属性注入
<bean id="c" class="less01.ioc.C">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="王五"></constructor-arg>
<constructor-arg name="b" ref="b"></constructor-arg>
</bean>
-->
<!-- 构造器注入 -->
<bean id="c" class="less01.ioc.C">
<constructor-arg type="java.lang.String" value="1"></constructor-arg>
<constructor-arg type="java.lang.String" value="王五"></constructor-arg>
<constructor-arg type="less01.ioc.B" ref="b"></constructor-arg>
</bean>
</beans>
简单的测试
package less01.ioc;
public class A {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package less01.ioc;
public class B {
private A a;
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}
package less01.ioc;
public class C {
private String id;
private String name;
private B b;
private C(String id,String name,B b){
super();
this.id=id;
this.name=name;
this.b=b;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
测试
package less01.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCotainer {
public static void main(String[] args) {
//创建容器
ApplicationContext context= new ClassPathXmlApplicationContext("classpath:/less01/ioc/spring.xml");
//获取容器实例
A a=(A)context.getBean("b1");
System.out.println(a.getName());
B b=(B)context.getBean("b");
System.out.println(b.getA().getName());
C c=(C)context.getBean("c");
System.out.println(c.getName()+"---"+c.getId()+"---"+c.getB().getA().getName());
}
}