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

【Java EE 学习 49 上】【Spring学习第一天】【基本配置】

2015-09-28 09:33 519 查看
一、HelloWorld

    需要的jar文件(以2.5.5为例):spring.jar,common-logging.jar

  1.新建类com.kdyzm.spring.helloworld.HelloWorld.java

<?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-2.5.xsd"> <bean id="person" class="com.kdyzm.spring.di.setting.Person">
<property name="name" value="小黄"></property>
<property name="num" value="123456"></property>
<property name="student" ref="student"></property>
<property name="list">
<list>
<ref bean="student"/>
<ref bean="student"/>
</list>
</property>
<property name="set">
<set>
<ref bean="student"/>
<ref bean="student"/>
</set>
</property>
<property name="map">
<map>
<entry key="张三" value-ref="student"></entry>
<entry key="李四" value-ref="student"></entry>
<entry key="王五" value-ref="student"></entry>
</map>
</property>
</bean>
<bean id="student" class="com.kdyzm.spring.di.setting.Student">
<property name="id" value="1"></property>
<property name="name" value="小强"></property>
</bean>
</beans>


com.kdyzm.spring.di.setting.applicationContext.xml
      测试代码:

ApplicationContext context=new ClassPathXmlApplicationContext("com/kdyzm/spring/di/setting/applicationContext.xml");
Person person=(Person) context.getBean("person");
System.out.println(person);


      运行结果:

Person对象被创建!
Student对象被创建!
Person [name=小黄, num=123456, student=Student [id=1, name=小强], list=[Student [id=1, name=小强], Student [id=1, name=小强]], set=[Student [id=1, name=小强]], map={张三=Student [id=1, name=小强], 李四=Student [id=1, name=小强], 王五=Student [id=1, name=小强]}]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: