Spring开发配置

来源:互联网 发布:牛尔的产品怎么样知乎 编辑:程序博客网 时间:2024/06/03 22:45

一、下载开发包
    http://projects.spring.io/spring-framework/ 网址下载开发包
二、 创建web工程引入相应jar包
    spring-aop-4.1.6.RELEASE.jar
    spring-aspects-4.1.6.RELEASE.jar
    spring-beans-4.1.6.RELEASE.jar
    spring-context-4.1.6.RELEASE.jar
    spring-context-support-4.1.6.RELEASE.jar
    spring-core-4.1.6.RELEASE.jar
    spring-expression-4.1.6.RELEASE.jar
    spring-jdbc-4.1.6.RELEASE.jar
    spring-orm-4.1.6.RELEASE.jar
    spring-tx-4.1.6.RELEASE.jar
    spring-web-4.1.6.RELEASE.jar
    spring-webmvc-4.1.6.RELEASE.jar
开发的日志记录的包:
    commons-logging-1.1.1.jar
    log4j-1.2.17.jar
    slf4j-log4j12-1.7.12.jar
    编写log4j.properties文件
三、创建Spring的配置文件
    在src下创建一个applicationContext.xml,引入XML的约束,找到xsd-config.html.引入beans约束。

<?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="address" class="com.bean.Address">    <property name="address" value="西安"/></bean>

五、创建测试类
    ApplicationContext 应用上下文,加载Spring 框架配置文件。

 加载classpath:        new ClassPathXmlApplicationContext(“applicationContext.xml”); 加载磁盘路径:        new FileSystemXmlApplicationContext(“applicationContext.xml”);
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");Student st= (Student)ac.getBean("student");st.show();BeanFactory context = new ClassPathXmlApplicationContext("applicationContext.xml");Address hello= context.getBean(Address.class);Address hello2=(Address)context.getBean("address");hello.show();hello2.show();