Spring 依赖注入四种方式

- 日理万妓 2022-07-26 01:27 285阅读 0赞

纸上得来终觉浅

1.构造器注入

AnimalDao:

  1. package com.roadArchitectWeb.dao;
  2. public interface AnimalDao {
  3. /*所有动物有一个说话的方法*/
  4. public void say();
  5. }

CatDaoImpl:

  1. package com.roadArchitectWeb.dao;
  2. public class CatDaoImpl implements AnimalDao{
  3. /*这个时候并没有值,会有xml文件注入*/
  4. private String says;
  5. public void say(){
  6. System.out.println("CatDaoImpl.say():"+says);
  7. }
  8. /*带参数,方便利用构造器进行注入*/
  9. public CatDaoImpl(String says){
  10. this.says=says;
  11. }
  12. }

AnimalMain:

  1. package com.roadArchitectWeb.dao;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. class AnimalSay1{
  5. private AnimalDao animalDao;
  6. public void setAnimalDao(AnimalDao animalDao) {
  7. this.animalDao = animalDao;
  8. }
  9. public void Say(){
  10. animalDao.say();
  11. }
  12. public AnimalSay1(AnimalDao animalDao){
  13. this.animalDao=animalDao;
  14. }
  15. }
  16. public class AnimalMain {
  17. public static void main(String[] args) {
  18. /*创建Spring的IOX容器对象*/
  19. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  20. /*从IOC容器中获取Bean实例*/
  21. AnimalSay1 animalSay1 = (AnimalSay1)ctx.getBean("AnimalSay1");
  22. /*调用其中的say方法*/
  23. animalSay1.Say();
  24. }
  25. }

ApplicationContext.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="CatDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl">
  6. <constructor-arg value="喵喵喵"></constructor-arg>
  7. </bean>
  8. <bean id="AnimalSay1" class="com.roadArchitectWeb.dao.AnimalSay1">
  9. <constructor-arg ref="CatDaoImpl"></constructor-arg>
  10. </bean>
  11. </beans>

输出结果:

CatDaoImpl.say():喵喵喵

所谓构造器注入,即类中有一个构造方法,同时在配置文件中利用该构造方法进行注入,上述例子用了两次构造器注入。

2.setter方法注入

上一篇文章是setter方法的具体应用。

3.静态工厂

既然是静态工厂方式注入,首先新建一个工厂用于获取CatDaoImpl实例:

  1. package com.roadArchitectWeb.dao;
  2. public class DaoFactory {
  3. /*静态工厂方法*/
  4. public static AnimalDao getStaticInstance(String says){
  5. return new CatDaoImpl(says);
  6. }
  7. }

applicationContext.xml变为:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="CatDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl">
  6. <constructor-arg value="喵喵喵"></constructor-arg>
  7. </bean>
  8. <bean id="AnimalSay1" class="com.roadArchitectWeb.dao.AnimalSay1">
  9. <constructor-arg ref="CatDaoImpl"></constructor-arg>
  10. </bean>
  11. <bean id="DaoFactory" class="com.roadArchitectWeb.dao.DaoFactory" factory-method="getStaticInstance">
  12. <constructor-arg value="哇哇哇"></constructor-arg>
  13. </bean>
  14. </beans>

AnimalMain修改为:

  1. package com.roadArchitectWeb.dao;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. class AnimalSay1{
  5. private AnimalDao animalDao;
  6. public void setAnimalDao(AnimalDao animalDao) {
  7. this.animalDao = animalDao;
  8. }
  9. public void Say(){
  10. animalDao.say();
  11. }
  12. public AnimalSay1(AnimalDao animalDao){
  13. this.animalDao=animalDao;
  14. }
  15. }
  16. public class AnimalMain {
  17. public static void main(String[] args) {
  18. /*创建Spring的IOX容器对象*/
  19. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  20. /*从IOC容器中获取Bean实例*/
  21. AnimalSay1 animalSay1 = (AnimalSay1)ctx.getBean("AnimalSay1");
  22. /*调用其中的say方法*/
  23. animalSay1.Say();
  24. AnimalDao animalDao = (AnimalDao)ctx.getBean("DaoFactory");
  25. animalDao.say();
  26. }
  27. }

结果为:

CatDaoImpl.say():喵喵喵
CatDaoImpl.say():哇哇哇

所谓静态工厂,不同于构造函数注入和setter方法注入,它通过factory-method指定方法,从而获得实例的引用。

4.实例工厂

同静态工厂相同,只是新增一个工厂DaoInstanceFactory:

  1. package com.roadArchitectWeb.dao;
  2. public class DaoInstanceFactory {
  3. /*实例工厂方法*/
  4. public AnimalDao getStaticInstance(String says){
  5. return new CatDaoImpl(says);
  6. }
  7. }

applicationContext.xml变为:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="CatDaoImpl" class="com.roadArchitectWeb.dao.CatDaoImpl">
  6. <constructor-arg value="喵喵喵"></constructor-arg>
  7. </bean>
  8. <bean id="AnimalSay1" class="com.roadArchitectWeb.dao.AnimalSay1">
  9. <constructor-arg ref="CatDaoImpl"></constructor-arg>
  10. </bean>
  11. <bean id="DaoFactory" class="com.roadArchitectWeb.dao.DaoFactory" factory-method="getStaticInstance">
  12. <constructor-arg value="哇哇哇"></constructor-arg>
  13. </bean>
  14. <bean id="DaoInstanceFactory" class="com.roadArchitectWeb.dao.DaoFactory" factory-method="getStaticInstance">
  15. <constructor-arg value="呜呜呜"></constructor-arg>
  16. </bean>
  17. </beans>

AnimalMain修改为:

  1. package com.roadArchitectWeb.dao;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. class AnimalSay1{
  5. private AnimalDao animalDao;
  6. public void setAnimalDao(AnimalDao animalDao) {
  7. this.animalDao = animalDao;
  8. }
  9. public void Say(){
  10. animalDao.say();
  11. }
  12. public AnimalSay1(AnimalDao animalDao){
  13. this.animalDao=animalDao;
  14. }
  15. }
  16. public class AnimalMain {
  17. public static void main(String[] args) {
  18. /*创建Spring的IOX容器对象*/
  19. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  20. /*从IOC容器中获取Bean实例*/
  21. AnimalSay1 animalSay1 = (AnimalSay1)ctx.getBean("AnimalSay1");
  22. /*调用其中的say方法*/
  23. animalSay1.Say();
  24. AnimalDao animalDao = (AnimalDao)ctx.getBean("DaoFactory");
  25. animalDao.say();
  26. AnimalDao animalDao2 = (AnimalDao)ctx.getBean("DaoInstanceFactory");
  27. animalDao2.say();
  28. }
  29. }

结果为:

CatDaoImpl.say():喵喵喵
CatDaoImpl.say():哇哇哇
CatDaoImpl.say():呜呜呜

发表评论

表情:
评论列表 (有 0 条评论,285人围观)

还没有评论,来说两句吧...

相关阅读

    相关 spring依赖注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注

    相关 spring依赖注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理。 spring提出了依赖