Java Spring Boot VS .NetCore (三)Ioc容器处理

 

Java Spring Boot VS .NetCore (一)来一个简单的 Hello Worldhtml

Java Spring Boot VS .NetCore (二)实现一个过滤器Filterspring

Java Spring Boot VS .NetCore (三)Ioc容器处理数据库

Java Spring Boot VS .NetCore (四)数据库操做 Spring Data JPA vs EFCoreapp

Java Spring Boot VS .NetCore (五)MyBatis vs EFCoreide

Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml函数

Java Spring Boot VS .NetCore (七) 配置文件spa

Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attributeprototype

Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Securitycode

Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptorserver

Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper

Java中Spring Ioc 的处理仍是经过配置文件的方式来实现,容器的初始须要说到上下文对象

ApplicationContext这个类,经过ClassPathXmlApplicationContext 加载 Ioc容器Xml配置并经过该实例对象的GetBean来获取对象实例

下面我就这一块对比 Java 与 .NetCore的使用方式

容器处理

Java:

首先须要创建Spring Ioc的xml配置文件,配置Bean, 也能够经过 @Component 注解的方式实现

<?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="testUserServices" class="com.example.demo.Services.interfaces.Impl.UserServices" >
       </bean>

</beans>

结合以前的helloword例子,咱们修改下代码 getBean经过Id获得,略掉了类相关代码

@RequestMapping("/helloworld")
    public String Index()
    {

        ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-ioc.xml");
        IUserServices servers= (IUserServices) ctx.getBean("testUserServices");
        return  servers.SayHello("张三");
    }

.NetCore:

core中添加了services.AddScoped<IUserServices, UserServices>(); 添加了相关类的注册,而Java中则是经过XML配置

 [Route("~/helloworld")]
        public IActionResult Index()
        {
            var servers=(IUserServices) HttpContext.RequestServices.GetService(typeof(IUserServices));
            var model=  servers.FindByName("admin").Result;
            return View();
        }

这里能够看到 .NetCore 中的 GetServices 与 Java中的 getBean 其实也就是那么一回事

注册的种类 

Java:Java能够经过构造函数、属性注入的方式来注册

.NetCore:同理使用Autofac 也可实现上述,可是.NetCore 中的 Dependence Injection (DI) 通常为构造函数注册 ,可是属性注入须要特殊处理

 

 

Java的xml配置 :

在Bean中添加

属性注入:

 <property name="testUserServices"  value="com.example.demo.Services.interfaces.Impl.UserServices"></property>

构造函数注入:

  <constructor-arg name="testUserServices" value="com.example.demo.Services.interfaces.Impl.UserServices"></constructor-arg>

.NetCore中对注入这块我的以为很方便的

属性注入

services.AddScoped<IDbProviderFactory>(p=> {
return new CreateDbProviderFactory() { Abc = new ABC(); } 
});

 

构造函数注入

 

在Startup中添加  services.AddScoped<IUserServices, UserServices>(); 

 

生命周期管理

Java 配置 Scope

scope="singleton"
scope="prototype"
scope="request"

singleton:单例模式,容器中只会存在一个实例

prototype:每次请求的每次访问生成一个实例

request:每一次请求生成一个实例

.NetCore中

services.AddSingleton
services.AddScoped
services.AddTransient

Singleton :单例模式

Scoped:每次请求的每次反问生成一个实例

 Transient:每次请求生成一个实例

 

先介绍到这里下面看下Java 这边的效果

相关文章
相关标签/搜索