spring快速开发搭建环境

来源:互联网 发布:淘宝买了双色球送用户 编辑:程序博客网 时间:2024/04/30 09:32

1.加入相关的jar

2.buildpath

3.在src下建立类Hello 有一个属性 Name 有setName(作为一个bean)

new spring bean configuration file --finksh

建立一个bean

package com.my.beans;
public class HelloWord {
private String name;
public void setName1(String name) {
this.name = name;
}
public void Hello(){
System.out.println("hello"+name);
}
}

在应用配置文件中配置bean

<?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.xsd">

<bean id="hello" class="com.my.jd.Hello"> id bean的标识

<property name="name" value="zhangsan"></property>属性名字和值:就是bean的属性名和给属性赋值value

</bean> class 就是bean的全类名即Hello

</beans>

spring 就是通过class得到类名,用反射创建对象进行赋值

在测试类中写:

package com.my.beans;


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


public class Main {


public static void main(String[] args) {
// 1.创建spring的IOC对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC中获取bean实例
HelloWord hello = (HelloWord) context.getBean("hello");
//3.调用hello方法
hello.Hello();
}


}

注意:classpath:是src下



0 0
原创粉丝点击