• 我的第六个java程序 spring-bean


    配置文件 Beans.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="helloWorld" class="com.importnew.HelloWorld">
               <property name="message" value="棒棒哒"/>
               <property name="xingrongci" value="夸奖"/>
           </bean>
           
           
     
        </beans>

    主程序 MainApp.java

    package com.importnew;
     
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    public class MainApp {
        public static void main(String[] args) {
     
            ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
     
            HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
            obj.printMessage();
            
    
            
        }
    }

    HelloWorld.java

    package com.importnew;
     
    public class HelloWorld {
     
        private String message;
        private String xingrongci;//私有
     
        public void setMessage(String message){
            this.message  = message;
        }
     
        public String getMessage(){
            return this.message;
        }
        
        /**设置形容词为此类的一个属性**/
        public void setXingrongci(String xingrongci){
            this.xingrongci  = xingrongci;
        }
     
        public String getXingrongci(){
            return this.xingrongci;
        }
        
     
        public void printMessage(){
            System.out.println("小明" + message + "
    ");
            System.out.println(message + "小王"+ "
    ");
            System.out.println(message + "是一个形容词" + "
    ");
            System.out.println("说某人" + message +"有可能是"+xingrongci+"的意思 "+"
    ");
            System.out.println(message + "小王和小明" + "
    ");
        }
    }

    效果

  • 相关阅读:
    Python float() 函数
    Python bytearray() 函数
    scanner.nextInt()与scanner.nextDouble
    Scanner对象next与nextLine
    JavaDoc
    包机制
    运算符要点
    变量与常量
    test
    类型转换
  • 原文地址:https://www.cnblogs.com/hellowzd/p/5002951.html
Copyright © 2020-2023  润新知