dependency-injection

Spring Qualifier and property placeholder

偶尔善良 提交于 2019-12-17 04:31:01
问题 Does anyone know if I should be able to use property placeholder as an expression in a Qualifier? I can't seem to get this working. I am using spring 3.0.4. @Controller public class MyController { @Autowired @Qualifier("${service.class}") Service service; } @Service @Qualifier("ServiceA") ServiceA implements Service { public void print() { System.out.println("printing ServiceA.print()"); } } @Service @Qualifier("ServiceB") ServiceB implements Service { public void print() { System.out.println

How to inject a Map using the @Value Spring Annotation?

£可爱£侵袭症+ 提交于 2019-12-17 04:02:11
问题 How can i inject values into a Map from the properties file using the @Value annotation in Spring ? My Spring Java class is and i tried using the $ but, got the following error message Could not autowire field: private java.util.Map Test.standard; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.test.standard' in string value "${com.test.standard}" @ConfigurationProperty("com.hello.foo") public class Test { @Value("${com.test.standard}") private Map

Using Spring 3 autowire in a standalone Java application

强颜欢笑 提交于 2019-12-17 03:48:26
问题 Here is my code: public class Main { public static void main(String[] args) { Main p = new Main(); p.start(args); } @Autowired private MyBean myBean; private void start(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/config.xml"); System.out.println("my beans method: " + myBean.getStr()); } } @Service public class MyBean { public String getStr() { return "string"; } } <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3

Dependency Injection & Singleton Design pattern

时间秒杀一切 提交于 2019-12-17 03:46:19
问题 How do we identify when to use dependency injection or singleton pattern. I have read in lot of websites where they say "Use Dependency injection over singleton pattern". But I am not sure if I totally agree with them. For my small or medium scale projects I definitely see the use of singleton pattern straightforward. For example Logger. I could use Logger.GetInstance().Log(...) But, instead of this, why do I need to inject every class I create, with the logger's instance?. 回答1: If you want

How to explain dependency injection to a 5-year-old? [closed]

核能气质少年 提交于 2019-12-17 03:44:13
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not

Can I pass constructor parameters to Unity's Resolve() method?

末鹿安然 提交于 2019-12-17 03:05:20
问题 I am using Microsoft's Unity for dependency injection and I want to do something like this: IDataContext context = _unityContainer.Resolve<IDataContext>(); var repositoryA = _unityContainer.Resolve<IRepositoryA>(context); //Same instance of context var repositoryB = _unityContainer.Resolve<IRepositoryB>(context); //Same instance of context IDataContext context2 = _unityContainer.Resolve<IDataContext>(); //New instance var repositoryA2 = _unityContainer.Resolve<IRepositoryA>(context2);

Injecting data to a WCF service

浪子不回头ぞ 提交于 2019-12-17 03:02:53
问题 I have WCF services structured like suggested by Miguel Castro. This means that I have set everything up manually, and have a console application hosting my services using ServiceHost objects. I want to keep my service classes thin, and they are currently just passing on calls to behavior classes. My problem now is unit testing the service classes. I want to inject something to the classes as a constructor parameter such that I can mock this away and write proper isolated unit tests. The

Resolving instances with ASP.NET Core DI

爱⌒轻易说出口 提交于 2019-12-17 02:40:19
问题 How do I manually resolve a type using the ASP.NET Core MVC built-in dependency injection framework? Setting up the container is easy enough: public void ConfigureServices(IServiceCollection services) { // ... services.AddTransient<ISomeService, SomeConcreteService>(); } But how can I resolve ISomeService without performing injection? For example, I want to do this: ISomeService service = services.Resolve<ISomeService>(); There are no such methods in IServiceCollection . 回答1: The

What's the difference between the Dependency Injection and Service Locator patterns?

試著忘記壹切 提交于 2019-12-16 22:34:42
问题 Both patterns seem like an implementation of the principle of inversion of control. That is, that an object should not know how to construct its dependencies. Dependency Injection (DI) seems to use a constructor or setter to "inject" it's dependencies. Example of using Constructor Injection: //Foo Needs an IBar public class Foo { private IBar bar; public Foo(IBar bar) { this.bar = bar; } //... } Service Locator seems to use a "container", which wires up its dependencies and gives foo it's bar

Unity not injecting dependencies defined in base class

那年仲夏 提交于 2019-12-14 04:17:48
问题 I am integrating Unity 2.0 into my ASP.NET application (using the UnityPageHandlerFactory approach) and have everything working great until I tried to move one of the dependencies into a PageBase class that would then be shared by all pages. This property is never set when BuildUp is called. I'm using the UnityPageHandlerFactory class described here which uses the BuildUp(type, object) method to inject dependencies into each page when it is requested. As long as I have the properties defined