spring通过注解依赖注入和获取xml配置混合的方式
2014-10-02 17:07
776 查看
spring的xml配置文件中某个<bean></bean>中的property的用法是什么样的?
/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java
/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml
void org.springframework.beans.factory.xml.XmlBeanCollectionTests.testRefSubelement() throws Exception
<bean id="jenny" class="org.springframework.tests.sample.beans.TestBean">
<property name="name"><value>Jenny</value></property>
<property name="age"><value>30</value></property>
<property name="spouse">
<!-- Could use id and href -->
<ref local="david"/>
</property>
</bean>
<bean id="david" class="org.springframework.tests.sample.beans.TestBean">
<description>
Simple bean, without any collections.这里是注释,spring不会解析
</description>
<property name="name">
<description>The name of the user</description>
<value>David</value>
</property>
<property name="age"><value>27</value></property>
</bean>
上面的蓝色属性值,spring通过set方法注入值。
org.springframework.tests.sample.beans.TestBean这个类里面必须有上面属性的get和set方法,比如
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
<property name="description"><value>Simple bean, without any collections.ok</value></property>
这种方法可以获取description的值。
/spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlBeanCollectionTests.java
/spring-beans/src/test/resources/org/springframework/beans/factory/xml/collections.xml
void org.springframework.beans.factory.xml.XmlBeanCollectionTests.testRefSubelement() throws Exception
<bean id="jenny" class="org.springframework.tests.sample.beans.TestBean">
<property name="name"><value>Jenny</value></property>
<property name="age"><value>30</value></property>
<property name="spouse">
<!-- Could use id and href -->
<ref local="david"/>
</property>
</bean>
<bean id="david" class="org.springframework.tests.sample.beans.TestBean">
<description>
Simple bean, without any collections.这里是注释,spring不会解析
</description>
<property name="name">
<description>The name of the user</description>
<value>David</value>
</property>
<property name="age"><value>27</value></property>
</bean>
上面的蓝色属性值,spring通过set方法注入值。
org.springframework.tests.sample.beans.TestBean这个类里面必须有上面属性的get和set方法,比如
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
<property name="description"><value>Simple bean, without any collections.ok</value></property>
这种方法可以获取description的值。
相关文章推荐
- 原始方式:spring四种依赖注入方式 |注解方式:Spring零配置通过注解实现Bean依赖注入
- MyBatis+Spring在注解@Autowried后通过反射的方式调用方法获取注入的Service或DAO对象为空
- Spring IOC 依赖注入的两种方式XML和注解
- Spring IOC 依赖注入的两种方式:XML和注解
- Spring 配置bean, 注解方式配置 xml方式要生产set方法方便属性注入,注解方式不需要,利用反射机制注入
- Spring零配置通过注解实现Bean依赖注入总结<转>
- Spring4深入理解IOC&DI04----Bean配置方式(全类名,工厂方法,FactoryBean),配置形式(基于XML和注解),泛型依赖注入
- Spring通过XML配置c3p0连接池和dao层注解注入使用 jdbcTemplate
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- spring中读取xml配置文件、获取bean的几种方式
- spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)
- web.xml中配置spring的几种方式 以及 Spring获取Bean的几种方式
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- Spring 3.0.5 MVC 基于注解ehcache.xml 配置方式
- spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)
- web.xml 通过contextConfigLocation配置spring 的方式
- spring aop的使用(注解方式以及基于xml配置方式)
- spring中读取xml配置文件、获取bean的几种方式
- spring aop的使用(注解方式以及基于xml配置方式)
- spring+hibernate xml和注解方式配置事务