spring4学习之HelloWorld(一)

一、spring框架
spring是一站式轻量级开源框架。核心:Ioc控制反转和Aop面向切面。
优点:
1.低侵入式设计,代码污染极低
2.独立于各种应用服务器,基于Spring框架的应用,可以真正实现Write Once,Run Anywhere的承诺
3.Spring的DI机制降低了业务对象替换的复杂性,提高了组件之间的解耦
4.Spring的AOP支持允许将一些通用任务如安全、事务、日志等进行集中式管理,从而提供了更好的复用
5.Spring的ORM和DAO提供了与第三方持久层框架的良好整合,并简化了底层的数据库访问
6.Spring并不强制应用完全依赖于Spring,开发者可自由选用Spring框架的部分或全部

二、spring4入门实例

1.下载spring源码,导入核心包。
这里写图片描述

2.在classpath目录下,创建beans.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="helloWorld" class="com.newbeedaly.test.HelloWorld"></bean>


</beans>

3.创建HelloWorld类

package com.newbeedaly.test;

public class HelloWorld {

    public void say(){
        System.out.println("Spring4大爷你好!");
    }
}

4.创建测试类

package com.newbeedaly.service;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.newbeedaly.test.HelloWorld;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld helloWorld=(HelloWorld)ac.getBean("helloWorld");
        helloWorld.say();
    }
}

猜你喜欢

转载自blog.csdn.net/willdic/article/details/80535891