GVKun编程网logo

java – Spring能否理解@Inject将Weld替换为JSR-299实现?(java文件内容替换)

12

对于java–Spring能否理解@Inject将Weld替换为JSR-299实现?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解java文件内容替换,并且为您提供关于$inject.in

对于java – Spring能否理解@Inject将Weld替换为JSR-299实现?感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解java文件内容替换,并且为您提供关于$inject.instantiate VS $inject.get VS $injector.invoke in angularjs、asp.net – Quartz.net和Ninject:如何使用NInject将实现绑定到我的作业、Field error in object ''entry'' on field ''file'': rejected value [org.springframework.web.multipart.com、java – spring SQL Injection中的@Query注释是否安全?的宝贵知识。

本文目录一览:

java – Spring能否理解@Inject将Weld替换为JSR-299实现?(java文件内容替换)

java – Spring能否理解@Inject将Weld替换为JSR-299实现?(java文件内容替换)

我从几个网页中注意到,显然Spring 3.0支持JSR-330的@Inject.由于我们真的想在我们的库中为Web应用程序和独立应用程序使用JSR-299语法进行依赖项注入,并且可以选择Weld,如果Spring可以做到这一点会很好.

作为Spring的新手,我尝试下载Spring Framework发行版并将所有jar放在Eclipse构建路径上. No Inject annotation所以我使用Weld的现有测试项目没有编译.

这可以用Spring完成吗?要让它运行,我需要做什么?

(我知道Guice最终也会支持这个.现在只在SVN中使用,如果有正式的Spring版本可以,那会更好.)

可以办到.必须单独下载JSR-330 jar,并使用cglib解析手动编写的@Configuration类,以及公共日志记录实现.

与Weld最大的区别似乎是布线需要手动编写而不是神奇地找到(更麻烦,但可能会使应用程序更加强大),再加上启动时间要少得多.我还是Spring的新手 – 有没有办法让@Configuration类自动发现?

最佳答案
从Spring 3.0.x Reference documentation:

JSR 330’s @Inject annotation can be used in place of Spring’s @Autowired in the examples below. @Inject does not have a required property unlike Spring’s @Autowire annotation which has a required property to indicate if the value being injected is optional. This behavior is enabled automatically if you have the JSR 330 JAR on the classpath.

因此,您可以使用@Inject使您的代码与DI框架无关,但您仍需要在项目中包含一个带有javax.inject类的jar,因为Spring本身并不提供它们.您可以在JSR-330’s Google Code site的下载部分找到相关的jar.

$inject.instantiate VS $inject.get VS $injector.invoke in angularjs

$inject.instantiate VS $inject.get VS $injector.invoke in angularjs

AngularJS中的$inject.instantiate,$injector.get和$injector.invoke之间有什么区别?
给予以下服务:
app.service('myService',function ($q,$http) {
  return {
    q:    $q,http: $http
  };
});

$injector.get(name,[caller]);

返回所请求服务的实例.

$injector.get('myService');
// { q: $q,http: $http }

$injector.invoke(fn,[self],[localals]);

调用提供的方法,并从$inject中传递给定的参数.

$injector.invoke(function (myService,$http) {
  console.log(myService); // { q: $q,http: $http };
  console.log(this);      // { v: 'im this!' };
  console.log($http);     // null
},{ v: 'im this!' },{ $http: null });

$injector.instantiate(Type,[locals]);

创建给定类型的新实例.使用构造函数,然后使用构造函数注释中指定的参数调用新实例.

假设以下’class’:

function Person (fName,lName,$http,$q) {
  return {
    first_name: fName,last_name:  lName,http: $http,q:    $q
  }
}

现在,如果我们想在我们的控制器中创建一个新的Person,我们可以这样做:

app.controller('...',function ($injector) {
  var $http = $injector.get('$http');
  var $q    = $injector.get('$q');
  var p     = new Person('kasper','lewau',$q);

  console.log(p); // { first_name: 'kasper',last_name: 'lewau',q: $q };
});

想象一下,人有〜20个依赖关系,我们用$inject.get方法获取每个人的每一个.

繁琐!而且 – 你需要保持你的参数&参数同步啊.

相反,你可以这样做:

app.controller('...',function ($injector) {
  var p = $injector.instantiate(Person,{
    fName: 'kasper',lName: 'lewau'
  });
  console.log(p); // { first_name: 'kasper',q: $q };
});

而且 – 如果我们想要,我们可以提供本地的.instantiate调用,以便覆盖内部$inject.get()在实例化时通常会得到的内容.

var p = $injector.instantiate(Person,{
  fName: 'kasper',lName: 'lewau'
},{ $http: 'nothing!',$q: 'nothing!' });
console.log(p); // { first_name: 'kasper',http: 'nothing!',q: 'nothing!' };

我希望解释三者之间的区别.如果您需要更多有关差异的信息,我会推荐这些文章:

> http://taoofcode.net/studying-the-angular-injector/
> http://taoofcode.net/studying-the-angular-injector-annotate/
> http://taoofcode.net/studying-the-angular-injector-invoke/
> http://taoofcode.net/studying-the-angular-injector-getservice/
> http://taoofcode.net/studying-the-angular-js-injector-instantiate/

asp.net – Quartz.net和Ninject:如何使用NInject将实现绑定到我的作业

asp.net – Quartz.net和Ninject:如何使用NInject将实现绑定到我的作业

我实际上在ASP.Net MVC 4 Web应用程序中工作,我们使用NInject进行依赖注入.我们还使用基于Entity框架的UnitOfWork和Repositories.

我们想在我们的应用程序中使用Quartz.net定期启动一些自定义作业.我希望NInject自动绑定我们工作中需要的服务.

它可能是这样的:

public class dispatchingJob : IJob
{
    private readonly IdispatchingManagementService _dispatchingManagementService;

    public dispatchingJob(IdispatchingManagementService dispatchingManagementService )
    {
         _dispatchingManagementService = dispatchingManagementService ;
    }

    public void Execute(IJobExecutionContext context)
    {
         LogManager.Instance.Info(string.Format("dispatching job started at: {0}",DateTime.Now));
        _dispatchingManagementService.dispatchAtomicChecks();
        LogManager.Instance.Info(string.Format("dispatching job ended at: {0}",DateTime.Now));
    }
}

到目前为止,在我们的NInjectWebCommon绑定中配置如下(使用请求范围):

kernel.Bind<IdispatchingManagementService>().To<dispatchingManagementService>();

是否可以使用NInject将正确的实现注入我们的自定义作业?怎么做?我已经阅读了很少关于堆栈溢出的帖子,但是我需要一些建议和一些使用NInject的例子.

解决方法

在Quartz计划中使用JobFactory,并在那里解析您的作业实例.

所以,在你的NInject配置中设置工作(我在这里猜测正确的NInject语法)

// Assuming you only have one IJob
kernel.Bind<IJob>().To<dispatchingJob>();

然后,创建一个JobFactory:[编辑:这是@BatteryBackupUnit’s answer here的修改版本]

public class NInjectJobFactory : IJobFactory
{
    private readonly IResolutionRoot resolutionRoot;

    public NinjectJobFactory(IResolutionRoot resolutionRoot)
    {
        this.resolutionRoot = resolutionRoot;
    }

    public IJob NewJob(TriggerFiredBundle bundle,IScheduler scheduler)
    {
        // If you have multiple jobs,specify the name as
        // bundle.JobDetail.JobType.Name,or pass the type,whatever
        // NInject wants..
        return (IJob)this.resolutionRoot.Get<IJob>();
    }

    public void ReturnJob(IJob job)
    {
        this.resolutionRoot.Release(job);
    }
}

然后,在创建调度程序时,将JobFactory分配给它:

private IScheduler GetSchedule(IResolutionRoot root)
{
    var schedule = new StdSchedulerFactory().GetScheduler();

    schedule.JobFactory = new NInjectJobFactory(root);

    return schedule;
}

然后Quartz将使用JobFactory创建作业,NInject将为您解析依赖项.

Field error in object ''entry'' on field ''file'': rejected value [org.springframework.web.multipart.com

Field error in object ''entry'' on field ''file'': rejected value [org.springframework.web.multipart.com

当用 ssm 框架进行文件上传遇到的问题为:Field error in object ''entry'' on field ''file'': rejected value [org.springframework.web.multipart.commons.CommonsMultipartFile@4c6deff6]; codes [typeMismatch.entry.file,typeMismatch.file,typeMismatch.java.lang.String,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [entry.file,file]; arguments []; default message [file]]; default message [Failed to convert property value of type ''org.springframework.web.multipart.commons.CommonsMultipartFile'' to required type ''java.lang.String'' for property ''file''; 

后来在网上找了很多答案,发现都没有解决问题,最后才发现原来是文件上传对应 input 中的 name=“file” 所造成的之前的代码是:

controller 中的处理方法代码:

最终运行造成的结果是:

解决办法:

把 file 对应的 input 中的 name 改成 name=“filepath” 对应的 controller 中的参数也要进行对应的更改

更改后的代码为:

controller:

运行后的结果成功上传!

 

java – spring SQL Injection中的@Query注释是否安全?

java – spring SQL Injection中的@Query注释是否安全?

对于Spring,传递给@Query注释的字符串的参数是否被视为纯数据,例如,如果您使用的是PreparedStatement类或任何旨在阻止sql注入的方法?

final String MY_QUERY = "SELECT * FROM some_table WHERE some_column = ?1";

@Query(value=MY_QUERY,nativeQuery = true)
ListIoUsUserInput);

结论:上面的代码是否容易受到sql注入的影响?

最佳答案
看起来Spring Data的@Query只是JPA的一个包装器

看到这个答案:Are SQL injection attacks possible in JPA?

关于java – Spring能否理解@Inject将Weld替换为JSR-299实现?java文件内容替换的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于$inject.instantiate VS $inject.get VS $injector.invoke in angularjs、asp.net – Quartz.net和Ninject:如何使用NInject将实现绑定到我的作业、Field error in object ''entry'' on field ''file'': rejected value [org.springframework.web.multipart.com、java – spring SQL Injection中的@Query注释是否安全?等相关内容,可以在本站寻找。

本文标签: