How to add ASP.NET MVC Controller in Spring.Net Application Context? - asp.net-mvc-3

I am creating a plugin system for my ASP.NET MVC Application using Spring.Net IOC Framework.
I have placed my Controllers in a different assembly, I need to add these controllers to Spring.Net Application context dynamically.
Please guide me !!
I am trying to implement plugin system like following article but I want to use Spring.Net instead of Autofac.
http://www.codeproject.com/Articles/386674/ASP-NET-MVC-3-plug-in-architecture-using-Griffin-M

I have used Spring.Net and in it i've accessed Repository objects using Application Context
Try this
IApplicationContext ctx = ContextRegistry.GetContext();
dynamic controller= ctx.GetObject("MyController");
where MyController is the ID of conttoller that you've defined in your controller.xml file
<object
id="MyController "
type="Swapeteria.Web.Controllers.ItemPostController, Swapeteria.Web"
singleton="false">
<constructor-arg name="repository" ref="ItemRepository" />
<constructor-arg name="bookRepository" ref="BookRepository" />
<constructor-arg name="authorrepository" ref="authorRepository" />
<constructor-arg name="publisherRepository" ref="PublisherRepository" />
<constructor-arg name="itemMapper" ref="itemMapper" />
</object>

Related

Moving mvc interceptor from module-context to web-application context

Can we move mvc interceptors from individual context file to web-application context.xml
Thus removing interceptor from individual module context?
You can put them in the web-context as follow:
<mvc:interceptors>
<bean class="com.package.TheInterceptor" />
<bean class="com.package.AnotherInterceptor" />
</mvc:interceptors>

spring web flow enable scopes

Spring web flow provides additional bean scopes like flow, conversation, flash etc. I can define flow scope beans in flow.xml using var or i can set values to new scoped variables. How i can define it in spring application context xml file. I tried to use this pattern:
<bean id="abc" class="abc" scope="flow"/>
I got error that no scope defined. I searched on google and found this thing
http://blog.springsource.org/2007/05/08/spring-web-flow-bean-scopes-and-jsf/
but don't know how to enable it in spring web flow 2.3
try to define it in your application context:
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="flow">
<bean class="org.springframework.webflow.config.scope.FlowScope"/>
</entry>
</map>
</property>
</bean>

Required Some Best practices to detect the locale of a user using jsf2 + spring web flow

I am just trying to figure out some good approches to detect the locale.
The approach I am following is :
1-One language Bean with the below code
<code>
if(!classUtil.isNullObject(FacesContext.getCurrentInstance())){
return FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
}
return selectedLanguage;//Return default language in case its not possible to detect the language
</code>
2- creating bean definition inside parent-flow
<code><var name="languageBean" class="com.decitysearch.classified.LanguageBean"/></code>
3- using the same in the xhtml
<code>
<f:view locale="#{languageBean.findDefaultLocale}">
<f:loadBundle var="messageResource" basename="MessageResource_en"/>
</code>
My questions here it goes like:
1-can't we make the bean entry inside spring-context rather than flow bean as I tried with spring-context but getting some scope exceptions.
2-Is there any good approaches to detect the locale
your thought process is highly appreciated.
If you are using Spring, declare these beans and call URL with parameter "locale=xx" to change locale:
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="es" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

about spring social xml configuration

i wonder "#{request.userPrincipal.name}" in configuration blow. when I run my spring social project it always has error at "#{request.userPrincipal.name}", if I set a value such as "123" my project runs well. what's wrong and is there any other configuration instead of "#{request.userPrincipal.name}" ?
<bean id="connectionRepository" factory-method="createConnectionRepository" factory-bean="usersConnectionRepository" scope="request">
<constructor-arg value="#{request.userPrincipal.name}" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>
This is a Spring EL expression, which means getting the user identity from the Http request. Once you apply your own User Management component, you can replace #{request.userPrincipal.name} with your own way.

Spring, XML beans call Annotation beans when app start

I have one Annotation bean with some methods. It works fine.
public #Controller("adminController") class AdminController {
...
private #Autowired AdminDAO adminDAO;
public void resetTemporalList() {
System.out.println("HE SIDO EJECUTADO.");
this.adminDAO.resetTemporalRegisters();
}
...
}
Now, I am integrating one quartz task. But I am load it with XML definition beans that call previus annotation bean.
<bean id="resetTemporalRegisters" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="adminController" />
<property name="targetMethod" value="resetTemporalList" />
<property name="concurrent" value="false" />
</bean>
Whan I start my app appear next error.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'adminController' is defined
I believe the problem is that Spring load XML beans first, after Annotation beans, then in this moment "adminController" bean not exits...
How Can I fix it?
SOLVED IT!!
Problem was in I put xml bean definitions in applicationContext.xml.
No, XML and annotations integrate fine, but do you actually have the component scanning code in your XML?
<context:component-scan base-package="com.yourcompany.yourapp"/>
See: 4.10 Classpath scanning and managed components
A little bit of guessing: your controller is defined in child application context created by Spring MVC while you resetTemporalRegisters job in main application context (parent). Child context can access beans from parent context but not the other way around.
This raises important question: why is your business logic trying to call a method of a controller? These methods should be called only be the MVC framework. Can't you just call
this.adminDAO.resetTemporalRegisters();
directly from your job?
<bean id="resetTemporalRegisters" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="adminDAO" />
<property name="targetMethod" value="resetTemporalRegisters" />
<property name="concurrent" value="false" />
</bean>
adminDAO is probably defined in parent context, so you can access it easily.

Resources