Spring三种注入方式

时间:2022-07-07 14:20:56
一、构造方法注入(死的应用)   通过constructor-arg指定构造方法的参数 Source.java
  1. package com.gary.test;
  2. public class Source {
  3. public void helloWorld(){
  4. System.out.println("Hello World!");
  5. }
  6. }
 Target.java
  1. package com.gary.test;
  2. public class Target {
  3. private Source source;
  4. public Target(Source source){
  5. this.source = source;
  6. }
  7. public void sayHelloWorld(){
  8. source.helloWorld();
  9. }
  10. }
applicationContext.xml 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  7. <bean id="source" class="com.gary.test.Source" />
  8. <bean id="target" class="com.gary.test.Target">
  9. <constructor-arg ref="source" />
  10. </bean>
  11. </beans>
TargetTest.java 
  1. package com.gary.test;
  2. import org.junit.AfterClass;
  3. import org.junit.BeforeClass;
  4. import org.junit.Test;
  5. import org.springframework.beans.factory.BeanFactory;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class TargetTest {
  8. static BeanFactory factory = null;
  9. static Target target = null;
  10. @BeforeClass
  11. public static void setUpBeforeClass() throws Exception {
  12. try{
  13. factory = new ClassPathXmlApplicationContext("applicationContext.xml");
  14. target = (Target) factory.getBean("target");
  15. }catch(Exception e){
  16. e.printStackTrace();
  17. }
  18. }
  19. @AfterClass
  20. public static void tearDownAfterClass() throws Exception {
  21. }
  22. @Test
  23. public void testSayHelloWorld() {
  24. target.sayHelloWorld();
  25. }
  26. }
二、getter()和setter()方法注入(比较常用)   通过访问器方法(set)注入此方法较为常用.在目标类中,定义要注入的属性,并添加访问器方法,则spring会自动注入.Source.java
  1. package com.gary.test;
  2. public class Source {
  3. public void helloWorld(){
  4. System.out.println("Hello World!");
  5. }
  6. }
Target.java
  1. package com.gary.test;
  2. public class Target {
  3. private Source source;
  4. public void setSource(Source source) {
  5. this.source = source;
  6. }
  7. public Source getSource() {
  8. return source;
  9. }
  10. public void sayHelloWorld(){
  11. getSource().helloWorld();
  12. }
  13. }
applicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
  7. default-autowire="byType">
  8. <bean id="source" class="com.gary.test.Source" />
  9. <bean id="target" class="com.gary.test.Target">
  10. <!-- 在beans标签添加default-autowire="byType"属性后,下面这行可以省略 -->
  11. <!-- <property name="source" ref="source" /> -->
  12. </bean>
  13. </beans>
TargetTest.java 
  1. package com.gary.test;
  2. import org.junit.AfterClass;
  3. import org.junit.BeforeClass;
  4. import org.junit.Test;
  5. import org.springframework.beans.factory.BeanFactory;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class TargetTest {
  8. static BeanFactory factory = null;
  9. static Target target = null;
  10. @BeforeClass
  11. public static void setUpBeforeClass() throws Exception {
  12. try{
  13. factory = new ClassPathXmlApplicationContext("applicationContext.xml");
  14. target = (Target) factory.getBean("target");
  15. }catch(Exception e){
  16. e.printStackTrace();
  17. }
  18. }
  19. @AfterClass
  20. public static void tearDownAfterClass() throws Exception {
  21. }
  22. @Test
  23. public void testSayHelloWorld() {
  24. target.sayHelloWorld();
  25. }
  26. }
 三、接口方式注入(不推荐使用)   通过接口方式注入建议使用前两种方法Source.java
  1. package com.gary.test;
  2. public class Source {
  3. public void helloWorld(){
  4. System.out.println("Hello World!");
  5. }
  6. }
ISource.java
  1. package com.gary.test;
  2. public interface ISource {
  3. //方法名任意,方法参数是所依赖对象的类型
  4. public void injectSource(Source source);
  5. }
Target.java 
  1. package com.gary.test;
  2. public class Target implements ISource{
  3. private Source source;
  4. @Override
  5. public void injectSource(Source source) {
  6. this.source = source;
  7. }
  8. public void sayHelloWorld(){
  9. source.helloWorld();
  10. }
  11. }
applicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
  7. default-autowire="byType">
  8. <bean id="source" class="com.gary.test.Source" />
  9. <bean id="target" class="com.gary.test.Target" />
  10. </beans>
TargetTest.java 
  1. package com.gary.test;
  2. import org.junit.AfterClass;
  3. import org.junit.BeforeClass;
  4. import org.junit.Test;
  5. import org.springframework.beans.factory.BeanFactory;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. public class TargetTest {
  8. static BeanFactory factory = null;
  9. static Source source = null;
  10. static Target target = null;
  11. @BeforeClass
  12. public static void setUpBeforeClass() throws Exception {
  13. try{
  14. factory = new ClassPathXmlApplicationContext("applicationContext.xml");
  15. target = (Target) factory.getBean("target");
  16. source = (Source) factory.getBean("source");
  17. }catch(Exception e){
  18. e.printStackTrace();
  19. }
  20. }
  21. @AfterClass
  22. public static void tearDownAfterClass() throws Exception {
  23. }
  24. @Test
  25. public void testSayHelloWorld() {
  26. target.injectSource(source);
  27. target.sayHelloWorld();
  28. }
  29. }