一 setter方法注入
配置文件如下:
<bean id="helloAction" class="org.yoo.action.SpringSetterHelloAction"> <!-- setter injection using the nested <ref/> element --> <property name="helloservice"><ref bean="helloService"/></property> <!--可以不设置类型 --> <property name="name" value="yoo"></property> </bean>
action实现类中代码:
private IHelloService helloservice;private String name ; public void sayHello(){helloservice.sayHello(); System.out.println(this.name);} public void setHelloservice(IHelloService helloservice) {this.helloservice = helloservice;} public void setName(String name) {this.name = name;}
这里的name和helloservice都采用属性的setter方法注入。即类中设置一个全局属性,并对属性有setter方法,以供容器注入。
二 构造器注入
spring也支持构造器注入,也即有些资料中的构造子或者构造函数注入。
先看配置文件:
<!--普通构造器注入--> <bean id="helloAction" class="org.yoo.action.ConstructorHelloAction"> <!--type 必须为Java.lang.String 因为是按类型匹配的,不是按顺序匹配--> <constructor-arg type="java.lang.String" value="yoo"/> <!-- 也可以使用index来匹配--> <!--<constructor-arg index="1" value="42"/>--> <constructor-arg><ref bean="helloService"/> </constructor-arg> </bean>
action实现类中代码:
private HelloServiceImpl helloservice; private String name ; public SpringConstructorHelloAction(HelloServiceImpl helloservice,String name){this.helloservice = helloservice; this.name = name ;} @Overridepublic void sayHello() {helloservice.sayHello(); System.out.println(this.name);}
同样设置2个属性,然后在构造函数中注入。
三静态工厂注入
配置文件如下:
<!--静态工厂构造注入--> <!--注意这里的class 带factory-method参数--> <!--factory-method的值是FactoryHelloAction中的工厂方法名createInstance--> <bean id="helloAction" class="org.yoo.di.FactoryHelloAction" factory-method="createInstance"> <constructor-arg type="java.lang.String" value="yoo"/> <constructor-arg> <ref bean="helloService"/> </constructor-arg> </bean>
action实现类:
private HelloServiceImpl helloservice; private String name = null; private SpringFactoryHelloAction(String name ,HelloServiceImpl helloservice){this.helloservice = helloservice ; this.name = name ;} public static SpringFactoryHelloAction createInstance(String name ,HelloServiceImpl helloservice) {SpringFactoryHelloAction fha = new SpringFactoryHelloAction (name,helloservice); // some other operationsreturn fha;} @Overridepublic void sayHello() {helloservice.sayHello(); System.out.println(this.name);}
四 无配置文件注入(自动注入)
上面三种方法都需要编写配置文件,在spring2.5中还提供了不编写配置文件的ioc实现。需要注意的是,无配置文件指bean之间依赖,不基于配置文件,而不是指没有spring配置文件。
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.php.cn/"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="helloService" class="org.yoo.service.HelloServiceImpl"> </bean> <bean id="helloAction" class="org.yoo.action.AutowiredHelloAction"> </bean> </beans>
可见上面只是设置了helloService和helloAction两个bean,并没有设置helloAction对helloService的依赖。
另外b2909812a49c523281a899f85fa8455b是必须的,有此才支持自动注入。
action实现类:
/* * 属性自动装载 */ /* @Autowired private HelloService helloservice; @Override public void sayHello() { helloservice.sayHello(); 。
上面代码很简单,在属性上加上 @Autowired注释,spring会自动注入HelloService 。
同样也支持构造器和setteer方法的注入,如下:
构造器自动注入:
/* * 还可以使用构造函数设置 */ @Autowired public SpringAutowiredHelloAction(HelloServiceImpl helloservice){ this.helloservice = helloservice; }
setter方法自动注入:
/* * 也可以使用set方法设置 */ /* @Autowired public void setHelloservice(HelloService helloservice) { this.helloservice = helloservice; }
最后在spring的reference文档中有提到,如果不使用自动注入,尽量使用setter方法,一般通用的也是使用setter方法。
而使用自动注入还是配置文件方式,如果jdk低于1.5或者spring不是2.5,就只能够使用配置文件方式了。其它的就看实际项目选择了。
以上就是Spring Ioc-依赖注入的几种方式的详情介绍的内容,更多相关内容请关注PHP中文网(www.php.cn)!

JVM的工作原理是将Java代码转换为机器码并管理资源。1)类加载:加载.class文件到内存。2)运行时数据区:管理内存区域。3)执行引擎:解释或编译执行字节码。4)本地方法接口:通过JNI与操作系统交互。

JVM使Java实现跨平台运行。1)JVM加载、验证和执行字节码。2)JVM的工作包括类加载、字节码验证、解释执行和内存管理。3)JVM支持高级功能如动态类加载和反射。

Java应用可通过以下步骤在不同操作系统上运行:1)使用File或Paths类处理文件路径;2)通过System.getenv()设置和获取环境变量;3)利用Maven或Gradle管理依赖并测试。Java的跨平台能力依赖于JVM的抽象层,但仍需手动处理某些操作系统特定的功能。

Java在不同平台上需要进行特定配置和调优。1)调整JVM参数,如-Xms和-Xmx设置堆大小。2)选择合适的垃圾回收策略,如ParallelGC或G1GC。3)配置Native库以适应不同平台,这些措施能让Java应用在各种环境中发挥最佳性能。

Osgi,Apachecommonslang,JNA和JvMoptionsareeForhandlingForhandlingPlatform-specificchallengesinjava.1)osgimanagesdeppedendendencenciesandisolatescomponents.2)apachecommonslangprovidesitorityfunctions.3)

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Java代码可以在不同操作系统上无需修改即可运行,这是因为Java的“一次编写,到处运行”哲学,由Java虚拟机(JVM)实现。JVM作为编译后的Java字节码与操作系统之间的中介,将字节码翻译成特定机器指令,确保程序在任何安装了JVM的平台上都能独立运行。

Java程序的编译和执行通过字节码和JVM实现平台独立性。1)编写Java源码并编译成字节码。2)使用JVM在任何平台上执行字节码,确保代码的跨平台运行。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版
中文版,非常好用

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具