JAVA Spring 第一课

来源:互联网 发布:java防止页面脚本注入 编辑:程序博客网 时间:2024/04/19 15:31

  接口及面向接口编程:

 接口:对外说明,隐藏对内实现。接口拥有方法类eg List: ArrayList()

面向接口编程:分清结构设计中的层级和调用关系,每层只向上层听歌一组功能接口。

例子:一个接口

public interface OneInterFace{

String hello(string word);

}

接口实现类

public class OneInterFaceImpl implements OneInterFace{

public String hello(String word){

具体实现方法;

}

}

IOC:控制反转:外部容器负责对象的创建和维护

DI:依赖注入


接口的Bean配置:spring-ioc.xml

<?xml version = "1.0"encoding = "ITF-8"?>

<beans xmlns = "****"

******

<bean id = "oneInterFace" class="路径.OneInterFaceImpl(实体类)"></bean>

</beans>

新建类的构造函数,内容 super("classpath*:spring-ioc.xml");//xml配置文件名

申请实例:OneInterFace oneInterFace = super.getBean("oneInterFace");//getBean方法来自创建的unitTestBase类 ,import org.junit.Test;


Bean 容器初始化

基础:两个包

- org.springframework.beans

-org.springframework.context

方式,ApplicationContext//保存Bean对象,在Spring中广泛使用

-本地文件

-Classpath//项目中的相对路径

-Web应用中依赖servlet 或Listener

例子:

本地文件:FileSystemXmlApplicationcontext context = new FileSystemXmlApplicationcontext("本地路径");

Classpath:ClassPathXmlApplicationcontext context = new ClassPathXmlApplicationcontext("相对路径");

Web:

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<servlet>

<servlet-name>context</servlet-name>

<servlet-class>org.springframework.web.ContextLoaderListener</servlet-class>

<load-on-stratup>1</load-on-stratup>

</servlet>

原创粉丝点击