1、IntelliJ IDEA搭建Spring环境,实现helloworld前端
2、spring简介java
3、配置形式web
4、bean的配置方式spring
5、IOC和DI编程
6、在Spring的IOC容器里配置bean设计模式
7、关于ApplicationContext的介绍网络
8、依赖注入的方式session
9、Spring容器app
1、IntelliJ IDEA搭建Spring环境,实现helloworld框架
一、建立project
二、勾选Spring而后next
三、设置你项目所想要存放的路径以及名字
四、这时候IntelliJ IDEA就会自动下载Spring所须要的jars,只须要等待就好。
五、下载好后,Spring的jars和配置文件都准备好了。
六、Spring实现hello world
普通的方法:
接着咱们运行一下这个程序,成功输出了Hello Spring。
接下来咱们就要使用Spring了,首先在Spring的配置文件中加入以下内容。
<?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="helloWorld" class="HelloWorld"> <property name="name" value="Spring"></property> </bean> </beans>
这时候咱们就配置好了HelloWorld Bean的信息,咱们再调用sayHello()方法的时候就不向以前同样了,也须要3个步骤。
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloWorld { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void sayHello(){ System.out.println("hello "+name); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); helloWorld.sayHello(); } }
这么写好像和本身以前编程的时候不同啊,能够运行结果吗,咱们直接试一下就好。
好像不太同样啊,输出了咱们想要的Hello Spring ,可是好像多了许多其余的东西啊。这些实际上是Spring输出的日志而已。
第一次使用Spring,咱们明明没有建立HelloWorld的实例对象,只是配置了下Spring的配置文件,怎么就能得出正确的结果呢,这是由于咱们使用了Spring的IOC功能,把对象的建立和管理的功能都交给了Spring去管理,咱们须要对象的时候再和Spring去要就行。
那么何时new的对象呢?
我也不知道,哈哈,因此首先修改一下HelloWorld类的构造方法和setter方法。
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloWorld { private String name; public HelloWorld() { System.out.println("this is helloworld constructor"); } public String getName() { return name; } public void setName(String name) { System.out.println("this is helloworld setName()"); this.name = name; } public void sayHello(){ System.out.println("hello "+name); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); helloWorld.sayHello(); } }
而后直接添加断点进入Debug模式。
咱们能够看到,当执行到第一步建立IOC容器对象的时候就调用了HelloWorld类的构造方法和setter方法。
从上面的例子能够看出:
旧方法:
一、建立helloworld实例
二、设置实例对象的name属性
三、调用sayhello方法
spring:
一、建立spring的IOC容器
二、从IOC容器中获取Bean对象
三、调用sayhello方法
而后咱们研究了spring是啥时候new对象的,能够看出,spring帮咱们完成了前两步,也就是建立实例对象和设置对象属性,也就是说咱们能够把对象的建立和管理工做交给spring去完成,只要写好spring的配置文件便可。
2、spring简介
一、spring是一个开源框架
二、spring是为了简化企业级应用开发而生
三、javabean实现EJB的功能
四、spring是Java一站式框架
spring优势
一、方便解耦,简单开发
二、支持AOP编程
三、支持声明事务
四、方便程序测试
五、方便框架集合
六、下降开发难度
3、配置形式
一、基于xml文件的方式
二、基于注解的方式
4、bean的配置方式
一、经过全类名(反射)
二、经过工厂方法(静态工厂方法&实例工厂方法)
三、FactoryBean
5、IOC和DI
IOC(inversion of control):其思想是反转资源获取的方式。传统的资源查找方式要求组件向容器发起请求查找资源,做为回应,容器适时的返回资源。而应用了IOC以后,容器主动将资源推送给它所管理的组件,组件要作的仅是选择一种合适的方式接收资源。这种行为也被称为查找的被动形式。
DI(Dependency Injection):IOC的另外一种表达方式:即组件以一些预先定义好的方式(例如:setter方法)接收来自如容器的资源注入。相对于IOC而言,这种表述更为直接。
6、在Spring的IOC容器里配置bean
只有springIOC容器自己实例化后,才能从IOC容器里获取bean实例并使用。
spring提供了两种类型的IOC容器实现:
一、beanFactory:是spring框架的基础设施,面向spring自己
二、ApplicationContext:面向使用spring框架的开发者,几乎全部的应用场合都直接使用ApplicationContext而非底层的beanFactory,提供了更多的高级特性,ApplicationContext是beanFactory的子接口。
7、关于ApplicationContext的介绍
ApplicationContext的主要实现类:
一、ClassPathXmlApplicationContext:从类路径下加载配置文件
二、FileSystemXmlApplicationContext:从文件系统中加载配置文件
子接口ConfigurableApplicationContext 的做用:扩展于ApplicationContext,新增长两个主要方法:refresh()和close(),让ApplicationContext具备启动、刷新和关闭上下文的能力。
ApplicationContext在初始化上下文时就实例化全部单例的bean。
WebApplicationContext是专门为web应用而准备的,它容许从相对于web根目录的路径汇总完成初始化工做。
8、依赖注入的方式
spring支持3种依赖注入的方式
一、属性注入
属性注入即经过setter方法注入bean的属性值或依赖的对象
属性注入使用元素,使用name属性指定bean的属性名称,value属性或子节点属性值
属性注入是实际开发中最多见的注入方式
public void setName(String name) { System.out.println("setName:"+name); this.name=name; }
<bean id="helloWorld" class="spring.bean.HelloWorld"> <property name="name" value="Spring"></property> </bean>
二、构造器注入
经过构造方法注入bean的属性值或者依赖的对象(引用),保证了bean实例在实例化后就可使用
构造器注入在元素里声明属性,没有name属性
建立一个People对象
package com.container; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class People { private String name; private String sex; private int age; public People(String name, int age) { this.name = name; this.age = age; } public People(String name, String sex) { this.name = name; this.sex = sex; } @Override public String toString() { return "people{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml"); People people1 = (com.container.People) applicationContext.getBean("people1"); System.out.println(people1); People people2 = (com.container.People) applicationContext.getBean("people2"); System.out.println(people2); } }
<?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="people1" class="com.container.People"> <constructor-arg value="江疏影" type="java.lang.String"></constructor-arg> <constructor-arg value="20" type="int"></constructor-arg> </bean> <bean id="people2" class="com.container.People"> <constructor-arg value="江疏影" type="java.lang.String"></constructor-arg> <constructor-arg value="man" type="java.lang.String"></constructor-arg> </bean> </beans>
三、工厂方法注入(知道就行,不推荐)
9、spring容器
Spring容器,顾名思义是用来容纳东西的,装的就是Bean。Spring容器负责建立、配置、管理Bean。spring容器有两个核心接口:BeanFactory和ApplicationContext接口,后者是前者的子接口。在基于spring的Java EE程序中,全部的组件都被当成Bean来处理,包括数据源对象、hibernate的sessionFactory、事务管理等,程序中的全部Java类均可以被当成spring容器中的bean。
一、spring容器
spring容器的核心接口是BeanFactory,它有一个子接口就是ApplicationContext。ApplicationContext也被称为spring上下文。
调用者只须要使用getBean()方法便可得到指定bean的引用。对于大部分的Java程序而言,使用ApplicationContext做为spring容易更为方便。其经常使用的实现类有FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和AnnotationConfigXmlApplicationContext。若是Java web中使用spring容器,则一般有XmlWebApplicationContext、AnnotationConfigWebApplicationContext两个容器。
建立spring容器的实例时,必须提供spring容器管理的bean的配置文件,也就是咱们常说的spring.xml配置文件。所以在建立beanFactory时配置文件做为参数传入。xml配置文件通常以resource对象传入。resource是spring提供的资源访问接口,经过该接口spring更简单、透明的访问磁盘,网络系统和类路径上的相关资源。
对于独立的Java EE应用程序,能够经过以下方法来实例化BeanFactory。
//在当前项目类路径下搜索配置文件 ApplicationContext appContext = new ClassPathXmlApplicationContext("beans_7_3_3.xml"); //在文件系统搜索配置文件 appContext = new FileSystemXmlApplicationContext("D:\\spring-tool-workspace\\myspring\\src\\beans_7_3_3.xml"); //获取chinese的Bean,而且返回的类型为Chinese Person chinese = appContext.getBean("chinese", Chinese.class); chinese.useAxe();
二、使用ApplicationContext
大部分时间,都不会使用beanFactory实例做为spring容器,而是使用ApplicationContext做为spring容器,所以spring容器也被称为spring上下文。ApplicationContext加强了beanFactory的功能,提供了不少有用、方便开发的功能。
在web中能够利用如contextLoader的支持类,在web应用启动的时候自动建立ApplicationContext。
除了提供beanFactory所支持的所有功能外,application还额外的提供以下功能:
① ApplicationContext会默认初始化全部的singleton bean(单例bean),也能够经过配置取消。
② ApplicationContext继承了messageSource接口,所以提供国际化支持。
③ 资源访问,好比URL和文件。
④ 事件机制。
⑤ 同时加载多个配置文件。
⑥ 以声明式方式启动并建立spring容器。
ApplicationContext包括beanFactory的全部功能,并提供了一些额外的功能,优先使用ApplicationContext。对于在内存消耗的才使用beanFactory。
当系统建立ApplicationContext容器时,会默认初始化singleton bean,包括调用构造器建立该bean的实例,经过元素驱动spring调用setting方法注入所依赖的对象。这就意味着,系统前期建立ApplicationContext会有很大的开销,可是一旦初始化完成后面获取bean实例就会拥有较好的性能。为了阻止在使用ApplicationContext做为spring容器初始化singleton bean能够在元素添加lazy-init="true"属性。
三、ApplicationContext的国际化支持
ApplicationContext接口继承了MessageSource接口,所以具有国际化功能。
//MessageSource接口提供的国际化的两个方法 String getMessage(String code, Object [] args, Locale loc){ } String getMessage(String code, Object[]args, String default, Locale loc){ }
spring国际化的支持,实际上是创建在Java国际化的基础上的。其核心思路将程序中须要国际化的消息写入资源文件,而代码中仅仅使用国际化信息响应的key。
四、ApplicationContext的事件机制
ApplicationContext的事件机制是观察者设计模式的实现。经过ApplicationEvent和ApplicationListener接口实现,前者是被观察者,后者是观察者。
spring事件框架有两个核心的接口:
ApplicationEvent(事件):必须由ApplicationContext来发布。
ApplicationListener(监听器):实现了此接口就能够担任容器中的监听器bean。
实际上,spring的事件机制是由事件(实现ApplicationEvent接口的类)、事件源(也就是spring容器,而且有Java代码显示的触发)、监听器(ApplicationListener接口实现类)。这就像咱们在页面点击一个button。button是事件源,单机的这个动做就是事件,处理函数就是监听器。
如下代码演示spring事件机制:
import org.springframework.context.ApplicationEvent; public class EmailEvent extends ApplicationEvent{ private String address; private String text; public EmailEvent(Object source) { super(source); } public EmailEvent(Object source, String address, String text) { super(source); this.address = address; this.text = text; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.support.ClassPathXmlApplicationContext; public class EmailNotifier implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { //处理email事件 if(event instanceof EmailEvent){ EmailEvent email = (EmailEvent) event; System.out.println(email.getAddress()+" "+email.getText()); }else { //输出spring容器的内置事件 System.out.println("其它事件:"+event); } } public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans_7_4_4.xml"); EmailEvent emailEvent = applicationContext.getBean("emailEvent",EmailEvent.class); applicationContext.publishEvent(emailEvent); } }
<?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 class="EmailNotifier"></bean> <bean id="emailEvent" class="EmailEvent"> <constructor-arg value="test"></constructor-arg> <constructor-arg value="123@qq.com"></constructor-arg> <constructor-arg value="this is a test"></constructor-arg> </bean> </beans>
从上面的代码能够看出,事件监听器不只监听到了咱们程序显示触发的事件,还监听了spring容器内置的事件。若是实际开发须要,咱们能够在spring容器初始化或销毁时回调自定义方法,就能够经过上面的事件监听机制来完成。
spring提供了以下几个内置对象:
ContextRefreshedEvent、ContextStartedEvent、ContextClosedEvent、ContextStoppedEvent、RequestHandledEvent。
五、让bean获取spring容器
上面都是经过ApplicationContext建立spring容器,再调用spring容器的getBean()方法获取bean。这种状况下,程序老是持有spring容器的引用。可是在web应用中,咱们能够用声明式的方法来建立spring容器:在web.xml文件中配置一个监听,让这个监听类帮咱们来建立spring容器,前端MVC框架直接调用bean,使用依赖注入功能,无需访问spring容器自己。
在某些特殊状况下,bean须要实现某个功能(好比:bean须要输出国际化信息,或向spring容器发布事件),这些功能都须要借助spring容器来完成。就是说咱们须要将spring容器做为一个bean来注入到其它bean中,只不过spring容器bean是一个容器级别的bean。
为了让bean获取它所在容器的引用,可让bean实现beanFactoryAware接口。该接口只有一个方法setBeanFactory(BeanFactory beanFactory)方法,方法的beanFactory参数指向spring容器,会由spring容器注入。咱们bean中定义一个setter方法后,一般都是由在配置文件中配置元素来驱动spring容器来注入依赖bean的,可是这里咱们并无这样作,这是由于一个bean若是实现了beanFactory接口,spring在建立该bean时,会自动注入spring容器自己。与beanFactoryAware接口相似的还有BeanNameAware、ResourceLoaderAware接口,这些接口都会提供相似的setter方法,这些方法会由spring容器来注入。