第二章、Srping入门

2017-03-20  本文已影响0人  傲娇的呱太君

一、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
上一篇 下一篇

猜你喜欢

热点阅读