Spring(一:Spring配置)
来源:互联网 发布:四川大学网络学费多少 编辑:程序博客网 时间:2024/05/17 01:32
1. Spring是软件开发的一个非常流行的框架。它可以用一下关键词来描述——开源的、轻量级的、容器、松耦合、框架
2. Spring对于软件开发来说,简单、方便和快捷。使用Spring可以使用简单的JavaBean实现以前只有EJB才能实现的功能。Spring是一个IOC和AOP的容器框架。
3. 首先安装Spring的插件:springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip
4. 新建一个java工程,在工程下面新建一个lib文件夹,将(commons-logging-.jar)(spring-beans-4.0.0.RELEASE.jar)(spring-context-4.0.0.RELEASE.jar)(spring-core-4.0.0.RELEASE.jar)(spring-expression-4.0.0.RELEASE.jar)复制到lib下,然后将这些jar包buildpath。
5. 在src下新建一个包test,新建一个HelloWorld.java文件,文件内容如下:
public class HelloWorld {private String name;public void setName(String name){this.name = name;}public void hello(){System.out.println("Hello:"+name);}}
6. 在新建一个Main.java文件,文件内容如下:
public class Main {public static void main(String[] args) {//创建HelloWorld的一个对象HelloWorld helloWorld = new HelloWorld();//赋值helloWorld.setName("guolujie");//调用hello方法helloWorld.hello();}}
我们从Main可以看出调用hello()方法的步骤如上。
如果我们使用了Spring的话,那么创建对象和初始化都可以由Spring容器来完成。
7. 首先在src下创建一个配置文件applicationContext.xml,在文件中加入bean:
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-3.1.xsd"><!-- 配置bean --><bean id="helloWorld" class="glj.test.spring.beans.HelloWorld"><property name="name" value="Spring"></property></bean></beans>
Main.java的内容变为:
package glj.test.spring.beans;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/*Created By guolujie 2017年9月24日*/public class Main {public static void main(String[] args) {//创建IOC容器对象ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//从IOC容器里获取bean的实例HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");helloWorld.hello();}}
如果我们只是创建了ICO容器的话,那么函数会经过哪些流程呢?
8. 我们来修改一下HelloWorld.java文件,加入几个System语句,运行一下,可以清楚的看到整个流程:
HelloWorld.java
package glj.test.spring.beans;/*Created By guolujie 2017年9月24日*/public class HelloWorld {private String name;public void setName(String name){System.out.println("HelloWorld`s value is:" + name);this.name = name;}public void hello(){System.out.println("Hello:"+name);}public HelloWorld(){System.out.println("HelloWorld`s Constructor...");}}
Main.java
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {//创建IOC容器对象ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");}}
运行结果:
结论:我们可以从上面的结果看出来,如果我们只创建ICO容器的话,那么容器会去调用HelloWorld这个类的构造器,然后将name初始化为Spring。所以Spring容器的作用之一就显而易见了:新建对象和初始化都是由Spring容器来完成的。
阅读全文
0 0
- Spring(一:Spring配置)
- spring-MVC配置(一)
- spring配置缓存(一)
- Spring MVC配置(一)
- spring 事务配置 一
- Spring 配置 注解[一]
- Spring学习笔记(一)----Spring IOC配置与应用
- spring学习(一)--spring的安装配置
- Spring笔记(一)---myeclipse2014配置spring以及简单使用
- spring学习-spring jdbc 连接池的配置(一)
- Spring + Hibernate3+ Struct2 (一)配置
- Spring JPetStore学习(一)配置JPetStore
- 转载 (一)spring配置远程调用
- spring bean的配置(一)
- Spring 集成 Memcached 配置(一)
- Spring 集成 ActiveMQ 配置(一)
- 部分spring配置带注释(一)
- spring学习笔记 --配置项(一)
- cartographer源码分析(38)-io-null_points_processor.h
- C语言数据结构-栈-数组实现
- [第五季]7.HTML列表和表单标签
- 多校连萌 【赛后记录】
- Java for Web学习笔记(七三):国际化i18n(1)使用Spring框架MessageSource
- Spring(一:Spring配置)
- JavaScript学习--数组
- hdu 2076 夹角有多大(题目已修改,注意读题)
- 回形取数
- 1086. Tree Traversals Again (25)
- 内核编译
- 微信下拉出现黑框IP地址信息修改办法
- JavaScript学习--函数
- PCA的PWM