首页 技术 正文
技术 2022年11月21日
0 收藏 641 点赞 4,395 浏览 3634 个字

搭建环境完成之后。使用spring config editor打开配置的spring xml文件。

自己实现了,spring在java上的一些基本的应用。一共看了四节视频。下面是自己实现的编码:

最基本的就是创建spring的ioc容器对象。从ioc中得到bean的实例对象

对于spring的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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- 配置bean文件 -->
<bean id="helloword" class="lib.Helloword">
<property name="name" value="spring"></property>
</bean>
<bean id="car22" class="lib.Car">
<constructor-arg value="wen" index=""></constructor-arg>
<constructor-arg index="">
<value><![CDATA[<ncjsnjkad>]]></value>
</constructor-arg>
</bean>
<bean id="person" class="lib.Person">
<property name="name" value="Tom"></property>
<property name="age" value=""></property>
<!-- 建立bean之间的关系的ref写法 -->
<!-- <property name="car" ref="car2"></property> <property name="car">
<ref bean="car2"/>
</property>-->
<!-- 内部的bean的使用方法,不能为外部的引用 -->
<property name="car">
<bean class="lib.Car">
<constructor-arg value="wen" index=""></constructor-arg>
<constructor-arg index="">
<value><![CDATA[<ncjsnjkad>]]></value>
</constructor-arg>
<!-- 这是null的专属标记
<constructor-arg><null/></constructor-arg> -->
<!-- 为级联属性赋值,属性需要初始化才可以在次进行级联赋值
<property name="person.name" value=""></property>
-->
</bean>
</property>
</bean>
<bean id="car1" class="lib2.Car">
<constructor-arg value="wen" index=""></constructor-arg>
<constructor-arg index="">
<value><![CDATA[<ncjsnjkad>]]></value>
</constructor-arg>
</bean>
<bean id="car2" class="lib2.Car">
<constructor-arg value="wen" index=""></constructor-arg>
<constructor-arg index="">
<value><![CDATA[<ncjsnjkad>]]></value>
</constructor-arg>
</bean>
<bean id="car3" class="lib2.Car">
<constructor-arg value="wen" index=""></constructor-arg>
<constructor-arg index="">
<value><![CDATA[<ncjsnjkad>]]></value>
</constructor-arg>
</bean>
<!-- 配置集合属性 -->
<bean id="person3" class="lib2.Person">
<property name="name" value="mike"></property>
<property name="age" value=""></property>
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</list>
</property>
</bean>
<!-- 配置map属性值 -->
<bean id="person33" class="lib2.Person3">
<property name="name" value="mao"></property>
<property name="age" value=""></property>
<property name="cars">
<map>
<!-- 使用map的节点和map的子节点entry进行配置 -->
<entry key="AA" value-ref="car1"></entry>
<entry key="BB" value-ref="car2"></entry>
</map>
</property>
</bean>
<bean id="datasourse" class="lib2.Datasourse">
<property name="properties">
<!-- 使用props和prop节点来为properties来配置信息 -->
<props>
<prop key="us">djkchsk</prop>
<prop key="ee">hkjcsd</prop>
<prop key="www">chjdss</prop>
</props>
</property>
</bean>
<!-- 配置单例的集合bean,以供多个bean进行引用 -->
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</util:list>
<!-- 通过排名明空间为bean的属性赋值、需要先导入p的命名空间,先对于传统的更加简洁 -->
<bean id="person5" class="lib2.Person" p:age="" p:name="quens" p:cars-ref="cars"></bean>
</beans>

简单列出其中的主函数:

package lib2;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ceshi.newww;public class spring { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext se3t=new ClassPathXmlApplicationContext("applicationcontext.xml");
Person person3=(Person) se3t.getBean("person3");
System.out.println("1 "+person3);
Person3 person33=(Person3) se3t.getBean("person33");
System.out.println("2 "+person33);
Datasourse datasourse=se3t.getBean(Datasourse.class);
System.out.println("3 "+datasourse);
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860