spring入门(一)

学习参考教程:https://www.cnblogs.com/fysola/p/6361181.html

(一)环境配置

          JDK11(即JDK1.11)

          eclipse4.13

          jar包:spring-framework-4.0.4.RELEASE(spring框架包)和commons-logging-1.1.3(日志包)

 (二)遇到的问题

         1.(JAVA9新版本特性)导入包的时候在Configure Build Path 中的Libraries中存在Modulepath和Classpath,需要导入在Classpath下。

         Modulepath将jar包当作模块处理,Classpath将Jar包以传统方式导入。

         2.配置beans.xml文件在主程序中调用路径需要为:"/***/beans.xml",这里为不知道路径的情况。正常情况将.xml文件与package放在同一个src目录下,调用路径即为:"beans.xml"(区分大小写)

工程目录

(三)学习过程

        1.创建spi包,包中定义不同类型的接口并且实现这些接口类

        2.在xml文件中对bean进行配置

<?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="chinese" class="spi.Chinese">
        <property name="axe" ref="steelAxe" />
    </bean>
    <bean id="stoneAxe" class="spi.StoneAxe" />
    <bean id="steelAxe" class="spi.SteelAxe" />
    <bean id="win" class="javax.swing.JFrame" />
    <bean id="date" class="java.util.Date" />
</beans>

          bean id创建一个id标签,即可以通过ctx.getBean()进行读取的对象,class指向包中具体实现的类,

          property name="axe" ref="steelAxe",对实现类中的属性进行配置,这里对类中的axe属性进行了配置,配置的文件ref指              向包中实现的其他类(关联)

        3.创建主程序BeanTest,在主程序中调用两个包

                   import org.springframework.context.ApplicationContext;//spring上下文环境
                   import org.springframework.context.support.ClassPathXmlApplicationContext;//spring上下文环境配置

           以及使用ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//获取配置后的spring上下文环境

           最后通过Person p = ctx.getBean("chinese", Person.class);//获取对象

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值