Spring(一:Spring配置)

       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"?>
<beans
	xmlns="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容器来完成的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值