1.写一个helloSpring程序

导入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-webmvc</artifactId>
  5. <version>5.3.5</version>
  6. </dependency>
  7. ganfbian
  8. //使用测试导入junit
  9. <dependency>
  10. <groupId>junit</groupId>
  11. <artifactId>junit</artifactId>
  12. <version>4.12</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-context</artifactId>
  17. <version>5.3.5</version>
  18. </dependency>
  19. </dependencies>

创建实体类

  1. package com.sy.pojo;
  2. public class Hello {
  3. private String str;
  4. public String getStr() {
  5. return str;
  6. }
  7. public void setStr(String str) {
  8. this.str = str;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Hello{" +
  13. "str='" + str + '\'' +
  14. '}';
  15. }
  16. }

编写 Spring文件

<?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">

       <bean id="hello" class="com.sy.pojo.Hello">
            <property name="str" value="Spring"/>
       </bean>
       </beans>

使用Spring中创建对象,在Spring中这些都称为Bean
类型 变量名 = new 类型();
Hello hello = new Hello();



id = 变量名
class = new 的对象;
property相当于给对象中的属性设置一个值

bean就是java对象 , 由Spring创建和管理

测试

import com.sy.pojo.Hello;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void Text(){
        //获取上下文对象
        //解析beans.xml文件 , 生成管理相应的Bean对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //对象现在都在Spring中管理了,我们使用时,直接去里面取出就可
        Hello hello = (Hello) context.getBean("hello");
         hello.toString();
         System.out.println(hello.toString());

    }

}

//getBean : 参数即为spring配置文件中bean的id .

2.控制反转原理

231241fjfjf254f.png

3.修改案例一

修改beans.xml文件

<bean id="mysqlImpl" class="com.sy.dao.UserDaoMysqlImpl"/>
 <bean id="UserServiceImpl" class="com.sy.service.UserServiceImpl">
        <!--ref:引用容器中创建好的对象
            value:具体的值-->
        <property name="userDao" ref="oracleImpl"></property>
</bean>

测试

public static void main(String[] args) {
        //获取ApplicationConext; 拿到Spring的容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        userServiceImpl.getUser();
   }

OK , 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !

4.IOC创建对象方式

无参方法构造

实体类

package com.sy.pojo;

import java.util.Objects;

public class User {
    private String name;

    public User() {
        System.out.println("User()被创建了");
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void show(){
        System.out.println("name="+name);
    }
}

beans.xml

<bean id="user" class="com.sy.pojo.User">
        <property name="name" value="嘻嘻"/>
  </bean>

测试

public static void main(String[] args) {
        //获取ApplicationConext; 拿到Spring的容器
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user");       
        user.show();
}

用debug断点测试,可以发现在调用show方法之前,User对象已经通过无参构造初始化了!

有参

有三种方法

<!--   第一种:下标赋值-->
   <!-- <bean id="user" class="com.sy.pojo.User">
        <constructor-arg index="0" value="逢坂"/>
    </bean>-->

    <!--类型赋值,同类型有两个以上时不建议使用-->
    <!--<bean id="user" class="com.sy.pojo.User">
        <constructor-arg type="java.lang.String" value="大河"/>
    </bean>-->

    <!--直接通过参数名赋值-->
    <bean id="user" class="com.sy.pojo.User">
        <constructor-arg name="name" value="逢坂大河"/>
    </bean>

User类:

public class User {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(String name) {
        this.name = name;
    }

    public void show(){
        System.out.println("name="+name);
    }
}

测试:在配置文件加载的时候。其中管理的对象都已经初始化了。

5.Spring配置

alias别名

 <alias name="user" alias="laohu"/>

Bean的配置

<bean id="wife" class="com.sy.pojo.Wife" name="LaoPo,sama queen">
        <property name="name" value="食蜂操祈"/>
    </bean>

name的用法远比alias好用,中间用标点和空格等标识符就可以隔开创建多个别名

import

<import resource="beans.xml"/>

在总的applicationContext.xml中通过输入import导入其他xml,使用时直接引用applicationContext.xml就可。适用于团队合作的项目中。