您的位置:首页 > 编程语言 > Java开发

spring快速开发搭建环境

2015-04-17 23:30 246 查看
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下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: