Java - Inject Bean inside method - java

I want to inject bean inside my method. I have remote method(Direct Web Remoting) and i need to inject some bean inside this method. I can't use #Inject annotation in field declaration section because it will not work. It is even possible ?

It could be possible using serious bytecode instrumentation, but that's probably not feasible.
Does DWR prevent you from using regular Injection?

There are lots of ways to do something like this. What container do you run? DWR doesn't seem to support CDI yet.
Apache DeltaSpike has a way you can manually inject beans. One way is to do a call like this:
EchoService echoService = BeanProvider.getContextualReference(EchoService.class, false);
Another is to enrich the actual class at runtime. Take a look at getBeans in BeanManager.

Related

Is extending an existing webflowconfigurer (of the CAS webflow) feasible?

In this blog post, the subject of creating a new Webflowconfigurer to extend the web flow is covered.
In the examples provided this is done through classes that extend the AbstractCasWebflowConfigurer and introducing new actions that are appended to the webflow through the included process.
Is extending already existing configurers like for instance AcceptableUsagePolicyWebflowConfigurer and overriding some of its methods feasible or is that outside the scope of CAS web flow? If its feasible, what is the correct way to do this?
p.s. currently on version 5.3.x
Is extending already existing configurers like for instance AcceptableUsagePolicyWebflowConfigurer and overriding some of its methods feasible or is that outside the scope of CAS web flow?
Yes, that is feasible.
If you examine this block, you will find that AcceptableUsagePolicyWebflowConfigurer is only created conditionally, if an existing bean is not already found in the context by the same name. So to provide your own, you just need to register a bean with the same name using your own #Configuration class. Something like this:
#Bean
#DependsOn("defaultWebflowConfigurer")
public CasWebflowConfigurer acceptableUsagePolicyWebflowConfigurer() {
return new MyAcceptableUsagePolicyWebflowConfigurer(...);
}
public class MyAcceptableUsagePolicyWebflowConfigurer extends
AcceptableUsagePolicyWebflowConfigurer {}
To learn about how #Configuration classes work in general, you can:
Review this post
or This post
or consult the documentation for Spring and/or Spring Boot.
Is there an example of extending the cas login webflow using Java for cas 6.x. I assume this can be done without having to modify the login-webflow.xml.
https://apereo.github.io/cas/6.1.x/webflow/Webflow-Customization-Extensions.html does not explain very well where these changes need to be made at.

How to configure Spring AOP to use AspectJ

I want to use AOP concept to time execution time of some methods that I mark with an annotation that I created. My problem however is that I refer to the annotated method internally, from within the same class. For example:
public void login(params) {
some logic ...
performLogin();
some logic ...
}
#Measured
public void performLogin() {
some logic ...
}
This is a known issue caused by the fact that Spring AOP is using proxy based approach that does not "see" the internal calls within the same class. Apparently I can solve this situation by using AspectJ instead of Spring AOP. If I understand correctly, it can be configured from within Spring itself. From what I found, it looks like I should include #EnableAspectJAutoProxy annotation to configure Spring to use AspectJ instead of its own AOP. Unfortunately, it did not help and after adding the annotation, the interception of the annotated method did not occur.
There is a lot of information on this topic in Spring reference documentation and I got a bit lost. Is there anything else I am supposed to do so that AspectJ will be used?
P.S. Please note that I cannot refactor the whole class and move the calling method outside.
P.P.S. I also verified my pointcut configuration. I annotated the calling method which is invoked externally and it worked fine.
Proxies can only achieve a sub-set of the full capabilities of the actual AspectJ system, basically advice that wraps methods. Due to their nature proxies have following limitations:
interception on external calls only (while breaching proxy boundary)
interception on public members only (private/protected can't be intercepted)
unawareness to local calls (or calls with this or super)
<aop:aspectj-autoproxy /> is not enough - it only wraps methods, you need something like this: <context:load-time-weaver/>
If you want to be able to advise fields for example, you would need to enable the use of Native AspectJ.

What's the best place to create Guice injection when handling GET with a servlet?

Injector has to be created at some point. Currently I put in into static ctor of my servlet. But somehow it looks fishy. Furthermore I am calling getInstance directly from goGet to create all sort of classes. That may not be too bad but there are some limitations that I have to work around. So is there a better way?
Check out the Guice Servlet extension. You can #Inject your Guice managed classes directly into your servlets, filter your requests, and also gain access to useful scopes like #RequestScoped and #SessionScoped.

Best/most clean way to inject dependencies into Servlets without any frameworks

What is the best way to inject dependencies into Servlets when you don't want to use any sort of DI frameworks? Shall I put them into the ServletContext in a ServletContextListener?
Yes. You could initialize them in a ServletContextListener (if you need them pre-initialized) and then put them into the ServletContext for all your servlets to access.
It's probably best to store the objects by their class name so retrieval is type-safe.
Foo foo = servletContext.getAttribute(Foo.class.getName());
To inject something in servlets, you need to get the servlet instances in another class. And you can't do that, because the getServlet(name) method is deprecated (and not working).
So each servlet will have to register itself manually in the context. In the init() method of each servlet you can add itself to a collection in the servlet context:
((List<HttpServlet>) servletContext.getAttribute("servlets")).add(this);
Then, in a ServletContextListener you can loop all registered servlets and invoke some setters, or user reflection, to externally set the dependencies.
But..that seems too complicates, so you might stick with the new operator here and there.

In what case would a programmer get the webApplicationContext out of DispatchServlet?

I notice that there is getWebApplicationContext in org.springframework.web.servlet.mvc.AbstractController. This means that spring programmers can use getWebApplicationContext to access beans in the spring IoC container.
However, I never see people use this way to get beans in all the spring MVC tutorials. So here comes my question, in that case would a programmer want to get the WebApplicationContext?
That's an odd question... are you asking why a method is in the API if none of the tutorials use it? Do you expect every API method to be in the tutorials?
The getWebApplicationContext() method is rarely used by application code, but it is used internally by Spring for some tasks.
In some cases when you implement org.springframework.web.servlet.View a call to getWebApplicationContext can be useful to get access to Spring beans that can not (or should not) be passed along in the Model object.
You also might need it when implementing custom JSP tags to access Spring Beans e.g. But this is usually a method you try to avoid from application code.

Categories