Spring入门程序及准备工作---01

来源:互联网 发布:ai汉化包for mac 编辑:程序博客网 时间:2024/06/05 16:21

spring-framework-x.x.x.RELEASE-dist.zip     ------Spring开发包

         开发包目录结构:

                   docs     框架API和规范

                   libs       jar包 javadoc  sources

                   schema     约束

spring-framework-x.x.x.RELEASE-dependencies.zip      ------依赖包

传统方式 
package cn.nedu.wy.demo01;public interface HelloService {public void sayHello();}

package cn.nedu.wy.demo01;public class HelloServiceImpl implements HelloService {private String info;public void setInfo(String info) {this.info = info;}public void sayHello(){System.out.println("hello spring!"+info);}}

public void demo01(){HelloService helloService = new HelloServiceImpl();helloService.sayHello();}

    传统方式 造成 程序紧密耦合

Spring开发方式

    引入所需jar包

     在src下创建 applicationContext.xml

       找到xsd-config.html 引入beans约束

       

<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">
在applicationContext.xml配置
<bean id="SpringService" class="cn.nedu.wy.demo01.HelloServiceImpl"></bean>
    
public void demo02(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");HelloService springService = (HelloService) applicationContext.getBean("SpringService");springService.sayHello();}
注意:有时候  spring 和 jdk的版本 问题 会引起 莫名其妙的问题!!!


原创粉丝点击