zoukankan      html  css  js  c++  java
  • 第一个Spring例子

    手动添加sechame文件:

    windows-->preferences-->myeclipse-->files and editors-->xml-->xmlcatalog--->点add--->Key Type中选择URL-->File system-->在dist/resources/spring-beans-2.5.xsd-->Sechema location;

    下载Spring的包

    1.倒入所需要的Spring的jar包

      必须要的两个jar包:dist\spring.jar,lib\jakarta-commons-logging.jar

      如果使用了切面变成,还需要下列jar文件:lib\aspectj\aspectjwear.jar和aspectjrt.jar,lib\cglib\cglib-nodep-2.1_3.jar

      如果使用了JSR-250中的注解:lib\j2ee\common-annotations.jar

    2.建立beans.xml文件

      

    <?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-2.5.xsd">

    <bean id="personService" class="com.cn.service.impl.PersonServiceBean"></bean>
    </beans>

    3.定义一个借口,并写出其实现层:

    package com.cn.service;

    public interface PersonService {

    public abstract void save();

    }

    package com.cn.service.impl;

    import com.cn.service.PersonService;

    public class PersonServiceBean implements PersonService {
    public void save(){
    System.out.println("my name is save method");
    }
    }

    4.通过Spring框架来调用bean中的方法 

    ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
    PersonService psersonService=(PersonService) ctx.getBean("personService");
    psersonService.save();

      

  • 相关阅读:
    发现一个github上特别优秀的面试准备资料
    坠吼的大哥的博客
    [BJDCTF 2nd]假猪套天下第一
    [网鼎杯 2020 朱雀组]phpweb
    [GWCTF 2019]我有一个数据库
    [BJDCTF2020]ZJCTF,不过如此
    [GXYCTF2019]禁止套娃
    洛谷
    [ZJCTF 2019]NiZhuanSiWei
    蓝帽杯决赛-爆炒腰花-WP
  • 原文地址:https://www.cnblogs.com/GodFather001/p/2281725.html
Copyright © 2011-2022 走看看