Spring基础——ioc

IOC底层原理的学习

将传统通过new创建对象类 变成 交给Spring配置来创建对象类

目录


1. ioc底层原理

(1)实现过程
  • 创建xml配置文件,配置要创建的对象类
  • 创建工厂类,使用dom4j解析配置文件+反射
    根据bean里的id得到对应的class属性;使用反射创建类对象
  • 使用,在类中直接调用工厂类的方法
    例如:Factory.getClazz();
    高内聚,低耦合
    在使用时,我们只需做配置就可以
(2)入门案例
  • 导入jar包
    commons-logging-1.2.jar
    log4j-1.2.17.jar
    spring-beans-4.3.16.RELEASE.jar
    spring-context-4.3.16.RELEASE.jar
    spring-core-4.3.16.RELEASE.jar
    spring-expression-4.3.16.RELEASE.jar
  • 创建Spring配置文件呢,配置创建类
    在项目src下面,创建文件applicationContext.xml,引入schema约束
    注意xml文件在应用时的名称一致;bean的id自定义;class为类的全路径
<?xml version="1.0" encoding="UTF-8"?>
<!-- 1.引入schema约束 -->
<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">
    <!-- 2.配置对象创建 -->
    <bean id="userBean1" class="com.smxy.lj.ioc.User">
                ...
    </bean>
</beans> 
  • 写代码测试
//1.加载spring的配置文件,据此来创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.得到配置创建的对象
User user = (User)context.getBean("userBean2");
//输出类的地址
System.out.println(user);

2.bean标签注入————————配置文件方式

注入 即 创建对象时候,给类里的属性里面设置值。

(1)普通注入方式(2种)

以User来学习

class User {
    private String username;
    private String passwd;
    //有参构造方法
    public User(String username, String passwd) {
        super();
        this.username = username;
        this.passwd = passwd;
    }
   //set方法
    public void setUsername(String username) {
        this.username = username;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", passwd=" + passwd + "]";
    }
}
  • set方法注入(最常用)
<!-- applicationContext.xml文件中 -->
<bean id="bean1" class="com.smxy.lj.User">
<!-- name要和User类中的属性一致 -->
     <property name=”username” value="小林子"></property >
     <property  name=”passwd” value="123456"></property >
</bean>
//测试代码
//1.加载spring的配置文件,据此来创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.得到配置创建的对象
User user = (User)context.getBean("bean1");
//3.输出,看是否注入成功
System.out.println(user.toString());
  • 构造有参构造方法注入
<!-- applicationContext.xml文件中 -->
<bean id="bean2" class="com.smxy.lj.User">
     <constructor-arg name=”username” value="张三四"></constructor-arg>
     <constructor-arg name=”passwd” value="123456"></constructor-arg>
</bean>
//测试代码
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)context.getBean("bean2");
System.out.println(user.toString());
(2)特殊的注入方式——注入对象类型属性

例如:在servic类中得到dao类中的

class UserDao{
//测试方法
  public void add(){
      Ststem.out.printf("dao___:"+);
  }
}
/*代替传统的创建类方法
 UserDao dao = new UserDao();
 dao.add();*/
class UserService{
//1.定义dao类型属性
  private UserDao userDao;
//2.生成set方法
  public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
  }
 public void add(){
      Ststem.out.printf("dao___:");
      userDao.add();
  }
}
<--!3.注入对象属性类型-->
<bean id="userDao" class="com.smxy.lj.UserDao"></bean>
<bean id="userService" class="com.smxy.lj.userService">
<!--注入dao对象 -->
<!--name:service类中类的属性名;ref(引用):dao配置bean标签中id值 -->
     <property name=”userDao” ref="userDao"></property >
</bean>
//Test类
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service= (UserService )context.getBean("userService");
UserService.add();
(3)注入复杂类型属性

注入数组,List集合,Map集合,Properties类型

public class PropertyTest {
    private String[] arrs;
    private List<String> list;
    private Map<String, String> map;
    private Properties properties;

    public void setList(List<String> list) {
        this.list = list;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public void setArrs(String[] arrs) {
        this.arrs = arrs;
    }
    public void test(){
        System.out.println("arrs:"+arrs);
        System.out.println("list:"+list);
        System.out.println("map:"+map);
        System.out.println("properties"+properties);
    }

}
    <bean id="property" class="com.smxy.lj.ioc.PropertyTest">
        <property name="arrs">
            <list>
                <value>arr1</value>
                <value>arr2</value>
            </list>
        </property>
        <!-- list -->
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
            </list>
        </property>
        <!-- map -->
        <property name="map">
            <map>
                <entry key="map1" value="1"></entry>
                <entry key="map2" value="2"></entry>
            </map>
        </property>
        <!-- properties -->
        <property name="properties">
            <props>
                <prop key=”driverclass”>com.mysql.jdbc.Driver</prop>
            <prop key="username">root</prop>
            </props>
        </property>
    </bean>
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
PropertyTest p = (PropertyTest) context.getBean("property");
p.test();

3.bean标签注入————————注解方式

(1)普通注入方式(2种)
  • 00
  • 00
  • 00
  • 00
(1)普通注入方式(2种)

4. ioc和DI区别

  • ioc:控制反转。把对象创建交给spring。
  • DI:依赖注入。向类里面的属性设置值。
  • 关系:DI不能单独存在,要在ioc的基础上完成操作,不能单独存在(类建立了才可以注入属性)

`

猜你喜欢

转载自blog.csdn.net/qq_37971615/article/details/80232607
今日推荐