菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
164
0

Spring入门教程:通过MyEclipse开发第一个Spring项目

原创
05/13 14:22
阅读数 745496

 

 

 

 

 

 

 Animal.java

package com.project;

public class Animal {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 Main.java

package com.project;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Main {

    public static void main(String[] args) {
        Resource resource=new ClassPathResource("applicationContext.xml");
        BeanFactory beanFactory=new XmlBeanFactory(resource);
        Animal animal=(Animal)beanFactory.getBean("animal");
        System.out.println(animal.getName());
    }
}

 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" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="animal" class="com.project.Animal">
        <property name="name">
            <value>Dog</value>
        </property>
    </bean>
</beans>


运行结果:

发表评论

0/200
164 点赞
0 评论
收藏
为你推荐 换一批