zoukankan      html  css  js  c++  java
  • 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效果

  • 相关阅读:
    java生成随机大数据文件
    spark读hdfs文件实现wordcount并将结果存回hdfs
    spark streaming 实现接收网络传输数据进行WordCount功能
    spark mllib k-means算法实现
    java实现读取文件大全
    spark+hcatalog操作hive表及其数据
    在hdfs上存取xml文件的实现代码
    hadoop shell 详解
    day 01 预科
    c盘没有新建修改权限的,执行下面命令
  • 原文地址:https://www.cnblogs.com/zoeyqq/p/6547411.html
Copyright © 2011-2022 走看看