项目中用到需要初始化一些数据,Spring提供了filed的值注入和method的返回值注入。
一、Field值的注入
filed值注入需要使用org.springframework.beans.factory.config.FieldRetrievingFactoryBean来获取类的静态变量。
例如,我们通常在接口中定义常量:
package com.wy.inject; /** * * @author wy * */ public interface Fruit { public String APPLE = "苹果"; public String ORANGE = "桔子"; }
下面利用FieldRetrievingFactoryBean获取CarBandType接口中定义的常量,并注入到某个bean的属性中:
<?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:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!-- 1、通过注入属性 <bean id="appleBean" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField" value="com.wy.inject.Fruit.APPLE" /> </bean> <bean id="apple" class="com.wy.inject.Apple"> <property name="name" ref="appleBean" /> </bean>-->
Spring还允许用户用常量的全限定名作为FieldRetrievingFactoryBean的id,其效果和通过配置staticField属性是一样的:
<!-- 2、通过全限量名称 <bean id="com.wy.inject.Fruit.APPLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> </bean> <bean id="apple" class="com.wy.inject.Apple"> <property name="name" ref="com.wy.inject.Fruit.APPLE" /> </bean> -->
当然,也可以直接将FieldRetrievingFactoryBean以内置bean的方式对属性进行赋值,这样在配置上更紧凑一些:
<!-- 3、通过内置Bean的方式 <bean id="apple" class="com.wy.inject.Apple"> <property name="name"> <bean id="com.wy.inject.Fruit.APPLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/> </property> </bean>-->
二、Method返回值的注入
Method返回值的注入需要使用MethodInvokingFactoryBean来完成。
在xml配置中,需要设定targetObject和targetMethod来指定目标bean和方法。如果使用静态方法,则需要指定targetClass和targetMethod。
1、非静态方法配置文件如下:
<!-- 1、Method返回值的注入 <bean id="apple" class="com.wy.inject.Apple"> <property name="name"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="cons" /> </property> <property name="targetMethod"> <value>getAPPLE</value> </property> </bean> </property> </bean> <bean id="cons" class="com.wy.inject.Cons" /> -->
2、静态方法的配置
<!-- 2、静态方法返回值的注入 --> <bean id="banna" class="com.wy.inject.Banna"> <property name="name"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass"> <value>com.wy.inject.Cons</value> </property> <property name="targetMethod"> <!-- getBann必须是静态方法 --> <value>getBann</value> </property> </bean> </property> </bean>
涉及到的类:
Apple.java
package com.wy.inject; /** * * @author wy * */ public class Apple { String name = null; String price = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
Cons.java
package com.wy.inject; /** * * @author wy * */ public class Cons { public String APPLE = "苹果"; public String ORANGE = "桔子"; public static String Bann = "香蕉"; public String getAPPLE() { return APPLE; } public void setAPPLE(String aPPLE) { APPLE = aPPLE; } public String getORANGE() { return ORANGE; } public void setORANGE(String oRANGE) { ORANGE = oRANGE; } public static String getBann() { return Bann; } public static void setBann(String bann) { Bann = bann; } }
Banna.java
package com.wy.inject; /** * * @author wy * */ public class Banna { String name = null; String price = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
三、测试例子
package com.wy.inject; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InjectTest { public Apple apple = null; public Banna banna = null; public InjectTest() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // apple = (Apple)context.getBean("apple"); banna = (Banna)context.getBean("banna"); } public void name(){ // System.out.println("Fruit name=" + apple.getName()); System.out.println("Fruit name=" + banna.getName()); } /** * @param args */ public static void main(String[] args) { new InjectTest().name(); } }
相关推荐
1.spring 普通属性注入 2.xml中 map集合标签与其他标签不通-- 3.用junit测试
Spring Boot系列四 Spring @Value 属性注入使用总结一
spring各种属性的注入 spring各种属性的注入 spring各种属性的注入
Spring注解注入属性
实例主要讲述了,Spring的Xml(list,set,map)怎样进行注入值
知识共享-Spring注入属性值案例(雷惊风).
主要为大家详细介绍了Spring boot工具类静态属性注入,及多环境配置详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Spring中的方法注入方式.Spring中的方法注入方式.
Spring属性注入:关于Spring的属性注入一共有两种方式,分别为XML方式与注解方式,其中XML方式又分为Set注入与构造方法注入,注解方式分为java注解与Spring注解
在spring中,当属性是由工厂方法创造的,而不是用new关键字实例化的,就需要在applicationContext.xml配置工厂方法,是spring容器为我们自动注入属性
一个spring设值注入的例子,自己也是初学者。希望高手都知道啊……
NULL 博文链接:https://364232252.iteye.com/blog/2369889
我们学习了spring框架spring框架里面有3个优势第一个是轻量级的IOC也叫控制反转后来改名为DI也叫依赖注入,依赖注入里面有3中注入方法分别是set注入,构造器注入,注解注入,我传的是set注入的视频
Spring IOC之方法注入 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45022649
主要给大家介绍了关于spring boot中几种注入方法的一些个人看法,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
Spring.net框架属性注入+用户单点登录原理+memcached缓存,可以用于学习和研究,很好的例子,欢迎大家下载学习
该演示项目是在“Spring MVC使用篇”系列文章之前的项目工程的基础上搭建的,因此依赖的基础jar包于第二篇文章中已经介绍过了(点击链接即可查看:Spring MVC使用篇(二)—— 环境搭建),这里便不再赘述。
本篇文章主要介绍了Spring boot将配置属性注入到bean类中,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring注入方式 set注入 构造注入 自动注入
NULL 博文链接:https://wujiu.iteye.com/blog/2010397