Spring入门学习——从对象属性中声明Bean
来源:互联网 发布:我的世界透视矿物js 编辑:程序博客网 时间:2024/05/20 04:51
package com.cgy.springrecipes.shop;
public class ProductRanking {
private Product bestSeller;
public void setBestSeller(Product bestSeller) {
this.bestSeller = bestSeller;
}
public Product getBestSeller() {
return bestSeller;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="productRanking"
class="com.cgy.springrecipes.shop.ProductRanking">
<property name="bestSeller">
<bean class="com.cgy.springrecipes.shop.Disc">
<property name="name" value="taylor"/>
<property name="price" value="1.5"/>
</bean>
</property>
</bean>
<bean id="bestSeller"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject" ref="productRanking"/>
<property name="propertyPath" value="bestSeller"/>
</bean>
</beans>
package com.cgy.springrecipes.shop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans_two.xml");
//Product kingston = (Product) context.getBean("battery");
//Product taylor = (Product) context.getBean("disc");
//System.out.println(kingston);
//System.out.println(taylor);
Product bestSeller = (Product) context.getBean("bestSeller");
System.out.println(bestSeller);
}
}
使用<util:property-path>标记需要指定一个id(就是bean的id),还有一个path属性,写法正如开头说的一样,对象属性,但是这个与对象属性写法有点差别,因为是 BeanId.property,写法上类似 对象.属性。<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="productRanking"
class="com.cgy.springrecipes.shop.ProductRanking">
<property name="bestSeller">
<bean class="com.cgy.springrecipes.shop.Disc">
<property name="name" value="taylor"/>
<property name="price" value="1.5"/>
</bean>
</property>
</bean>
<util:property-path id="bestSeller" path="productRanking.bestSeller"/>
</beans>
- Spring入门学习——从对象属性中声明Bean
- Spring入门学习——从静态字段中声明Bean
- Spring入门学习——bean属性配置(一)
- Spring入门学习——bean属性配置(二)
- 学习笔记——Spring 中Bean属性的注入
- spring bean 声明学习
- spring 从bean实例中获取对象
- (学习笔记)spring框架入门之配置bean(处理属性中有集合的情况)
- Spring入门学习——从Classpath中扫描组件
- 从bean中获取对象的属性值
- spring bean对象的声明周期
- Spring学习笔记四: 从Spring容器中获取Bean
- spring mvc 从bean中自动获取对象
- Spring入门学习——指定Bean引用
- Spring入门学习——继承Bean配置
- Spring入门学习——设置Bean作用域
- Spring入门学习——使用Bean感知容器
- Spring入门学习——创建Bean后处理器
- Spring入门学习——调用一个实例工厂方法创建Bean
- 从最容易理解的图到SysML和UML
- iOS开发之Masonry框架源码深度解析
- Spring入门学习——从静态字段中声明Bean
- 为何需要SysML以及系统思维?以及…
- Spring入门学习——从对象属性中声明Bean
- WebSocket服务器的几次改进
- Spring入门学习——使用Spring表达式语言
- Swing做界面还是相对好用。对于界…
- Spring入门学习——设置Bean作用域
- ant打包程序依赖外部jar包
- 用SysML做的编程技巧总结图
- Spring入门学习——自定义Bean初始化和析构
- 社科方面,许多人觉得直觉比数据研…