Spring.Net不能实例化一个类 [英] Spring.Net Cannot instantiate a class
问题描述
错误::无法实例不具有的类的无参数的构造函数[ClassLibrary2.Class1]
我创建了一个测试项目。
我得到了在春天src中的误差源。
//////////////
ConstructorInfo构造= GetZeroArgConstructorInfo(的objectType); //这个,只有零ARG允许。但是我使用依赖注入的构造函数。这行的事工作。
////
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;命名空间ClassLibrary1的
{
公共接口IHI
{
字符串的sayHi();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;命名空间ClassLibrary1的
{
公共类的Class1:IHI
{
公共字符串的sayHi()
{
返回您好呀;
}
}
}使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用ClassLibrary1的;
命名空间ClassLibrary2
{
公共类的Class1:IWannaSay
{
私人只读IHI喜;
///行的事在这里工作。提示无法实例不具有一个无参数的构造函数[ClassLibrary2.Class1]一类。 公众的Class1(IHI喜)
{
this.Hi =喜;
}
公共字符串的sayHi()
{ 返回Hi.SayHi();
} }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;命名空间ClassLibrary2
{
公共接口IWannaSay
{
字符串的sayHi();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用ClassLibrary2;
命名空间Mvc4.Controllers
{
公共类HomeController的:控制器
{
私人只读IWannaSay WannaSay;
/// H ERE的作品。其所以werid。
公众的HomeController(IWannaSay WannaSay)
{
this.WannaSay = WannaSay; }
公众的ActionResult指数()
{ 变种C = WannaSay.SayHi();
ViewBag.Message =修改此模板,以启动你的ASP.NET MVC应用程序。 返回查看();
}}
我的test.xml
<?XML版本=1.0编码=UTF-8&GT?;
<对象的xmlns =http://www.springframework.net> <对象ID =W型=ClassLibrary2.Class1,ClassLibrary2>
< /对象>
<对象ID =嗨TYPE =ClassLibrary1.Class1,ClassLibrary1的>
< /对象> <对象ID =HIOTYPE =ClassLibrary2.Class1,ClassLibrary2单身=false的>
<! - 构造器注入 - >
<! - <构造带参数的名称=嗨REF =嗨/> - >
<属性名=嗨REF =W/> < /对象> <对象ID =HomeCountrollerTYPE =Mvc4.Controllers.HomeController,Mvc4单身=false的>
<! - 构造器注入 - >
<构造 - 精氨酸名=WannaSayREF =W/>
<! - <属性名=UserRespositoryImplREF =UserRespositoryImpl/> - > < /对象>
< /对象>
的web.config
<?XML版本=1.0编码=UTF-8&GT?;
<! -
有关如何配置ASP.NET应用程序的更多信息,请访问:
http://go.microsoft.com/fwlink/?LinkId=169433
- >
<结构>
< configSections>
<! - 有关Entity Framework的配置的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
<节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本5.0.0.0 =文化=中性公钥= b77a5c561934e089requirePermission =FALSE/>
< sectionGroup NAME =春天里>
<节名称=背景TYPE =Spring.Context.Support.MvcContextHandler,Spring.Web.Mvc4/>
<节名称=对象TYPE =Spring.Context.Support.DefaultSectionHandler,Spring.Core/>
<节名称=解析器TYPE =Spring.Context.Support.NamespaceParsersSectionHandler,Spring.Core/>
< / sectionGroup>
< / configSections>
<春>
<&分析器GT;
<解析器TYPE =Spring.Data.Config.DatabaseNamespaceParser,和Spring.Data/>
<解析器TYPE =Spring.Transaction.Config.TxNamespaceParser,和Spring.Data/>
< /解析器>
<上下文中,GT;
<资源URI =集结号://Mvc4/Mvc4.Configs/test.xml/> < /背景>
< /春>
<&是connectionStrings GT;
<添加名称=DefaultConnection的connectionString =数据源=(的LocalDB)\\ V11.0;初始目录= ASPNET-Mvc4-20120919193749;集成安全性= SSPI; AttachDBFilename = | DataDirectory目录| \\ ASPNET,Mvc4-20120919193749.mdf的providerName =System.Data.SqlClient的/>
< /&是connectionStrings GT;
<&的appSettings GT;
<添加键=网页:版本值=2.0.0.0/>
<添加键=网页:启用VALUE =FALSE/>
<添加关键=preserveLoginUrlVALUE =真/>
<添加键=ClientValidationEnabledVALUE =真/>
<添加键=UnobtrusiveJavaScriptEnabledVALUE =真/>
< /的appSettings>
<&的System.Web GT;
<编译调试=真targetFramework =4.5/>
<的httpRuntime targetFramework =4.5/>
<身份验证模式=表格>
<形式loginUrl =〜/帐号/登录超时=2880/>
< /认证>
<网页和GT;
<&命名空间GT;
<添加命名空间=System.Web.Helpers/>
<添加命名空间=System.Web.Mvc/>
<添加命名空间=System.Web.Mvc.Ajax/>
<添加命名空间=System.Web.Mvc.Html/>
<添加命名空间=System.Web.Optimization/>
<添加命名空间=System.Web.Routing/>
<添加命名空间=System.Web.WebPages/>
< /命名空间>
< /页>
< /system.web>
< system.webServer>
<验证validateIntegratedModeConfiguration =FALSE/>
<&处理GT;
<清除NAME =ExtensionlessUrlHandler-ISAPI-4.0_32bit/>
<清除NAME =ExtensionlessUrlHandler-ISAPI-4.0_64bit/>
<清除NAME =ExtensionlessUrlHandler - 集成 - 4.0/>
<添加名称=ExtensionlessUrlHandler-ISAPI-4.0_32bitPATH =*。动词=GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS模块=IsapiModulescriptProcessor =%WINDIR%\\ Microsoft.NET \\框架\\ v4.0.30319 \\ ASPNET_ISAPI.DLLpreCondition = classicMode,runtimeVersionv4.0,bitness32responseBufferLimit =0/>
<添加名称=ExtensionlessUrlHandler-ISAPI-4.0_64bitPATH =*。动词=GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS模块=IsapiModulescriptProcessor =%WINDIR%\\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ ASPNET_ISAPI.DLLpreCondition = classicMode,runtimeVersionv4.0,bitness64responseBufferLimit =0/>
<添加名称=ExtensionlessUrlHandler - 集成 - 4.0PATH =*。动词=GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONSTYPE =System.Web.Handlers.TransferRequestHandlerpreCondition =integratedMode,runtimeVersionv4.0/>
< /处理器>
< /system.webServer>
<&运行GT;
< assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>
< dependentAssembly>
< assemblyIdentity名称=System.Web.Helpers公钥=31bf3856ad364e35/>
< bindingRedirect oldVersion =1.0.0.0-2.0.0.0NEWVERSION =2.0.0.0/>
< / dependentAssembly>
< dependentAssembly>
< assemblyIdentity名称=System.Web.Mvc公钥=31bf3856ad364e35/>
< bindingRedirect oldVersion =1.0.0.0-4.0.0.0NEWVERSION =4.0.0.0/>
< / dependentAssembly>
< dependentAssembly>
< assemblyIdentity名称=System.Web.WebPages公钥=31bf3856ad364e35/>
< bindingRedirect oldVersion =1.0.0.0-2.0.0.0NEWVERSION =2.0.0.0/>
< / dependentAssembly>
< / assemblyBinding>
< /运行>
<&的EntityFramework GT;
< defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.SqlConnectionFactory,的EntityFramework/>
< /&的EntityFramework GT;
< /结构>
您可以调整控制器参数
(问题类型是不匹配)
由于
<对象ID =W型=ClassLibrary2.Class1,ClassLibrary2>
< /对象>
<对象ID =HomeCountrollerTYPE =Mvc4.Controllers.HomeController,Mvc4单身=false的>
<构造 - 精氨酸名=WannaSayREF =W/>
< /对象>
您需要的类型的参数 1级
但在构造函数有
公开的HomeController(IWannaSay WannaSay)//< ---
{
this.WannaSay = WannaSay;}
解决方案:修改你的论点,并设置为 Class1的类型
公共类HomeController的
{ 私人Class1的yourProperty;
公共HomeController中(Class1的值)
{
yourProperty =价值;
} .........
}
通过这个春天
<对象ID =W型=ClassLibrary2.Class1,ClassLibrary2>
< /对象>
<对象ID =HomeCountrollerTYPE =Mvc4.Controllers.HomeController,Mvc4单身=false的>
<构造 - 精氨酸名=WannaSayREF =W/>
< /对象>
error :: Cannot instantiate a class that does not have a no-argument constructor [ClassLibrary2.Class1].
I created a test project.
i got the error source in Spring src. //////////////
ConstructorInfo constructor = GetZeroArgConstructorInfo(objectType); // in this , only zero arg allow. But I use dependency inject constructor. It doest work. ////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public interface IHi
{
string SayHi();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1:IHi
{
public string SayHi()
{
return "Hello Yeah";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary1;
namespace ClassLibrary2
{
public class Class1:IWannaSay
{
private readonly IHi Hi;
///Here doest work. prompts Cannot instantiate a class that does not have a no-argument constructor [ClassLibrary2.Class1].
public Class1(IHi Hi)
{
this.Hi = Hi;
}
public string SayHi()
{
return Hi.SayHi();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary2
{
public interface IWannaSay
{
string SayHi();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ClassLibrary2;
namespace Mvc4.Controllers
{
public class HomeController : Controller
{
private readonly IWannaSay WannaSay;
///H ere works .Its so werid.
public HomeController(IWannaSay WannaSay)
{
this.WannaSay = WannaSay;
}
public ActionResult Index()
{
var c = WannaSay.SayHi();
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
}
My test.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="w" type="ClassLibrary2.Class1,ClassLibrary2">
</object>
<object id="Hi" type="ClassLibrary1.Class1,ClassLibrary1">
</object>
<object id="Hio" type="ClassLibrary2.Class1,ClassLibrary2" singleton="false">
<!--构造器注入-->
<!--<constructor-arg name="Hi" ref="Hi"/>-->
<property name="Hi" ref="w"/>
</object>
<object id="HomeCountroller" type="Mvc4.Controllers.HomeController,Mvc4" singleton="false">
<!--构造器注入-->
<constructor-arg name="WannaSay" ref="w"/>
<!--<property name="UserRespositoryImpl" ref="UserRespositoryImpl"/>-->
</object>
</objects>
web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
</parsers>
<context>
<resource uri="assembly://Mvc4/Mvc4.Configs/test.xml" />
</context>
</spring>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Mvc4-20120919193749;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Mvc4-20120919193749.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
You can adjust your controller argument
(Problem is mismatch of type)
Because
<object id="w" type="ClassLibrary2.Class1,ClassLibrary2">
</object>
<object id="HomeCountroller" type="Mvc4.Controllers.HomeController,Mvc4" singleton="false">
<constructor-arg name="WannaSay" ref="w"/>
</object>
You need argument of type Class1
But in your constructor you have
public HomeController(IWannaSay WannaSay)//<---
{
this.WannaSay = WannaSay;
}
Solution : Modify your argument and set to Class1 Type
public class HomeController
{
private Class1 yourProperty;
public HomeController(Class1 value)
{
yourProperty = value;
}
.........
}
With this spring
<object id="w" type="ClassLibrary2.Class1,ClassLibrary2">
</object>
<object id="HomeCountroller" type="Mvc4.Controllers.HomeController,Mvc4" singleton="false">
<constructor-arg name="WannaSay" ref="w"/>
</object>
这篇关于Spring.Net不能实例化一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!