我正在浏览一些关于SpringSource的博客,在一个博客中,作者正在使用@Inject,我想他也可以使用@Autowired.
这是一段代码:
@Inject private CustomerOrderService customerOrderService;
我不确定它们之间的区别,@Inject并且@Autowired如果有人解释了它们之间的差异以及在什么情况下使用哪一个,我将不胜感激?
我看到他们在这里被记录在案.它们是一样的吗?为什么Ruby有这么多别名(比如数组的map/collect)?非常感谢.
我设法在Spring中使用JobStoreTX持久存储来配置和调度Quartz作业.我不使用Spring的Quartz作业,因为我需要在运行时动态调度它们,并且我发现Spring与Quartz集成的所有示例都是对Spring配置文件中的shcedules进行硬编码...无论如何,这里是如何我安排工作:
JobDetail emailJob = JobBuilder.newJob(EMailJob.class)
.withIdentity("someJobKey", "immediateEmailsGroup")
.storeDurably()
.build();
SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
.withIdentity("someTriggerKey", "immediateEmailsGroup")
.startAt(fireTime)
.build();
// pass initialization parameters into the job
emailJob.getJobDataMap().put(NotificationConstants.MESSAGE_PARAMETERS_KEY, messageParameters);
emailJob.getJobDataMap().put(NotificationConstants.RECIPIENT_KEY, recipient);
if (!scheduler.checkExists(jobKey) && scheduler.getTrigger(triggerKey) != null) {
// schedule the job to run
Date scheduleTime1 = scheduler.scheduleJob(emailJob, trigger);
}
Run Code Online (Sandbox Code Playgroud)
EMailJob是一个简单的工作,它使用Spring的JavaMailSenderImpl类发送电子邮件.
public class EMailJob implements Job {
@Autowired
private JavaMailSenderImpl mailSenderImpl;
public EMailJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
....
try {
mailSenderImpl.send(mimeMessage);
} catch (MessagingException e) {
.... …Run Code Online (Sandbox Code Playgroud) 我有一组angular2组件,应该都会注入一些服务.我的第一个想法是,最好创建一个超级类并在那里注入服务.然后我的任何组件都会扩展该超类,但这种方法不起作用.
简化示例:
export class AbstractComponent {
constructor(private myservice: MyService) {
// Inject the service I need for all components
}
}
export MyComponent extends AbstractComponent {
constructor(private anotherService: AnotherService) {
super(); // This gives an error as super constructor needs an argument
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过MyService在每个组件中注入并使用该参数进行super()调用来解决这个问题,但这肯定是某种荒谬的.
如何正确组织我的组件,以便他们从超类继承服务?
我想知道是否有一种在Angular2中注入接口的正确方法?(参见下文)
我认为这与接口上缺少的@Injectable()装饰器有关,但似乎不允许这样做.
问候.
当CoursesServiceInterface作为接口实现时,TypeScript编译器会抱怨"CoursesServiceInterface找不到名称":
import {CoursesServiceInterface} from './CoursesService.interface';
import {CoursesService} from './CoursesService.service';
import {CoursesServiceMock} from './CoursesServiceMock.service';
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
GlobalService,
provide(CoursesServiceInterface, { useClass: CoursesServiceMock })
]);
Run Code Online (Sandbox Code Playgroud)
但是使用CoursesServiceInterface作为接口:
import {Injectable} from 'angular2/core';
import {Course} from './Course.class';
//@Injectable()
export interface CoursesServiceInterface {
getAllCourses(): Promise<Course[]>;//{ return null; };
getCourse(id: number): Promise<Course>;// { return null; };
remove(id: number): Promise<{}>;// { return null; };
}
Run Code Online (Sandbox Code Playgroud)
当service是一个类时,TypeScript编译器不再抱怨:
import {Injectable} from 'angular2/core';
import {Course} from './Course.class';
@Injectable()
export class CoursesServiceInterface {
getAllCourses() : Promise<Course[]> { return null; …Run Code Online (Sandbox Code Playgroud) 我理解为了在Ruby中对数组元素求和,可以使用inject方法,即
array = [1,2,3,4,5];
puts array.inject(0, &:+)
Run Code Online (Sandbox Code Playgroud)
但是如何在对象数组中求和对象的属性呢?
有一个对象数组,每个对象都有一个属性"现金",例如.所以我想将他们的现金余额总计为一个.就像是...
array.cash.inject(0, &:+) # (but this doesn't work)
Run Code Online (Sandbox Code Playgroud)
我意识到我可能会创建一个仅由物业现金组成的新阵列,并总结一下,但如果可能的话,我正在寻找一种更清洁的方法!
最近我开始重构我正在使用TypeScript进行的一个Angular项目.使用TypeScript类来定义控制器非常方便,并且由于static $inject Array<string>属性而适用于缩小的JavaScript文件.并且您可以获得非常干净的代码,而无需从类定义中拆分Angular依赖项:
module app {
'use strict';
export class AppCtrl {
static $inject: Array < string > = ['$scope'];
constructor(private $scope) {
...
}
}
angular.module('myApp', [])
.controller('AppCtrl', AppCtrl);
}
Run Code Online (Sandbox Code Playgroud)
现在我正在寻找解决方案来处理指令定义的类似情况.我找到了一个很好的做法,将指令定义为函数:
module directives {
export function myDirective(toaster): ng.IDirective {
return {
restrict: 'A',
require: ['ngModel'],
templateUrl: 'myDirective.html',
replace: true,
link: (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ctrls) =>
//use of $location service
...
}
};
}
angular.module('directives', [])
.directive('myDirective', ['toaster', myDirective]);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我被迫在指令定义中定义Angular依赖项,如果定义和TypeScript类在不同的文件中,则可能非常容易出错.使用typescript和$inject机制定义指令的最佳方法是什么,我正在寻找一种实现TypeScript IDirectiveFactory接口的好方法,但我对我找到的解决方案并不满意.
我对Angular很新,并且已经回顾了Stack Overflow上所有类似相关的问题,但没有人帮助过我.我相信我已经正确设置了一切但在尝试将服务注入单元测试时仍然出现"未知提供商"错误.我在下面列出了我的代码 - 希望有人能发现一个明显的错误!
我在一个单独的.js文件中定义我的模块,如下所示:
angular.module('dashboard.services', []);
angular.module('dashboard.controllers', []);
Run Code Online (Sandbox Code Playgroud)
这里我定义了一个名为EventingService的服务(为简洁起见,删除了逻辑):
angular.module('dashboard.services').factory('EventingService', [function () {
//Service logic here
}]);
Run Code Online (Sandbox Code Playgroud)
这是我使用EventingService的控制器(这在运行时都可以正常工作):
angular.module('dashboard.controllers')
.controller('Browse', ['$scope', 'EventingService', function ($scope, eventing) {
//Controller logic here
}]);
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试 - 它是我尝试注入EventingService的行,当我运行单元测试时会导致错误:
describe('Browse Controller Tests.', function () {
beforeEach(function () {
module('dashboard.services');
module('dashboard.controllers');
});
var controller, scope, eventingService;
beforeEach(inject(function ($controller, $rootScope, EventingService) {
scope = $rootScope.$new();
eventingService = EventingService
controller = $controller('Browse', {
$scope: scope,
eventing: eventingService
});
}));
it('Expect True to be True', function () …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用Spring Social:
<spring.framework.version>3.2.0.RELEASE</spring.framework.version>
<hibernate.version>4.1.9.Final</hibernate.version>
<commons-dbcp.version>1.4</commons-dbcp.version>
<org.springframework.social-version>1.1.0.BUILD-SNAPSHOT</org.springframework.social-version>
<org.springframework.social.facebook-version>1.1.0.BUILD-SNAPSHOT</org.springframework.social.facebook-version>
<org.springframework-version>3.2.1.RELEASE</org.springframework-version>
<org.springframework.security.crypto-version>3.1.3.RELEASE</org.springframework.security.crypto-version>
Run Code Online (Sandbox Code Playgroud)
当我申请
private final Facebook facebook;
@Inject
public SearchController(Facebook facebook) {
this.facebook = facebook;
}
Run Code Online (Sandbox Code Playgroud)
到我的HomeController:
@Controller
public class HomeController {
private final Facebook facebook;
@Inject
public HomeController(Facebook facebook) {
this.facebook = facebook;
}
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
return "home";
}
}
Run Code Online (Sandbox Code Playgroud)
注射工作就像有意,我可以从中获取信息facebook …
为什么以下代码运行正常
p (1..1000).inject(0) { |sum, i|
sum + i
}
Run Code Online (Sandbox Code Playgroud)
但是,以下代码给出了错误
p (1..1000).inject(0) do |sum, i|
sum + i
end
warning: do not use Fixnums as Symbols
in `inject': 0 is not a symbol (ArgumentError)
Run Code Online (Sandbox Code Playgroud)
它们应该不相同吗?