Spring+Maven学习实验- Spring 中给 Bean 属性注入value(一)

本实验参考实验楼Spring框架入门https://www.shiyanlou.com/courses/578

具体如下:

1.修改 pom.xml 文件

<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.shiyanlou.spring</groupId>
  <artifactId>bean</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bean</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring deploy 主要是添加以下内容 -->
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-core</artifactId>
       <version>4.2.0.RELEASE</version>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>4.2.0.RELEASE</version>
    </dependency>
  </dependencies>
</project>

2.创建FileNameGenerator类

package com.shiyanlou.spring.bean;

public class FileNameGenerator {
    private String name;
    private String type;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    /*
     * 打印文件名和文件类型的方法
     */
    public void printFileName(){
        System.out.println("FileName & FileType is:"+name+","+type);
    }

}

3.在src/main/resources下创建SpringBeans.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-3.0.xsd">
    
    <bean id="FileNameGenerator" class="com.shiyanlou.spring.bean.FileNameGenerator">
       <property name="name">
          <value>zoeychou</value>
       </property>
       <property name="type">
          <value>txt</value>
       </property>
    </bean>
    <!-- 另一种重配置方法 缩写方法 -->
    <!-- 
     <bean id="FileNameGenerator" class="com.shiyanlou.spring.bean.FileNameGenerator">
          <property name="name" value="shiyanlou"/>
          <property name="type" value="txt"/>
     </bean>
     -->
</beans>

4.创建测试类app.java

package com.shiyanlou.spring.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * Hello world!
 *
 */
public class App 
{
    private static ApplicationContext context;
    public static void main( String[] args )
    {
        context = new ClassPathXmlApplicationContext("SpringBeans.xml");
        FileNameGenerator fng = (FileNameGenerator)context.getBean("FileNameGenerator");
        fng.printFileName();
        
    }
}

运行app.java效果

 

posted @ 2017-03-14 11:00  Zoey Chou  阅读(1981)  评论(0编辑  收藏  举报