Spring开发步骤

来源:互联网 发布:酷宝数据 作假 编辑:程序博客网 时间:2024/06/08 15:04

1.Spring介绍

Spring是一种可以解决对象创建和处理对象依赖关系的一种框架,可以和其他框架 一起使用。

2.spring开发步骤

2.1spring对象层次图

2.2步骤

2.2.1引入jar包

五个必须映入的jar文件

commons-logging-1.1.3.jar           日志

spring-beans-3.2.5.RELEASE.jar       bean节点

spring-context-3.2.5.RELEASE.jar      spring上下文节点

spring-core-3.2.5.RELEASE.jar        spring核心功能

spring-expression-3.2.5.RELEASE.jar   spring表达式相关表


2.2.2写配置文件

核心配置文件:applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="user2" class="cn.itcast.b_create_obj.User">
</bean>
</beans>  
bean节点下的id:对象名

bean节点下的class:类的全路径


2.2.3创建IOC对象

public void testAc() throws Exception {// 得到IOC容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/a_hello/applicationContext.xml");// 从容器中获取beanUser user = (User) ac.getBean("user");System.out.println(user);}}



0 0
原创粉丝点击