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 这边的效果
Java Spring Boot VS .NetCore (三)Ioc容器处理的更多相关文章
- Java Spring Boot VS .NetCore (一)来一个简单的 Hello World
系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...
- Java Spring Boot VS .NetCore (二)实现一个过滤器Filter
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (七) 配置文件
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
随机推荐
- Python的数据库操作
使用原生SQL语句进行对数据库操作,可完成数据库表的建立和删除,及数据表内容的增删改查操作等.其可操作性很强,如可以直接使用“show databases”.“show tables”等语句进行表格之 ...
- ;。【】DAY14、递归,匿名函数
一.三元运算符 三元运算符也称三目运算符,就是if .....else.....语法糖 前提:if 和 else 只有一条语句 例:a = 20 b = 30 res = a if a > b ...
- C# 中ref与out关键字区别
ref 关键字通过引用传递的参数的内存地址,而不是值.简单点说就是在方法中对参数的任何改变都会改变调用方的基础参数中.代码举例: class RefExample { static void Meth ...
- 用django2.1开发公司官网(上)
1.在MySQL中新建数据库 show databases;//查看已经有的数据库 create database guanwang; 2.新建django项目guan 1.使用pycharm新建dj ...
- css实现弹出框
弹出框也是前端里面经常使用的一个应用场景了,最开始想到的是用js实现这个效果,看到codepen上面有用css实现的.其实就是用到了css里面的一个:target选择器+visibility属性. U ...
- django-url的分发
1)url的分发: 1,首先在全局的url里面的路径中写好,你要分发的路径名. 2,并且在你要分发的路径下,创好新的url文件. 在分发的路径名里面,把全局url里面的代码,复制过来 3,最后在浏览器 ...
- POJ - 3616 Milking Time (动态规划)
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...
- JS学习笔记Day14
一.正则表达式 (一)正则的概念 1.正则表达式是一种特殊的字符串模式,用于匹配一组字符串,就好比用模具做产品,而正则就是这个模具,定义一种规则去匹配符合规则的字符 (二)创建正则的方式 1.字面量的 ...
- The container 'Maven Dependencies' references non existing library '
解决办法 uncheck the option "resolve dependencies from workspace projects" from the maven tab ...
- Basic Calculator I && II && III
Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...