Eclipse spring基本配置步骤

**注意要接口编程

修改beans.xml文件
**如果本机没有联网,当输入"<"时,不会有相应的提示,这时需要进行一些配置

打开windows->preferences->xml->xmlcatalog
点"add",在出现的窗口中的Key Type中选择URL,在location中选"File system",
然后在spring解压目录的dist/resources目录中选择spring-beans-2.5.xsd,回到设置
窗口时,把Key Type改为Schemalocation,Key改为
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


beans.xml中的<bean>元素是用于配置我们要交给Spring管理的bean类。
id属性:是唯一的,通过它获得该Bean,该值不能包含特殊字符的
name属性:该值可以包含特殊字符的(如:/ss/ss)
如果没有特殊字符,建议用id
class属性:指定要交给Spring管理的Bean类

当bean创建好以后,就会由Spring容器帮我们创建和维护,当我们用到
该bean时,只需从Spring容器中获取就可以了。

 

1.导入两个最常用的jar包

    spring.jar和commons-logging.jar

2.在src中创建beans.xml文件

3.实例化spring容器
  (1)在类路径下寻找配置文件来实例化容器
     ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
4.建立业务Bean

 

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="cn.test.service.impl.PersonServiceBean"></bean>

</beans>

 

接口:PersonService.java

public interface PersonService {

 public void save();

}

 

实现类PersonServiceBean.java

public class PersonServiceBean implements PersonService {
 /* (non-Javadoc)
  * @see cn.test.service.impl.PersonService#save()
  */
 public void save(){
  System.out.println("我是save()方法");
 }
 
}

 

测试类test.java

public class test {
 public static void main(String args[])
 {
  // ClassPathXmlApplicationContext为Spring容器的实例(该步为实例化)
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  // 从容器中获取bean,然后通过接口对其进行引用
        PersonService personService = (PersonService)ctx.getBean("personService");
       
        personService.save();
 }

}

运行test.java 。框架已成功

 

常用jiar包下载:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值