Where to store custom app parameters in Spring MVC? - spring-mvc

I need to store "hostName" parameter in my Spring MVC application configuration (for writing links in templates to static recourses, which are on static.hostName). I assume hardcoding this is bad, so where should I store it?
Web.xml, or servlet-context.xml? And how do I get it?
Thanks.

With a PropertyPlaceholderConfigurer you can externalize beans (and primitives) into a properties file. You can inject those beans with SpEl and #Value:
#Value("${hostname}")
private String hostname;
You can find a configuration example in the reference documentation.

Related

Unable to access Controller Annotation data from Spring Controller

I am developing a Maven plugin which generates some application metadata related to Spring Controllers. Currently I am unable to get the "Controller" annotation (with its "value" property) of my spring controllers so I am kinda stuck...
How could I get this annotation and its value ?. Thankx
This code let me get the desired values. Thankx to all

Implementing some logic during the application startup itself in Spring MVC

I am trying to create a Spring MVC application. While starting the application in server itself I need some to implement some excel file reading logic. I am new to Spring. Can I use interceptors for this? Once the excel is read it will be saved in a map and that map will be used for the rest of the things.
Use a PostConstruct method to achieve the desired requirement. This will be called when all the dependencies are injected and when container is brought up.
#PostConstruct
public void loadData(){
// Read from your excel file here.
}

ASP.NET 5 dependency injection outside of Controllers and Views

Each tutorial or example that I've found for using DI in ASP.NET 5 only shows how it works with Controllers and Razor Views. I need to use DI for other classes but don't know the proper way to resolve types and provide an instance. Right now I have an instance of a HackyDependencyResolver that everything must reference in order to get the proper instances. I want to either access ASP.NET's service resolver or follow some other best-practice for resolving dependencies.
For example if I have
public class SomeClass
{
public SomeClass(IUseMe useMe)
{
}
}
which is not an ASP.NET MVC Controller. I need a pattern for resolving a correct instance for IUseMe when SomeClass is created. Certainly I can make my own global factory, but is that the best way?
DI has nothing to do with asp.net, controllers or views. In the end all are classes. Considering that an action is an entrypoint in your app, any service you need there should be injected, The service itself has some dependencies and those will be injected automatically by the DI Container when it instantiates the controller.
All you have to do is to define your services (not every object needs injected deps) then register those services into the Di Container.
How do I resolve IUseMe so that I'm not dependent on a particular implementation?
You don't. The Di Container does that based on configuration, when the controller is instantiated. Everything has a flow, you don't just pick classes out of thin air and say "I want this created by the Di container". OK you could, but it would be the wrong approach.
The whole point of using a DI Container is not to care about instantiating services. The framework takes care of integrating with the container, your only job is to define the classes properly and to configure the container .

Symfony 2: Collect controllers annotations on KernelRequest

Actually I want to store all the routing in the database. I wrote custom annotation class called VendorRoute I can read this annotation for a single controller, but does anybody know how to read this annotation for all controllers in application?
So I solved my problem. In Sf2 it's impossible out of the box. Check out my implementation https://github.com/Codeforges/CayenneCoreBundle/blob/master/Annotations/Driver/CayenneRouteDriver.php#L83

Ninject.Web (webforms extension), injecting outside of a webform page?

I've been using the Ninject.Web extension to inject business objects, repositories, Entity Framework context etc into my application. This works very well using the [Inject] attribute which can be applied within a webform that inherits from PageBase. I am now running into a snag as I am trying to write a custom membership provider that needs injection done inside of it but of course this provider is not instantiated from within a webform. Forms Authentication will instantiate the object when it needs it. I am unsure how to about doing this without having access to the [Inject] attribute. I understand that there is an application level kernel somewhere, but I have no idea how to tap into it. Any suggestions would be greatly appreciated.
You don't have to use the service locator pattern, just inject into properties of your custom membership provider in Application_Start. Assuming you've registed the providers properly you can do this with something like:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
// Inject account repository into our custom membership & role providers.
_kernel.Inject(Membership.Provider);
// Register the Object Id binder.
ModelBinders.Binders.Add(typeof(ObjectId), new ObjectIdModelBinder());
}
I've written up a more in depth explanation here:
http://www.danharman.net/2011/06/23/asp-net-mvc-3-custom-membership-provider-with-repository-injection/
You do a IKernel.Inject on the the instance. Have a look at the source for the Application class in the extension project you're using.
In the case of V2, it's in a KernelContainer. So you need to do a:
KernelContainer.Inject( this )
where this is the non-page, non application class of which you speak.
You'll need to make sure this only happens once - be careful doing this in Global, which may get instantiated multiple times.
Also, your Application / Global class needs to derive from NinjectHttpAppplication, but I'm sure you've that covered.
you may need to use the Service Locator pattern, since you have no control over the creation of the membership provider.

Resources