用Idea完成Spring中基于XML的IoC和DI小Demo

在学习Spring前,你需要明白反射到底是个什么东西?以及单例设计模式、工厂模式、代理模式,以及maven如何使用?
说明:这个Demo只是为了快速理解而已。
以下内容不做过多解释,对于以下内容不清楚的,可以看下我这篇文章
Spring入门-IoC&&DI

1、使用的版本:

IDEA-2017
maven-3.6.0
springframework:5.0.2.RELEASE

2、创建maven工程后,导入依赖

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

3、创建Service类:

package com;

public class Service {
    private int age =22;
    private String name = "张三";
    public void Info(){
        System.out.println("你要查询的是,"+name+"年龄为,"+age);
    }
}

4、编写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来管理-->
    <bean id="service" class="com.Service"></bean>

</beans>

5、创建User类来调用Service类对象:

I.以下代码采用的就是默认构造函数创建bean对象

package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class User {
    public static void main(String[] args) {
    	//1.获取核心容器对象
        ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取Bean对象,取得Service类对象service
        Service service=ac.getBean("service",Service.class);
        service.Info();
    }
}
//你要查询的是,张三年龄为,22

6、使用某个类中的方法创建对象,并存入Spring容器

II.使用普通工厂中的方法创建对象

这种情况是用别人写的类,但是那个类没有默认构造函数而是通过其他方法构造的类。

package com.factory;
import com.Service;
//模拟一个工厂类(该类可能是存在于jar包中,我们无法通过修改源码的方式来提供默认构造函数)
public class InstanceFactory {
    public Service getService(){
        return new Service();
    }
}

此时bean.xml中应该这样写:

<bean id="instanceFactory" class="com.factory.InstanceFactory"></bean>
<bean id="service" factory-bean="instanceFactory" factory-method="getService"></bean>

III:使用工厂中的静态方法创建对象

package com.factory;
import com.Service;

public class StaticFactory {
    public static Service getService(){
        return new Service();
    }
}

此时bean.xml中应该这样写:

<bean id="service" class="com.factory.StaticFactory" factory-method="getService"></bean>

7、依赖注入:

I.用构造函数注入,需要构造函数:

此时先把Service类改造下:

package com;
public class Service {
    private Integer age ;
    private String name ;
    public Service(Integer age,String name){
        this.age=age;
        this.name=name;
    }
    public void info(){
        System.out.println("你要查询的是,"+name+"年龄为"+age);
    }
}

此时bean.xml中应该这么写:

<bean id="service" class="com.Service">
    <constructor-arg name="name" value="张三"></constructor-arg>
    <constructor-arg name="age" value="18"></constructor-arg>
</bean>

II.用set注入,需要生成setter方法

在Service类中生成sett方法。

<bean id="service" class="com.Service">
    <property name="name" value="李四"></property>
    <property name="age" value="21"></property>
</bean>

8、注解配置

I.@Component

导入context约束并告知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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com"></context:component-scan>
</beans>

此时的Service类:

package com;
import org.springframework.stereotype.Component;

@Component(value = "service")
public class Service {
    public void info(){
        System.out.println("你要查询的是,"+name+"年龄为"+age);
    }
}

上面需要用到的User类都不做改变。

II.@Autowired注解:

1)新建一个Dao类。

package com;
import org.springframework.stereotype.Component;
@Component("dao")
public class Dao {
    public void Grade(){
        System.out.println("他们都是大一的学生");
    }
}

2)使用@Autowired自动按照类型注入。

package com;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(value = "service")
public class Service {
    @Autowired
    private Dao dao=null;
    private Integer age ;
    private String name ;
    public void info(){
        System.out.println("你要查询的是,"+name+"年龄为"+age);
    }
    public void Grade(){
        dao.Grade();
    }
}

在User类中加上 service.Grade();方法
此时@Autowired的作用是去IoC容器中找数据类型为Dao的(即找到匹配的bean对象)。
其实就相当于在Service中新建了一个Dao类对象(在Service中注入Dao类对象)

III.@Value

@Value("18")
private Integer age ;
@Value("张三")
private String name ;
发布了30 篇原创文章 · 获赞 1 · 访问量 1881

猜你喜欢

转载自blog.csdn.net/Zzh1110/article/details/104239820