Spring学习Ⅰ

Spring IoC

jar包

jar

工程目录

在这里插入图片描述

配置文件

applicationContext.xml

单例配置文件
<?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.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 配置service
        <bean> 配置需要创建的对象
            id :用于之后从spring容器获得实例时使用的
            class :需要创建实例的全限定类名
    -->
    <bean id="userServiceId" class="UserServiceImpl"></bean>
</beans>
多例配置文件
<?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.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 配置service
        <bean> 配置需要创建的对象
            id :用于之后从spring容器获得实例时使用的
            class :需要创建实例的全限定类名
    -->
    <bean id="userServiceId" class="UserServiceImpl" scope="prototype"></bean>
</beans>

代码

public interface UserService {
    void addUser();
}
public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("im xieyaoyan");
    }
}

测试文件

单例

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Execute {
    @Test
    public void demo02(){
        //从spring容器获得
        //1 获得容器
        String xmlPath = "resources/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        //2获得内容 --不需要自己new,都是从spring容器获得
        UserService userService1 = (UserService) applicationContext.getBean("userServiceId");
        UserService userService2 = (UserService) applicationContext.getBean("userServiceId");

        System.out.println("userService1 = " + userService1);
        System.out.println("userService2 = " + userService2);
        userService1.addUser();

    }
}

结果展示

多例

在这里插入图片描述

单例

在这里插入图片描述


Spring DI

jar包

jar

工程目录

在这里插入图片描述

配置文件

applicationContext.xml

<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">
    <!--
        <property> 用于进行属性注入
            name: bean的属性名,通过setter方法获得
                setBookDao ##> BookDao  ##> bookDao
            ref :另一个bean的id值的引用
     -->

    <!-- 创建service -->
    <bean id="bookServiceId" class="BookServiceImpl">
        <property name="bookDao" ref="bookDaoId"></property>
    </bean>

    <!-- 创建dao实例 -->
    <bean id="bookDaoId" class="BookDaoImpl"></bean>


</beans>

代码

public interface BookDao {

    public void addBook();

}
public class BookDaoImpl implements BookDao {

    @Override
    public void addBook() {
        System.out.println("study DI");
    }

}
public interface BookService {

    public abstract void addBook();

}
public class BookServiceImpl implements BookService {

    // 方式2:接口 + setter
    private BookDao bookDao;
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    @Override
    public void addBook(){
        this.bookDao.addBook();
    }

}

测试文件

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Execute {
    @Test
    public void demo02(){
        //从spring容器获得
        //1 获得容器
        String xmlPath = "resources/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        BookService bookService = (BookService) applicationContext.getBean("bookServiceId");

        bookService.addBook();

    }
}

结果展示

在这里插入图片描述


Spring工厂

jar包

jar

工程目录

在这里插入图片描述

配置文件

applicationContext.xml

<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 id="myBeanFactoryId" class="MyBeanFactory"></bean>
    <!-- 获得userservice
        * factory-bean 确定工厂实例
        * factory-method 确定普通方法
    -->
    <bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean>

</beans>

代码

/**
 * 实例工厂,所有方法非静态
 *
 */
public class MyBeanFactory {

    /**
     * 创建实例
     * @return
     */
    public UserService createService(){
        return new UserServiceImpl();
    }

}
public interface UserService {
    void addUser();
}
public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("factory");
    }
}

测试文件

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Execute {
    @Test
    public void demo02(){
        //从spring容器获得
        //1 获得容器
        String xmlPath = "resources/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        UserService userService = applicationContext.getBean("userServiceId" ,UserService.class);
        userService.addUser();

    }
}

结果展示

在这里插入图片描述


静态Spring工厂

jar包

jar

工程目录

配置文件

applicationContext.xml

<?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.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 将静态工厂创建的实例交予spring
          class 确定静态工厂全限定类名
          factory-method 确定静态方法名
      -->
    <bean id="userServiceId" class="MyBeanFactory" factory-method="createServiceForStatic"></bean>

</beans>

代码

/**
 * 实例工厂,所有方法非静态
 *
 */
public class MyBeanFactory {

    /**
     * 创建实例
     * @return
     */
    public UserService createService(){
        return new UserServiceImpl();
    }

    /**
     * 创建实例
     * @return
     */
    public static UserService createServiceForStatic(){
        return new UserServiceImpl();
    }


}
public interface UserService {
    void addUser();
}
public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("this is static call");
    }
}

测试文件

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Execute {
    @Test
    public void demo02(){
        //从spring容器获得
        //1 获得容器
        String xmlPath = "resources/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        UserService userService = applicationContext.getBean("userServiceId" ,UserService.class);
        userService.addUser();
    }
}

结果展示

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值