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


 

Java Spring Boot VS .NetCore (一)來一個簡單的 Hello World

Java Spring Boot VS .NetCore (二)實現一個過濾器Filter

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

Java Spring Boot VS .NetCore (四)數據庫操作 Spring Data JPA vs EFCore

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

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

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

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

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

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

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 這邊的效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM