dependency-injection

How to programmatically use Spring's JdbcTemplate?

倖福魔咒の 提交于 2019-12-21 01:57:11
问题 We use Spring's JdbcTemplate which is configured through Spring config as illustrated below. Is there a way to do this without injecting the data source? I'd like to just create the JdbcTemplate instance programmatically and "initalize" the datasource using TheOracleDS . Our current config: Java class private JdbcTemplate jdbcTemplate; @Resource(name = "myDataSource") public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } Spring config <jee:jndi

Simple Injector usage for generic command handler

旧街凉风 提交于 2019-12-20 23:24:07
问题 The interfaces,commands and command handler set up as per instructions in Simpleinjector wiki. public interface ICommand { string Name { get; set; } } public class Command1 : ICommand { public string Name { get; set; } } public class Command2 : ICommand { public string Name { get; set; } } public interface ICommandHandler<TCommand> { void Execute(TCommand Command); } public class Command1Handler : ICommandHandler<Command1> { public void Execute(Command1 Command) { Console.WriteLine(Command

When does Spring create instances of objects that are injected

此生再无相见时 提交于 2019-12-20 21:56:22
问题 Spring does DI and creates objects so that your program need not worry of creating objects. But the question here is when an instance of injected object is created. Is it when the main program makes use of the instance or at the time an instance of main program is created. 回答1: All beans in the context are instantiated, injected and initialized when the context starts up. By the time the first bean has been retrieved from the context, all beans are ready for use. There are two things that can

Is it good practice to have DI container replace a global $registry object?

天大地大妈咪最大 提交于 2019-12-20 20:10:10
问题 I have started refactoring a small application to use a small DI container instead of having $registry::getstuff(); calls in my classes I inject them in a container. This has raised 2 questions, Q1 -> I extend Pimple DI class and create a container with dependencies specific to each object that will need DI. I then feed the object the whole shebang, and decrontruct it it in the constructor assigning the DI's objects to the class properties of the object I'm building. Should I be separating

Can I Use Typed Factory Facility to Return Implementation Based on (enum) Parameter?

会有一股神秘感。 提交于 2019-12-20 18:32:28
问题 Not sure if this is possible or not. I need to return the correct implementation of a service based on an enum value. So the hand-coded implementation would look something like: public enum MyEnum { One, Two } public class MyFactory { public ITypeIWantToCreate Create(MyEnum type) { switch (type) { case MyEnum.One return new TypeIWantToCreate1(); break; case MyEnum.Two return new TypeIWantToCreate2(); break; default: return null; } } } The implementations that are returned have additional

Dependency injection duplication in Controller and BaseController in .Net Core 2.0

人走茶凉 提交于 2019-12-20 12:52:14
问题 If I create a BaseController in my Asp.Net Core 2.0 web application that capsulizes some of the common dependencies are they still necessary in the actual controllers. For Example, the standard Account and Manage controllers in a default MVC 6 web application. public class AccountController : Controller { private readonly UserManager<ApplicationUser> _userManager; private readonly SignInManager<ApplicationUser> _signInManager; private readonly IEmailSender _emailSender; private readonly

Is it dependency injection and is it a bad practice?

╄→尐↘猪︶ㄣ 提交于 2019-12-20 12:36:42
问题 I have a small framework and I coded it like this. I'm not sure if it is called dependency injection or not. I don't know if it is like a design pattern or not. I also don't know and wonder if passing $this as param is a bad practice. Have a look at this; (Not a working example, just wrote those codes into browser for explanation.) /* This is engine model */ require_once('Database.class.php'); require_once('Image.class.php'); require_once('Misc.class.php'); require_once('BBCode.class.php');

Play 2.5.3: Using dependency injection to get configuration values

♀尐吖头ヾ 提交于 2019-12-20 12:31:27
问题 I'm trying to migrate a Playframework application from 2.4 to 2.5.3 and I have problems to get values from application.conf file: Before to get a value of from application.conf what I do was: Play.application().configuration().getString("label") Now as Play.application() is deprecated, I should use Dependency injection. Based on the framework documentation I use the following instructions: Define import : import javax.inject.*; import play.Configuration; Define class property : @Inject

Angular 5 Http Interceptors error when injecting service

♀尐吖头ヾ 提交于 2019-12-20 12:24:50
问题 I am receiving the following strange dependency injection behavior when using custom HttpInterceptors in angular 5+. The following simplified code works fine: export class AuthInterceptor implements HttpInterceptor { constructor(private auth: AuthService) {} intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const token = this.auth.getToken(); return next.handle(req); } } export class AuthService { token: string; constructor() { console.log('AuthService

How to use Spring with JavaFX ?

我只是一个虾纸丫 提交于 2019-12-20 11:56:49
问题 I am using javaFX with Scene Builder in my project and I have many pages. I want to avoid complexity, that's why I want to use Spring framework. So please can anyone explain to me in details how to configure JavaFX with spring framework? 回答1: I searched for Spring and JavaFX integration sample but could not find a good one. So I worked on an example. You can look at this example application. https://gitlab.com/sunkur/SpringJavaFXController I hope it helps. 回答2: There are many ways to