重温spring(一)搭建spring ioc开发环境

目录

项目结构

pom.xml

ICustomerService.java

 CustomerServiceImpl.java

InstanceFactory.java

StaticFactory.java

bean.xml

CustomerTest.java


idea建maven项目

项目结构

pom.xml

jar包:四个spring包,俩个日志包。spring-core,beans,context,expression,log4j,common-logging

 <properties>
    <org.springframework-version>4.3.3.RELEASE</org.springframework-version>
  </properties>
  
<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${org.springframework-version}</version>
  </dependency>

  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${org.springframework-version}</version>
</dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework-version}</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>${org.springframework-version}</version>
  </dependency>

  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>

  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
  </dependency>
 <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>
</dependencies>

ICustomerService.java

public interface ICustomerService {
  public void save();
}

 CustomerServiceImpl.java

public class CustomerServiceImpl implements ICustomerService {
//    private ICustomerDao customerDao = null;

//    测试延迟加载,以及创建方式(加参数报错)
  public CustomerServiceImpl() {
    System.out.println("bean创建");
  }

  public void save() {
      System.out.println("业务层");
//      customerDao.save();
    }
}

InstanceFactory.java

public class InstanceFactory {

  public ICustomerService getCustomerService() {
    return new CustomerServiceImpl();
  }
}

StaticFactory.java

public class StaticFactory {
    public static ICustomerService getCustomerService() {
      return new CustomerServiceImpl();
    }
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--下载spring-framework-4.3.3.RELEASE-dist.zip ,spring-framework-4.3.3.RELEASE-dist/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html里-->
<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.springframework.org/schema/beans/spring-beans.xsd">

  <!-- bean definitions here,对象创建交由spring -->
  <!--class.forName().newInstance-->
  <bean id="customerService" class="com.luobo.service.impl.CustomerServiceImpl"></bean>
  <bean id="customerDao" class="com.luobo.dao.impl.CustomerDaoImpl"></bean>

<!--配置使用静态工厂创建bean对象-->
  <bean id = "staticCustomerService" class="com.luobo.factory.StaticFactory" factory-method="getCustomerService"></bean>

  <!--配置使用实例工厂创建bean对象-->
  <bean id="instanceFactory" class="com.luobo.factory.InstanceFactory"></bean>
  <bean id = "instanceCustomerService" factory-bean="instanceFactory" factory-method="getCustomerService"></bean>
</beans>

CustomerTest.java

public class CustomerTest {

    /**
     * bean创建俩种规则:
     *      BeanFactory:提供延迟加载来创建bean对象。什么时候用什么时候创建
     *      ApplicationContext:提供立即加载来创建bean对象。
     *
     * Ctrl + H:查看该接口的层级结构
     *
     * bean创建的三种方式:
     *      1.调用无参构造函数创建 若无无参构造,则创建失败报异常
     *      2.使用静态工厂中的方法创建对象
     *      3.使用实例工厂创建对象
     */
    @Test
    public void test() {
        //1.获取容器
//        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        Resource rs = new ClassPathResource("bean.xml");
        BeanFactory ac = new XmlBeanFactory(rs);
        //2.根据bean的id获取对象
//        ICustomerService cs = (ICustomerService) ac.getBean("customerService");
//        ICustomerService cs = (ICustomerService) ac.getBean("staticCustomerService");
        ICustomerService cs = (ICustomerService) ac.getBean("instanceCustomerService");
        ICustomerDao cd = (ICustomerDao) ac.getBean("customerDao");
        System.out.println(cs);
        System.out.println(cd);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值