配置文件 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 + "\n"); System.out.println(message + "小王"+ "\n"); System.out.println(message + "是一个形容词" + "\n"); System.out.println("说某人" + message +"有可能是"+xingrongci+"的意思 "+"\n"); System.out.println(message + "小王和小明" + "\n"); } }
效果
时间: 2024-11-05 14:53:43