HelloSpring

来源:互联网 发布:什么是阿里云证书 编辑:程序博客网 时间:2024/05/25 19:57

Hello 实体类

package entity;public class Hello {private String someBody;public void setSomeBody(String someBody) {this.someBody = someBody;}public String getSomeBody() {return someBody;}public void sayHi() {String say = "Hello" + someBody + "!";System.out.println(say);}}

配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!--id是自己随便写的,class是实体类。   property是实体类中的set方法中的名字。   value是给对象定义属性值 --><bean id="hello" class="entity.Hello"><property name="someBody" value="孟振"></property></bean></beans>

测试类

package Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import entity.Hello;public class test {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");Hello h=(Hello)context.getBean("hello");//hello是配置文件中的那个bean的idh.sayHi();}}


输出结果:Hello孟振!

0 0
原创粉丝点击