Spring基本知识1
2015-10-14 19:21
441 查看
<?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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 这是在spring主配置文件中,告诉spring IOC容器,user在这里spring一旦运行起来就会首先加载该配置文件作为spring的上下文
创建user对象(实例) -->
<bean id="user" class="com.spring.di.User">
<property name="id" value="2"></property>
<property name="username">
<value>邵磊是傻逼</value>
</property>
<property name="pasword">
<value>aass</value>
</property>
</bean>
<bean id="person" class="com.spring.di.Person">
<property name="name">
<value>是是是</value>
</property>
<property name="age">
<value>22</value>
</property>
<property name="salary">
<value>122222</value>
</property>
<property name="user">
<ref bean="user"></ref>
</property>
<property name="user2">
<!-- 内部bean对象作用范围只在当前personbean的内部,外部 是看不见的不能被外部bean使用 -->
<bean class="com.spring.di.User">
<property name="id" value="4"></property>
<property name="username">
<value>邵</value>
</property>
</bean>
</property>
</bean>
<bean id="dog" class="com.spring.di.Dog">
<!-- 根据构造器参数的索引来注入 -->
<!-- <constructor-arg value="白色" index="0"></constructor-arg> <constructor-arg
value="小花花" index="1"></constructor-arg> -->
<!-- 根据构造器参数类型注入 -->
<constructor-arg value="白色" type="java.lang.String"></constructor-arg>
<constructor-arg value="小花花" type="java.lang.String"></constructor-arg>
<constructor-arg value="1" type="java.lang.Integer"></constructor-arg>
<constructor-arg ref="user" type="com.spring.di.User"></constructor-arg>
</bean>
<!-- 注入空值 也可以注入 -->
<bean id="cat" class="com.spring.di.Cat">
<property name="name" value="珍妮"></property>
<property name="color">
<null />
</property>
</bean>
<!-- 范型注入 -->
<bean id="pet" class="com.spring.di.Pet">
<property name="dogs">
<list>
<value>小花花</value>
<value>珍妮</value>
<value>旺财</value>
<value>小强</value>
</list>
</property>
<property name="cats">
<set>
<value>小mai</value>
<value>珍妮d</value>
<value>旺财ddd</value>
<value>小强dd</value>
</set>
</property>
<property name="pigs">
<map>
<entry>
<key>
<value>1</value>
</key>
<value>dddddddd</value>
</entry>
<entry key="2" value="龚兴岱">
</entry>
</map>
</property>
<!-- 数组类型 -->
<property name="birds">
<list>
<value>信天翁</value>
<value>喜鹊</value>
</list>
</property>
<!-- properties类型 -->
<property name="pp">
<props>
<prop key="p1" >p1_value</prop>
<prop key="p2" >p2_value</prop>
</props>
</property>
</bean>
<!-- 自动注入 -->
<bean id="a" class="com.spring.auto.A">
<property name="name" value="AAA"></property>
</bean>
<bean id="b" class="com.spring.auto.B">
<property name="bname" value="BBB"></property>
</bean>
<!-- 正常对象属性注入 -->
<bean id="ab" class="com.spring.auto.AB">
<property name="a">
<ref bean="a"></ref>
</property>
<property name="b">
<ref bean="b"></ref>
</property>
</bean>
<!-- byname Spring 的IOC容器会根据AB类的属性名称去在容器中自动查找id等于其名称的bean 进行自动装配,实现注入-->
<bean id="byname" class="com.spring.auto.AB" autowire="byName">
</bean>
<!-- bytypeSpring 的IOC容器会根据AB类的属性类型去在容器中自动查找类型等同于属性类型的bean 进行自动装配,实现注入 -->
<bean id="bytype" class="com.spring.auto.AB" autowire="byType">
</bean>
<!--注意!!多个类型相同的bean使用bytype自动装配就会报出异常 org.springframework.beans.factory.NoUniqueBeanDefinitionException: -->
<!-- <bean id="b1" class="com.spring.auto.B">
<property name="bname" value="BBB1"></property> </bean> -->
<!-- 分散装配PropertyPlaceHolderConfigurer -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>test.properties</value>
</property>
</bean>
<bean id="testpro" class="com.spring.auto.Location">
<proper
953f
ty name="key1" value="${key1}"></property>
<property name="key2" value="${key2}"></property>
<property name="key3" value="${key3}"></property>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 这是在spring主配置文件中,告诉spring IOC容器,user在这里spring一旦运行起来就会首先加载该配置文件作为spring的上下文
创建user对象(实例) -->
<bean id="user" class="com.spring.di.User">
<property name="id" value="2"></property>
<property name="username">
<value>邵磊是傻逼</value>
</property>
<property name="pasword">
<value>aass</value>
</property>
</bean>
<bean id="person" class="com.spring.di.Person">
<property name="name">
<value>是是是</value>
</property>
<property name="age">
<value>22</value>
</property>
<property name="salary">
<value>122222</value>
</property>
<property name="user">
<ref bean="user"></ref>
</property>
<property name="user2">
<!-- 内部bean对象作用范围只在当前personbean的内部,外部 是看不见的不能被外部bean使用 -->
<bean class="com.spring.di.User">
<property name="id" value="4"></property>
<property name="username">
<value>邵</value>
</property>
</bean>
</property>
</bean>
<bean id="dog" class="com.spring.di.Dog">
<!-- 根据构造器参数的索引来注入 -->
<!-- <constructor-arg value="白色" index="0"></constructor-arg> <constructor-arg
value="小花花" index="1"></constructor-arg> -->
<!-- 根据构造器参数类型注入 -->
<constructor-arg value="白色" type="java.lang.String"></constructor-arg>
<constructor-arg value="小花花" type="java.lang.String"></constructor-arg>
<constructor-arg value="1" type="java.lang.Integer"></constructor-arg>
<constructor-arg ref="user" type="com.spring.di.User"></constructor-arg>
</bean>
<!-- 注入空值 也可以注入 -->
<bean id="cat" class="com.spring.di.Cat">
<property name="name" value="珍妮"></property>
<property name="color">
<null />
</property>
</bean>
<!-- 范型注入 -->
<bean id="pet" class="com.spring.di.Pet">
<property name="dogs">
<list>
<value>小花花</value>
<value>珍妮</value>
<value>旺财</value>
<value>小强</value>
</list>
</property>
<property name="cats">
<set>
<value>小mai</value>
<value>珍妮d</value>
<value>旺财ddd</value>
<value>小强dd</value>
</set>
</property>
<property name="pigs">
<map>
<entry>
<key>
<value>1</value>
</key>
<value>dddddddd</value>
</entry>
<entry key="2" value="龚兴岱">
</entry>
</map>
</property>
<!-- 数组类型 -->
<property name="birds">
<list>
<value>信天翁</value>
<value>喜鹊</value>
</list>
</property>
<!-- properties类型 -->
<property name="pp">
<props>
<prop key="p1" >p1_value</prop>
<prop key="p2" >p2_value</prop>
</props>
</property>
</bean>
<!-- 自动注入 -->
<bean id="a" class="com.spring.auto.A">
<property name="name" value="AAA"></property>
</bean>
<bean id="b" class="com.spring.auto.B">
<property name="bname" value="BBB"></property>
</bean>
<!-- 正常对象属性注入 -->
<bean id="ab" class="com.spring.auto.AB">
<property name="a">
<ref bean="a"></ref>
</property>
<property name="b">
<ref bean="b"></ref>
</property>
</bean>
<!-- byname Spring 的IOC容器会根据AB类的属性名称去在容器中自动查找id等于其名称的bean 进行自动装配,实现注入-->
<bean id="byname" class="com.spring.auto.AB" autowire="byName">
</bean>
<!-- bytypeSpring 的IOC容器会根据AB类的属性类型去在容器中自动查找类型等同于属性类型的bean 进行自动装配,实现注入 -->
<bean id="bytype" class="com.spring.auto.AB" autowire="byType">
</bean>
<!--注意!!多个类型相同的bean使用bytype自动装配就会报出异常 org.springframework.beans.factory.NoUniqueBeanDefinitionException: -->
<!-- <bean id="b1" class="com.spring.auto.B">
<property name="bname" value="BBB1"></property> </bean> -->
<!-- 分散装配PropertyPlaceHolderConfigurer -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>test.properties</value>
</property>
</bean>
<bean id="testpro" class="com.spring.auto.Location">
<proper
953f
ty name="key1" value="${key1}"></property>
<property name="key2" value="${key2}"></property>
<property name="key3" value="${key3}"></property>
</bean>
</beans>
相关文章推荐
- PhoneGap插件开发 js与Java之间的交互例子 详解
- JavaWeb监听器
- 为什么Java byte 类型的取值范围是-128~127
- java 集合中的Collections.sort()排序方法的源码分析(一)
- 手动安装Genymotion plugin for Eclipse
- java程序性能优化
- Java内存回收机制(转)
- 修改weblogic的jdk版本
- Java学习笔记---数组操作的10大方法
- java连接数据库工具类—JDBC
- java线程池
- java调用html模板发送html内容的邮件
- java 集合转数组
- 在eclipse中的类中每次都生成作者等信息注释
- hive查询,用到where方法时 报java.lang.NoSuchMethodError
- myeclipse 于 否update software 解
- java 用log4j输出日志信息
- java(继承体系)
- MyEclipse10安装checkStyle与findBugs插件--详细完美结局-费元星
- NetBeans中表格的增加、删除、修改