spring一
首先Spring和MyBaits一样是一个框架,Java不死的原因是因为Java有Spring这个非常强大的技术框架的支持,而且他是一个轻量级的Java开发框架
那么是谁创建了Spring呢?
Spring之父——Rod JohnSon

Spring的官网是:Spring.io(有需要的可以去上面看看)
Spring又有那些特征呢?(在这里简单的介绍了一下)

1.什么是IOC?
IOC的全称是Inverse Of Control ,是控制反转的意思,他是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,也是轻量级的Spring框架的核心
那么如何理解这句话呢?
第一种理解:将组建对象的控制权从代码本身转移到外部容器
第二种理解:将创建对象实力的控制权从代码控制剥离到IOC容器控制
2.Spring 的第一个案例
编写一个Spring的基本步骤:
(1)首先要引入jar包

引入下面这两个jar包后其他两个自动会有
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
(2)创建一个类,我在这叫HappyService
package demo01;
import java.util.StringTokenizer;
/**
* Created by mycom on 2018/3/3.
*/
public class HappyService {
private String info;
private Integer age;
public HappyService(){
System.out.println("======HappyService");
}
public void work(){
System.out.println("我是"+info);
}
public String getInfo() {
return info;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void setInfo(String info) {
this.info = info;
}
}
(3)在resource下配置一个ApplicationContext.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.xsd">
<bean id="service" class="demo01.HappyService">
<property name="info" value="第一个Spring项目"></property>
<property name="age" value="10"></property>
</bean>
</beans>
(4)编写测试类
import demo01.HappyService;
import demo03.printer.Printer;
import demo03.printer.Printer;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by mycom on 2018/3/3.
*/
public class Test20180303 {
@Test
public void t1(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HappyService service =(HappyService) context.getBean("service");
service.work();
}
}
测试的结果如下

这是IOC的简介,下面再说一下DI
3.什么是DI
DI的全称是 Dependency Injection 是依赖注入,主要是给对象属性输入属性值
那么他和IOC的关系是什么呢?
他们是Spring核心思想不同方面的描述,站在不同的维度对同一个概念或者事物的解释
下面用一个例子来解释(打印机案例)
一台打印机要先打印出东西首先要有 墨盒 纸张 还有一个打印机
那么首先我们要先编写一个墨盒接口,在墨盒的包下

package demo03.ink;
/**
* Created by mycom on 2018/3/3.
*/
public interface Ink {
public String getInk();
}
写完接口,要有实现类继承他,在写两个类,彩色墨盒和黑白墨盒
package demo03.ink;
/**
* Created by mycom on 2018/3/3.
*/
public class BlackInk implements Ink {
public String getInk() {
return "黑白墨盒";
}
}
package demo03.ink;
/**
* Created by mycom on 2018/3/3.
*/
public class ColorInk implements Ink {
public String getInk() {
return "彩色墨盒";
}
}
纸张接口
package demo03.paper;
/**
* Created by mycom on 2018/3/3.
*/
public interface Paper {
public String getPaper();
}
他的实现类
package demo03.paper;
/**
* Created by mycom on 2018/3/3.
*/
public class B5Paper implements Paper {
public String getPaper() {
return "B5";
}
}
package demo03.paper;
/**
* Created by mycom on 2018/3/3.
*/
public class A4Paper implements Paper {
public String getPaper() {
return "A4";
}
}
下面是打印机类,不是接口了
package demo03.printer;
import demo03.ink.Ink;
import demo03.paper.Paper;
/**
* Created by mycom on 2018/3/3.
*/
public class Printer {
private Ink ink;
private Paper paper;
public Ink getInk() {
return ink;
}
public void setInk(Ink ink) {
this.ink = ink;
}
public void printer(){
System.out.println("我在一张"+ink.getInk()+"颜色的"+paper.getPaper()+"纸上写东西了");
}
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
}
之后要在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.xsd">
<!--现在Spring中定义一个Ink-->
<bean id="ink" class="demo03.ink.ColorInk"></bean>
<!--在Spring中定义一个Paper-->
<bean id="paper" class="demo03.paper.A4Paper"></bean>
<!--在Spring中定义一个Printer-->
<bean id="printer" class="demo03.printer.Printer">
<property name="ink" ref="ink"></property>
<property name="paper" ref="paper"></property>
</bean>
</beans>
最后编写测试类,进行单侧
import demo01.HappyService;
import demo03.printer.Printer;
import demo03.printer.Printer;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by mycom on 2018/3/3.
*/
public class Test20180303 {
@Test
public void t1(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextPrinter.xml");
Printer printer =(Printer) context.getBean("printer");
printer.printer();
}
}

浙公网安备 33010602011771号