Spring_HelloWorld

来源:互联网 发布:win7电脑桌面美化软件 编辑:程序博客网 时间:2024/06/07 16:37

spring是什么?

1.spring是一个开源框架,为了解决企业应用开发的复杂性而创建的,但现在已经不仅应用于企业应用。
2.是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架。

详细描述spring

1.轻量级:spring是非侵入性的–基于Spring开发的应用中的对象可以不依赖于Spring的API。
2.依赖注入(IOC或DI:dependency injection)
3.面向切面编程(AOP:aspect oriented programming)
4.容器:Spring是一个容器,因为他包含并管理对象的生命周期。
5.框架:Spring实现了使用简单的组件配置组合成一个复杂的应用。
6.一站式:在IOC和AOP的基础上整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了展现层的SpringMVC和持久层的Spring JDBC)

Spring的HelloWorld代码:

实体类代码:

package com.lee.spring.beans;public class Person {    private String name;    public void setName(String name) {        this.name = name;    }    public void hello() {        System.out.println("Hello "+name);    }}

application.xml代码:

<?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="person"  class="com.lee.spring.beans.Person">    <property name="name" value="Spring"/></bean></beans>

测试代码:

package com.lee.spring.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lee.spring.beans.Person;public class Test {    public static void main(String[] args) {        //1.创建Spring的IOC容器对象        ApplicationContext act=new ClassPathXmlApplicationContext("ApplicationContext.xml");        //2.从IOC中获取bean的实例        Person p=(Person) act.getBean("person");        //3.调用        p.hello();    }}

运行结果:

这里写图片描述