spring-1-IoC

2020-06-22  本文已影响0人  blank_white

一、准备工作

环境

IntelliJ Idea
Maven

pom.xml 中添加依赖

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.16.RELEASE</version>
    </dependency>
  </dependencies>

二、IoC

Inversion of Control:控制反转

接口名  A = new 实现接口的类();
"server" 是需要  com.Server 类对象
"server2" 是需要  com.Server2 类对象
"server3" 是需要 com.Server3 类对象

在代码中需要使用对象的时候

接口名 A = Spring对象.获取名字叫xx的对象("server");

对象的创建委托给 Spring 框架,从而实现对对象的一些特殊处理的统一控制,更改对象的时候也只需改配置文件,不需要改代码

三、简单配置使用

spring 的 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">
</beans>

配置创建对象,默认是单例模式

<bean id="server" class="com.spyouth.service.SomeServerImpl" ></bean>
单例模式,只创建一个
<bean id="server" class="com.spyouth.service.SomeServerImpl" scope="singleton"></bean>
原型模式,用一次创建一个
<bean id="server" class="com.spyouth.service.SomeServerImpl" scope="prototype"></bean>

使用方法

        String config="applicationContext.xml";
        //通过 类的存放路径
        ApplicationContext cxt=new ClassPathXmlApplicationContext(config);
        //通过 文件系统的路径
        //ApplicationContext cx2=new FileSystemXmlApplicationContext(config);
        SomeServer server= (SomeServer) cxt.getBean("server");
        server.doSome(1,"asd");

四、为 bean 成员赋值

Java 对象若不为成员变量赋值,数值型成员为 0,引用型成员为 null

public class School {
    String name;
    String address;
    set、get方法......
}
public class Student {
    String name;
    School school;
    set、get方法......
}
    <bean id="school" class="com.spyouth.study.School">
        <property name="name" value="清华"></property>
        <property name="address" value="五道口"></property>
    </bean>

    <bean id="student" class="com.spyouth.study.Student">
        <!--基本类型用 name/成员名、value/参数值 -->
        <property name="name" value="张三"></property>
        <!--引用类型用 name/成员名、ref/引用对象名,就是上面那个School对象 -->
        <property name="school" ref="school"></property>
    </bean>
    public Student(String name, int age, School school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }
    <bean id="student4" class="com.spyouth.study.Student">
        <constructor-arg name="name" value="赵丽"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="school" ref="school"></constructor-arg>
    </bean>

    <bean id="student5" class="com.spyouth.study.Student">
        <constructor-arg index="0" value="赵丽"></constructor-arg>
        <constructor-arg index="1" value="18"></constructor-arg>
        <constructor-arg index="2" ref="school"></constructor-arg>
    </bean>
<!--自动为成员变量赋值 byType 找能赋值给成员变量的对象-->
    <bean id="student2" class="com.spyouth.study.Student" autowire="byType"></bean>
    <!--自动为成员变量赋值 byName 找与成员变量名相同的对象-->
    <bean id="student3" class="com.spyouth.study.Student" autowire="byName"></bean>

本包及子包都会去找
<context:component-scan base-package="com.spyouth.study"></context:component-scan>
@Component(value = "student8")
public class Student {
    @Value("王五")
    public String name;
    @Value("28")
    public int age;

    //byName
    @Autowired
    @Qualifier(value = "school")
    public School school;
    //byType
    @Autowired
    public School school;
    //先 byName 没有找到就 byType
    @Resource
    public School school;

    //也可以注解在 set 方法上  
    @Value("18")
    public void setAge(int age) {
        this.age = age;
    }
}
@Component(value = "student8")
@Component("student8")
对于参数只有 value  二者等效

五、import spring.xml

所有 bean 都写在同一个 xml 文件中,过于混乱,而且不利于协作开发
可以写一个总的 xml ,再将其他 xml 导入其中

    <import resource="classpath:spring-school.xml"></import>
    <import resource="classpath:spring-server.xml"></import>
上一篇 下一篇

猜你喜欢

热点阅读