Can I use two kinds of dependency injection in the same class?
i.e.
public class Test {
private ObjectTwo o2;
#Autowired // constructor injection
public Test(ObjectTwo o2 ) {
this.o2=o2;
}
#Autowired // field injection
private ObjectOne o1;
...
"using o1"
"using o2"
...
}
Related Question
https://stackoverflow.com/a/70656587/17755094
Yes you can. You can inject o1 and o2 and differentiate them using the #Qualifier annotation along with #Autowired. Refer here - https://www.baeldung.com/spring-qualifier-annotation.
Here's an example; you could have as many "Formatter" instances as you want as long as you use the #Qualifier annotation on them to differentiate which you want to inject.
#Component("fooFormatter")
public class FooFormatter implements Formatter {
public String format() {
return "foo";
}
}
#Component("barFormatter")
public class BarFormatter implements Formatter {
public String format() {
return "bar";
}
}
public class FooService {
#Autowired
#Qualifier("fooFormatter")
private Formatter formatter;
}
Mixing Injection Types
You can mix constructor and property injection (setters), so I'm sure you can also mix annotation and constructor injection.
Related
I have a class (OmeletteMaker) that contains an injected field (Vegetable). I would like to write a producer that instantiates an injected object of this class. If I use 'new', the result will not use injection. If I try to use a WeldContainer, I get an exception, since OmeletteMaker is #Alternative. Is there a third way to achieve this?
Here is my code:
#Alternative
public class OmeletteMaker implements EggMaker {
#Inject
Vegetable vegetable;
#Override
public String toString() {
return "Omelette: " + vegetable;
}
}
a vegetable for injection:
public class Tomato implements Vegetable {
#Override
public String toString() {
return "Tomato";
}
}
main file
public class CafeteriaMainApp {
public static WeldContainer container = new Weld().initialize();
public static void main(String[] args) {
Restaurant restaurant = (Restaurant) container.instance().select(Restaurant.class).get();
System.out.println(restaurant);
}
#Produces
public EggMaker eggMakerGenerator() {
return new OmeletteMaker();
}
}
The result I get is "Restaurant: Omelette: null", While I'd like to get "Restaurant: Omelette: Tomato"
If you provide OmeletteMaker yourself, its fields will not be injected by the CDI container. To use #Alternative, don't forget specifying it in the beans.xml and let the container instantiate the EggMaker instance:
<alternatives>
<class>your.package.path.OmeletteMaker</class>
</alternatives>
If you only want to implement this with Producer method then my answer may be inappropriate. I don't think it is possible (with standard CDI). The docs says: Producer methods provide a way to inject objects that are not beans, objects whose values may vary at runtime, and objects that require custom initialization.
Thanks Kukeltje for pointing to the other CDI question in comment:
With CDI extensions like Deltaspike, it is possible to inject the fields into an object created with new, simply with BeanProvider#injectFileds. I tested this myself:
#Produces
public EggMaker eggMakerProducer() {
EggMaker eggMaker = new OmeletteMaker();
BeanProvider.injectFields(eggMaker);
return eggMaker;
}
I am working on a piece of code wherein I have a Spring Bean say B.java.
The constructor for this Bean depends upon 2 arguments.
Additionally this Spring Bean has another Spring Bean, say C.java injected to it.
I have a factory class say Factory.java that has a getInstance() Method that returns a new object of type B.java depending upon the passed in constructor parameters.
Now I want to inject the returned instance of class B in another class, say C.java which is a Spring Bean with all its properties instantiated i.e the reference to C.java as well.
Can somebody please help in creating context.xml files for this.
Following is a rough prototype of the class structures:
public class B{
private String arg1;
private String arg2;
private C c;
public void setC(C c){
this.c=c;
}
B(String arg1, String arg2){
this.arg1 = arg1;
this.arg2 = arg2;
}
}
................................
public class Factory{
private String arg1;
private String arg2;
public B getInstance(){
return new B(arg1, arg2);
}
}
...............................
public class C{
#Autowired
private B b;
#Autowired
private D d;
}
Thanks
Recently I have worked with asp.net mvc and I have seen in sample project is using Database Factory class. How can you explain for me why use it ?
IDatabaseFactory class
public interface IDatabaseFactory : IDisposable
{
EFMVCDataContex Get();
}
DatabaseFactory class
public class DatabaseFactory : Disposable, IDatabaseFactory
{
private EFMVCDataContex dataContext;
public EFMVCDataContex Get()
{
return dataContext ?? (dataContext = new EFMVCDataContex());
}
protected override void DisposeCore()
{
if (dataContext != null)
dataContext.Dispose();
}
}
This is an example of an Abstract Factory design pattern. The idea is to create a seam to provide loose coupling between the classes so another type of context could be swapped, either for testing purposes or to extend the application.
Generally speaking, a factory is a way to manage short-lived dependencies, such as database connections. Typically, a framework exposes a way to inject an instance of the factory and then the framework can work with it based on an interface (in this case IDatabaseFactory) as a contract between the framework, and the framework user. The framework will have code that looks something like this:
public interface ISomeService
{
void DoSomething();
}
public class SomeService()
{
private readonly IDatabaseFactory factory;
// The factory is injected through the constructor
public SomeService(IDatabaseFactory factory)
{
this.factory = factory;
}
public void DoSomething()
{
using (EFMVCDataContex context = this.factory.Get())
{
// Run a LINQ query here using the context
} // This bracket disposes the context
}
}
The service can then be instantiated for a much longer lifetime than the context that is created by the factory. What's more is that the context is always properly disposed in this scenario.
Now, the main benefit from doing this is that you can swap the DatabaseFactory with an alternate implementation (commonly referred to as the Liskov Substitution Principle):
public class MyDatabaseFactory : Disposable, IDatabaseFactory
{
private EFMVCDataContex dataContext;
public EFMVCDataContex Get()
{
return dataContext ?? (dataContext = new AlternateDataContext());
}
protected override void DisposeCore()
{
if (dataContext != null)
dataContext.Dispose();
}
}
Assuming that AlternateDataContext inherits (or implements) EFMVCDataContex, MyDatabaseFactory can be swapped apples-for-apples with DatabaseFactory without making any changes to SomeService.
MyDatabaseFactory could be coded with a connection string in the constructor, giving you a way to connect to alternate databases, for example.
Of course, another great benefit of doing this is to create a mock implementation of IDatabaseFactory that can be used to test the DoSomething method. In a unit test, SomeService (the class under test) should be the only real class being used, IDatabaseFactory should be a mock (which could either be done by hand coding a class, or using a mocking framework).
Please forgive my ignorance, but I am very new to IOC and NinJect. I have searched for high and low for easily understandable solutions but so far they have eluded me.
So far I have the following and all works as expected:
private class StandardModule : NinjectModule
{
public override void Load()
{
Bind<ILog>().To<NLogLogger>(); // Use NLog
Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>();
}
}
MyEntityFrameWorkRepository then creates its own EF DbContext via a connection string declared in app/web.config:
public class MyDbContext : DbContext
{
public MyDbContext() : base("MyAppConfig")
{
}
........
}
HOWEVER!! My goal is something like this - I realise this syntax is "nonsense" (and I think I may have to IOC MyDbConext too) , but I hope the "pseudo-code" conveys my desire:
private class StandardModule : NinjectModule
{
public override void Load()
{
Bind<ILog>().To<NLogLogger>(); // Use NLog
string mySqlConnectionString = MyApp.GetCommandLineArgument("sqlconn"); // "Data Source=..."
Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>(mySqlConnectionString);
}
}
.................
public class MyDbContext : DbContext
{
public MyDbContext( string sqlConnectionString) :
base(sqlConnectionString) // will accept a standard SQL connection string
{
}
........
}
I would truly appreciate some feedback from IOC / NinJect experts, since I am sure any "pattern" can be very useful in other scenarios.
You can use the .WithConstructorArgument() method to specify constructor arguments. The first argument should be the name of the constructor parameter.
public class StandardModule : NinjectModule
{
public override void Load()
{
string connectionString = "...";
Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>()
.WithConstructorArgument("sqlConnectionString", connectionString);
}
}
Newer versions of Ninject allow to get rid of magic strings in the binding definition. Something like this:
public class StandardModule : NinjectModule
{
public override void Load()
{
string connectionString = "...";
Bind<IMyEntityFrameWorkRepository()
.ToConstructor(_ => new MyEntityFrameWorkRepository(connectionString);
}
}
For bindings involving generic types (e.g. bind ISomeService<T> to SomeService<T> and binding should be performed for all possible types at once), ToConstructor cannot be used (a new expression is required), so WithConstructorArgument remains the simplest approach. E.g.:
Bind(typeof(ISomeService<>))
.To(typeof(SomeService<>))
.WithConstructorArgument("someParam", "someValue");
Having seen how NInject can do it and AutoFac can do it I'm trying to figure out how to inject dependencies into MVC ActionFilters using Castle Windsor
At the moment I'm using an ugly static IoC helper class to resolve dependencies from the constructor code like this:
public class MyFilterAttribute : ActionFilterAttribute
{
private readonly IUserRepository _userRepository;
public MyFilterAttribute() : this(IoC.Resolve<IUserRepository>()) { }
public MyFilterAttribute(IUserRepository userRepository)
{
_userRepository = userRepository;
}
}
I'd love to remove that static antipattern IoC thing from my filters.
Any hints to as how I would go about doing that with Castle Windsor?
And no, changing DI framework is not an option.
When I needed this, I built upon the work others have done with Ninject and Windsor to get property injection dependencies on my ActionFilters.
Make a generic attribute: MyFilterAttribute with ctor taking a Type as argument - i.e. something like this:
public class MyFilterAttribute : ActionFilterAttribute {
public MyFilterAttribute(Type serviceType) {
this.serviceType = serviceType;
}
public override void OnActionExecuting(FilterExecutingContext c) {
Container.Resolve<IFilterService>(serviceType).OnActionExecuting(c);
// alternatively swap c with some context defined by you
}
// (...) action executed implemented analogously
public Type ServiceType { get { return serviceType; } }
public IWindsorContainer Container { set; get; }
}
Then use the same approach as the two articles you are referring to, in order to take control of how actions are invoked, and do a manual injection of your WindsorContainer into the attribute.
Usage:
[MyFilter(typeof(IMyFilterService))]
Your actual filter will then be in a class implementing IMyFilterService which in turn should implement IFilterService which could look something like this:
public interface IFilterService {
void ActionExecuting(ActionExecutingContext c);
void ActionExecuted(ActionExecutedContext c);
}
This way your filter will not even be tied to ASP.NET MVC, and your attribute is merely a piece of metadata - the way it is actually supposed to be! :-)