spring揭秘-概念以BeanFactory介绍

2018-01-22  本文已影响161人  liangxifeng833

本质:Spring框架为POJO提供的各种服务共同组成了Spring的生命之树


Paste_Image.png

spring框架为基础,有很多家庭成员, 比如(Spring Web Flow, Spring Web Services, Spring Security, Spring Batch等等), 这些家族成员全部以Apache Lisence Version 2.0协议发布,共同组成了Spring Projects组合, 因为Spring Projects组合是开源产品,需要社区推动和发展, 活跃的开发社区可以为我们带来快速的反馈和支持,但没有任何主体或个人可以保证我们所需要的反馈和支持能够及时有效地得到满足。鉴于这一点, SpringSource
(原来的Interface21,即Spring框架的“东家”)在Spring Projects组合的基础上,提供了Spring Portfolio 产品,SpringSource为Spring Portfolio产品中的各成员提供咨询、培训和支持服务,Spring Portfolio只是SpringSource产品组合之一

IoC的理念就是,让别人为你服务, 也就是让IoC Service Provider来为你服务, 其实IoC就这么简单!原来是需要什么东西自己去拿,现在是需要什么东西就让别人送过来

Paste_Image.png Paste_Image.png

依赖注入的三种方式:

  1. 首先声明人和箭的接口
    Person
package spring.read.note.ioc;
/**
 * 人接口
 * @author lxf
 */
public interface Person {
    public void hunt();
}

Arrow

/**
 * 弓箭接口
 * @author lxf
 */
public interface Arrow {
    public String getArrow();
}

(1)在PersonImpl实现类中配置构造方法

public class PersonImpl implements Person {
    private Arrow arrow;
    public PersonImpl() {}
    public PersonImpl(Arrow arrow) {
        this.arrow = arrow;
    }
    @Override
    public void hunt() {
        System.out.println("I get " + arrow.getArrow() + " to hunt.");
    }   
}

(2)配置spring配置文件

    <bean id="ArrowImpl" class="iocdi.ArrowImpl"/>
    
    <bean id="PersonImpl" class="iocdi.PersonImpl">
        <!-- 构造注入 -->
        <constructor-arg ref="ArrowImpl"/>
    </bean>

1.配置PersonImpl人的实现类

package spring.read.note.ioc;
/**
 * 人实现类
 * @author lxf
 */
public class PersonImpl implements Person{
    private Arrow arrow;
    @Override
    public void hunt() {
        System.out.println("I get " + arrow.getArrow() + " to hunt!");
    } 
    //set注入一支箭
    public void setArrow(Arrow arrow)
    {
        this.arrow = arrow;
    }
}

2.配置spring配置文件

    <bean id="arrowImpl" class="spring.read.note.ioc.ArrowImpl"></bean>
    <bean id="personImpl" class="spring.read.note.ioc.PersonImpl">
        <!-- setter注入 -->
        <property name="arrow" ref="arrowImpl"/>
    </bean>  
  1. testMain测试
public class TestMain {
    public static void main(String[] args) {
        // 得到ApplicationContext对象
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-read-note.xml");   
        // 得到Bean
        Person person = (Person)ctx.getBean("personImpl");
        person.hunt();
     }
}

输出:

I get an arrow to hunt.
public void hunt() {
            Object obj = Class.forName("spring.read.note.ioc.ArrowImpl").newInstance();
            arrow = (Arrow) obj;
            System.out.println("I get " + arrow.getArrow() + " to hunt.");
}

testMain

            Object obj = Class.forName("spring.read.note.ioc.PersonImpl").newInstance();
            Person p = (Person) obj;
            System.out.println("-----------------接口注入-----------------");
            p.hunt();

推荐使用setter和构造方法注入

IoC Service Provider 的职责

Spring的IoC容器之BeanFactory

Paste_Image.png Paste_Image.png

BeanFactory 的对象注册与依赖绑定方式

1.直接编码方式


Paste_Image.png Paste_Image.png

2.外部配置文件方式

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ➥
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="djNewsProvider" class="..FXNewsProvider">
    <constructor-arg index="0">
      <ref bean="djNewsListener"/>
    </constructor-arg>
   <constructor-arg index="1">
      <ref bean="djNewsPersister"/>
   </constructor-arg>
  </bean>
  <bean id="djNewsListener" class="..impl.DowJonesNewsListener">
  </bean>
  <bean id="djNewsPersister" class="..impl.DowJonesNewsPersister">
  </bean>
</beans>
public static void main(String[] args)
{
  DefaultListableBeanFactory beanRegistry = new   DefaultListableBeanFactory();
  BeanFactory container = (BeanFactory)bindViaXMLFile(beanRegistry);
FXNewsProvider newsProvider = ➥
(FXNewsProvider)container.getBean("djNewsProvider");
newsProvider.getAndPersistNews();
}
public static BeanFactory bindViaXMLFile(BeanDefinitionRegistry registry)
{
  XmlBeanDefinitionReader reader = new     XmlBeanDefinitionReader(registry);
  reader.loadBeanDefinitions("classpath:../news-config.xml");
  return (BeanFactory)registry;
  // 或者直接
  //return new XmlBeanFactory(new ClassPathResource("../news-config.xml"));
}

3.注解方式

@Component
public class FXNewsProvider
{
  @Autowired
  private IFXNewsListener newsListener;
  @Autowired
  private IFXNewsPersister newPersistener;
}
<context:component-scan base-package="cn.spring21.project.base.package"/>
public static void main(String[] args)
{
  ApplicationContext ctx = new ClassPathXmlApplicationContext("配置文件路径");
  FXNewsProvider newsProvider = (FXNewsProvider)container.getBean("FXNewsProvider");
  newsProvider.getAndPersistNews();
}
上一篇下一篇

猜你喜欢

热点阅读