Spring学习(一)

来源:互联网 发布:国外语音聊天软件 编辑:程序博客网 时间:2024/06/05 11:21

一、Spring了解

基本了解:spring最为熟知的几个功能:依赖注入/控制反转 和 面向切面编程

在Spring中有许多的容器,在以前都是使用Bean工厂,但是现在基本都是使用应用

上下文

下面是几种常用的应用上下文加载方式:

ClassPathXmlApplicationContext:通过加载类路径下的xml文件,最常使用的方

式,即加载src目录下的bean.xml文件。

FileSystemXmlApplicationContext:通过指定绝对路径加载配置文件。

XmlWebApplicationContext:读取web应用下的配置文件加载。

二、第一个Spring实例

package com.firstSpring;public interface Helloworld {    public String sayHi();}
package com.firstSpring;public class HelloWorldImpl implements Helloworld {    @Override    public String sayHi() {        return "firstSpring";    }}
package com.firstSpring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestMain {    public static void main(String[] args) {        //1:创建Spring的IOC容器对象        ApplicationContext apc=new ClassPathXmlApplicationContext("springXML/beans.xml");         //2:从IOC容器中获取Bean实例        Helloworld hello=(Helloworld) apc.getBean("helloWorld");        System.out.println(hello.sayHi());    }}
<?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-3.0.xsd">    <bean id="helloWorld" class="com.firstSpring.HelloWorldImpl"></bean></beans>