目录
该篇随笔,主要用于记录Spring Framework 基础知识。由于笔者是初学者,见识与能力有限,难免出现错误,如果发现错误,望不吝赐教。
以下列出了使用Spring Framework的一些巨大好处
Spring使开发人员能够使用POJO开发企业级应用程序。仅使用POJO的好处是您不需要EJB容器产品(如应用程序服务器),但您可以选择仅使用强大的servlet容器(如Tomcat)或某些商业产品。
Spring采用模块化方式组织。即使包和类的数量很大,你也只需要担心你需要的那些而忽略其余的。
Spring并没有重新发明轮子,而是真正利用了一些现有技术,如几个ORM框架,日志框架,JEE,Quartz和JDK计时器以及其他视图技术。
测试用Spring编写的应用程序很简单,因为依赖于环境的代码被移动到这个框架中。此外,通过使用JavaBeanstyle POJO,使用依赖注入来注入测试数据变得更加容易。
Spring的Web框架是一个设计良好的Web MVC框架,它提供了一个很好的替代Web框架,如Struts或其他过度设计或不太流行的Web框架。
Spring提供了一个方便的API,用于将特定于技术的异常(例如,JDBC,Hibernate或JDO抛出)转换为一致的,未经检查的异常。
轻量级IoC容器往往是轻量级的,尤其是与EJB容器相比时。这有利于在具有有限内存和CPU资源的计算机上开发和部署应用程序。
Spring提供了一致的事务管理接口,可以缩小到本地事务(例如,使用单个数据库)并扩展到全局事务(例如,使用JTA)。
SpEL模块提供了一种强大的表达式语言,用于在运行时查询和操作对象图。
1.使用IDEA创建Spring项目 2.创建HelloWorld文件
package top.ninwoo.learn;public class HelloWorld { private String message; public void getMessage() { System.out.println("Your message " + message); } public void setMessage(String message) { this.message = message; }}
package top.ninwoo.learn;
public class HelloWorld {
private String message;
public void getMessage() {
System.out.println("Your message " + message);
}
public void setMessage(String message) {
this.message = message;
3.创建Main函数
public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloworld"); obj.getMessage(); }}
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloworld");
obj.getMessage();
使用ClassPathXmlApplicationContext读取bean配置文件
4.配置文件
<?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="top.ninwoo.learn.HelloWorld"> <property name="message" value="Hello World!"/> </bean></beans>
<?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="top.ninwoo.learn.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
property 传入了HelloWorld的参数
Spring IoC容器利用Java POJO类和配置元数据来生成完全配置和可执行的系统或应用程序,配置元数据可以由:
容器类型:
ApplicationContext包含全部的BeanFactory,BeanFactory在小型化设备上仍可以用。
构成应用程序主干并由Spring IoC容器管理的对象称为bean.
Bean的定义:
参数:
方法:
<?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-3.0.xsd"> <!-- A simple bean definition --> <bean id = "..." class = "..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- A bean definition with lazy init set on --> <bean id = "..." class = "..." lazy-init = "true"> <!-- collaborators and configuration for this bean go here --> </bean> <!-- A bean definition with initialization method --> <bean id = "..." class = "..." init-method = "..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- A bean definition with destruction method --> <bean id = "..." class = "..." destroy-method = "..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --> </beans>
<?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-3.0.xsd">
<!-- A simple bean definition -->
<bean id = "..." class = "...">
<!-- collaborators and configuration for this bean go here -->
<!-- A bean definition with lazy init set on -->
<bean id = "..." class = "..." lazy-init = "true">
<!-- A bean definition with initialization method -->
<bean id = "..." class = "..." init-method = "...">
<!-- A bean definition with destruction method -->
<bean id = "..." class = "..." destroy-method = "...">
<!-- more bean definitions go here -->
当定义一个
Spring Framework 支持以下5种作用域,如果使用的是web-aware Application只有三种可用。
只创建该Bean定义的对象的一个实例。
<!-- A bean definition with singleton scope --><bean id = "..." class = "..." scope = "singleton"> <!-- collaborators and configuration for this bean go here --></bean>
<!-- A bean definition with singleton scope -->
<bean id = "..." class = "..." scope = "singleton">
配置完成之后,如下的代码,有以下的效果。
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld objA = (HelloWorld) context.getBean("helloWorld"); objA.setMessage("I'm object A"); objA.getMessage(); HelloWorld objB = (HelloWorld) context.getBean("helloWorld"); objB.getMessage();
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.setMessage("I'm object A");
objA.getMessage();
HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
objB.getMessage();
该程序打印:
I'm object AI'm object A
I'm object A
这证明,实际只创建了一个bean对象,虽然调用了两次getBean,但返回的都是同一个对象。
每次调用bean,都会创建一个新的方法。
将上述的bean设置为prototype,执行同样的代码将会出现不同的效果。
I'm object Anull
null
主要指的是,当bean被实例化时,可能需要执行一些初始化以使其进入可用状态。类似地,当不再需要bean并从容器中移除bean时,可能需要进行一些清理。
中间也存在一些活动,本章重点讨论以下两个声明周期回调方法。
org.springframework.beans.factory.InitializingBean已经为我们提供了开发接口。
void afterPropertiesSet() throws Exception;
这样我们就可以通过实现上面的接口实现初始化工作
public class ExampleBean implements InitializingBean { public void afterPropertiesSet() { // do some initialization work }}
public class ExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
另外,对于XML的配置元数据,可以通过init-method指定具有void无参数的方法
<bean id = "exampleBean" class = "examples.ExampleBean" init-method = "init"/>
org.springframework.beans.factory.DisposableBean也已经提供好了接口
void destroy() throws Exception;
这样,我们可以通过实现该接口实现销毁的工作
public class ExampleBean implements DisposableBean { public void destroy() { // do some destruction work }}
public class ExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
注意:如果想要使该函数起作用,需要注册registerShutdownHook()方法。
同样,基于XML配置元数据,可以使用destroy-method指定具有void的无参方法
<bean id = "exampleBean" class = "examples.ExampleBean" destroy-method = "destroy"/>
建议还是使用XML的方式,更加灵活。
如果很多bean都需要同名的初始化和销毁方法,可以在
<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-3.0.xsd" default-init-method = "init" default-destroy-method = "destroy"> <bean id = "..." class = "..."> <!-- collaborators and configuration for this bean go here --> </bean> </beans>
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-init-method = "init"
default-destroy-method = "destroy">
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728