Spring入门一----HelloWorld

知识点:

  1. 简介
  2. HelloWorld

简介:

百度百科

 

HelloWorld

项目结构图:

    

导入Spring支持包:

然后选中所有包,右键Build Path à Add to Build Path

编写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.xsd">

<!--相当于实例化HelloWorld类-->

<bean id="HelloWorld" class="com.qinb.service.HelloWorld"></bean>

 

</beans>

编写HelloWorld类:

编写Test类

public class Test {

    public static void main(String[] args) {

        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");

        HelloWorld helloWorld=(HelloWorld)ac.getBean("helloWorld");

        helloWorld.say();

    }

}

测试:

 

其实这里的通过在bean中对类进行实例化,然后在需要调用的时候直接调用bean的设置的id就行了,如果不用spring,我们平时在调用一个类的时候,通常采用new的方法来new一个对象,来供我们使用,这样在比较大的项目中不适合我们项目使用,占内存也耦合度比较高,就相当于我们需要筷子吃饭,在我们要吃饭的时候我们是新做一个筷子出来吃饭好些还是先把筷子做好,在我们吃饭的时候直接拿来就行了?当然是后面的情况吧。

posted @ 2016-06-15 23:36  旷世  阅读(383)  评论(0编辑  收藏  举报