下载spring:

https://repo.spring.io/

创建一个Web项目导入必要包和log配置文件:




编写注入类HelloSpring

编写ApplicationContext.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.xsd">
    <!-- id获取bean节点的唯一标识 class注入的类所在的具体包 -->
    <bean id="testHelloSpring" class="cn.spring.demo.SpringHello">
        <!-- property类的属性 name则为属性名 -->
        <property name="name">
            <!-- 赋值 -->
            <value>张三</value>
        </property>
        <property name="age">
            <value>18</value>
        </property>
    </bean>
</beans>

编写测试类:


 @Test
    public void hello(){
        /* 获取配置文件实例 */
        ApplicationContext app=new ClassPathXmlApplicationContext("ApplictionContext.xml");
        /* 获取bean节点 */
        SpringHello helo = (SpringHello)app.getBean("testHelloSpring");
        helo.print();
    }

其他方法获取配置文件:


FileSystemXmlApplicationContext
BeanFactory


标签: JAVA, spring

添加新评论