spring(2)_开发环境搭建

使用myeclipse自动反转

  1. 新建java项目:sday01
  2. 右击项目
  3. 点击 Add Spring Capabilities...
项目如下:

但是一般开发中不用翻转
如下:

手动添加spring

1、引入spring的jar文件
libs/spring-beans-3.2.2.RELEASE.jar
libs/spring-context-3.2.2.RELEASE.jar
libs/spring-context-support-3.2.2.RELEASE.jar
libs/spring-core-3.2.2.RELEASE.jar
libs/spring-expression-3.2.2.RELEASE.jar

需要:
commons-logging-1.1.1.jar


2、需要添加spring的配置文件
 配置文件的名称可以随便起名,但是最好有实际意义。
   例如:
spring-dao.xml
spring-service.xml
spring.xml

3、spring配置文件 怎么引入 beans约束
  1. ---spring-framework-3.2.2.RELEASE-dist/spring-framework-3.2.2.RELEASE/docs/spring-framework-reference/html/index.html
  2.    帮助文档 目录中:5.2.1. Configuration metadata  点击进去
   可以拷贝原信息:
    
  <?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="..." class="...">
    <!-- collaborators and configuration for this bean go here -->
  </bean>


  <bean id="..." class="...">
    <!-- collaborators and configuration for this bean go here -->
  </bean>


  <!-- more bean definitions go here -->


</beans>


4、下面就有实例化 :容器
// 读取 classes 路径下面的文件  参数 动态参数、单个参数、数组 、可以使用通配符等
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");


5、在容器中获取bean
   HelloDao helloDao = (HelloDao) context.getBean("helloDaoImpl");
   HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);
   
6、有关spring配置文件 多个的时候 可以采用import标签导入一个文件中
 <import resource="spring-dao.xml"/>
 <import resource="spring-service.xml"/>
 
 
7、spring容器 管理bean
    bean 属性 
        id:  标识 唯一
        class: 类
        scope:作用域   singleton  prototype  request session
        lazy-init:default=false ,false ,true  
           lazy-init结合scope=singleton使用
             scope="singleton" lazy-init="default" -->说明:容器已经加载就实例化对象
             scope="singleton" lazy-init="true" -->说明:容器已经加载当使用到该对象的时候,实例化该对象
               



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值