0

错误 :: 无法实例化没有无参数构造函数 [ClassLibrary2.Class1] 的类。

我创建了一个测试项目。

我在 Spring src 中得到了错误源。//////////////

ConstructorInfo 构造函数 = GetZeroArgConstructorInfo(objectType); // 在 this 中,只允许零 arg。但我使用依赖注入构造函数。它不起作用。////

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();
        }

}

我的 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>

网络配置

<?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>
4

1 回答 1

0

你可以调整你的controller argument (Problem is mismatch of type)

因为

  <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>

你需要类型的参数Class1

但是在你的构造函数中你有

public HomeController(IWannaSay WannaSay)//<--- 
{
     this.WannaSay = WannaSay;

}

解决方案:修改您的参数并设置为Class1 Type

public class HomeController
{

    private Class1 yourProperty;
    public HomeController(Class1 value) 
    {
        yourProperty = value;   
    }

    .........


}

带着这个春天

 <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>
于 2012-09-19T14:11:16.780 回答