第二章、Srping入门

一、Maven配置

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.huangyian</groupId>
<artifactId>firstmaven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.5.RELEASE</version>
            </dependency>
    </dependencies>
</project>

二、一个简单的 Spring bean

1、Bean类

package tg.export;
public class GoodsExport {

    private Integer id;
    
    /**
   * 实现方法
   * @return
   */
    public Integer addGoods() {
        return this.id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

2、Spring 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="GoodsExport" class="tg.export.GoodsExport">
        <property name="id" value="5" />
     </bean>
</beans>

3、执行代码

public class App {
    public static void main(String[] args) {
         ApplicationContext context = new ClassPathXmlApplicationContext(
            "config/applicationContext.xml");
         GoodsExport obj = (GoodsExport) context.getBean("GoodsExport");
         System.out.println(obj.addGoods());
    }
}

4、结果为5

5
Process finished with exit code 0
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,987评论 6 342
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,754评论 0 3
  • 我在你还未大红大紫的时间,偶然而来。 初始惊艳,限于才华,忠于人品。 慢慢的,你越来越红。你的事业开始上升。 渐渐...
    书笑人阅读 264评论 0 0
  • 1女生都不能避免一个事,来月经,我经常用大姨妈来称呼她。人们常开玩笑的是:一个月总有那么几天,脾气不好。是的,总有...
    林小仙仙仙阅读 527评论 0 6