Injecting jaxws:endpoint configuration published URL - spring

<jaxws:endpoint id="serviceEndpoint" implementor="#serviceImpl" address="/public/api/service/v1"" publishedEndpointUrl="#{serviceImpl.endpointUrl}"> </jaxws:endpoint>
<bean id="serviceImpl" class="com.services.ServiceProviderImpl" autowire="byType"> </bean>
I need to populate publishedEndpointUrl refering a variable in my implementor bean. (ServiceProviderImpl) Please let me know the way I can make it. I refereed http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html.
Making it publishedEndpointUrl="#{serviceImpl}.publishedUrl" does not make work ?
Thanks

Assuming ServiceProviderImpl has a public getter getPublishedUrl(), the syntax you need is
#{serviceImpl.publishedUrl}
In future, when adding questions here, "does not work" is not useful at all.
Always provide the error message and/or stack trace.

Related

Single spring bean with two different ids in same xml(application context)

If we create a new another bean with different id in the same xml for the same class, will spring produce another singleton bean(in same ApplicationContext)?
As per my understanding there should be only one instance of the bean in single ApplicationContext.
Below example-
<bean id="bean1" class="com.myCompany.myPackage.MyClass" scope="singleton" />
<bean id="bean2" class="com.myCompany.myPackage.MyClass" scope="singleton" />
To keep it short: No, the singleton only says that you'll have: "one shared instance, which will be returned by all calls to getBean with the given id" (that's what the documentation states).
So, you can do any number of calls to application context and obtain "bean1" and you'll always get the same instance, but if you call by "bean2" id, you'll get another instance.
The "singleton" says that you'll have only one object. Now in a non-Spring application, you'll have it usually per JVM. But in spring application, let the framework manage this. So usually you'll want to define only one class like "MyClass" with a scope singleton.
When dependency management container (Spring in this case) manages singletons, it has a lot of advantages over the 'regular' singleton. Just to name a few:
Much easier to test
You always know when the object is created and when it becomes subject to garbage collector
No static code (the Spring driven singleton is just a regular bean with no statics)
But in general it's not directly related to your question.
I did have this same question just in a different context of Spring's singleton vs. Java's singleton and I found this answer provided by 'Dexter' in this link more subtle and easy to understand.
Also, this blog here provides a perfect example for the same which is backed by the official spring documentation for better-detailed understanding.
Hope these pointers help. Thanks.

Constructors in Bean

I have one small issue with creating a new bean. Basically as per request, I get some parameters, which needs to be passed to a bean. Below I am instantiating ControllerService for each request. Rather I would like it to be a bean with scope=protype. So that I get a fresh object for every request.
But then how do i set the 2 properties (kpiName, kpiInput) that I am sending via constructors in the bean??
#Autowired
#Qualifier("serviceManager")
Cleanser serviceManager;
#RequestMapping(method = RequestMethod.POST)
public #ResponseBody
String getKPIResult(#RequestParam("kpiName") String kpiName,
#RequestParam("kpiInput") String kpiInput) {
return serviceManager.checkAndExecute(new ControllerService(kpiName, kpiInput));
}
In situations like this where you're going against the grain of Spring, I'd suggest that perhaps you're doing something in a way that's not considered best practice. Without more context it's hard to see though.
Spring Social uses a request scope bean to embody a repository for a specific user. I've now idea why as it's a horribly inefficient way of doing things, and much less understandable IMHO.
<bean id="connectionRepository" factory-method="createConnectionRepository"
factory-bean="usersConnectionRepository" scope="request">
<constructor-arg
value="#{T(org.springframework.security.core.context.SecurityContextHolder).getContext().getAuthentication().getPrincipal()}" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>
You can see here the use of factory-bean and factory-method to declare a class/method to call when wanting an instance of your class. The constructor argument is passed using SpEL. I'm not quite sure how you'd achieve this with Spring MVC responding to web requests, but I'm fairly sure you could use Spring Integration to pass a message and use SpEL to grab headers/payload form that message to pass to the constructor.
Again though, I'd really question your design pattern here - a more usual SOA idiom is to create services on startup, and have them as stateless as possible from there-on in, rather than create an instance with specific state for each request. Best of luck!
Don't. The Controller as it's intended in Spring MVC is largely derived from the old Java servlet, which should be stateless by specification.
In fact, Controller-objects are hard-cached inside the handler mapping framework and not fetched from the bean context on each request. Setting scope to "prototype" would, effectively, do nothing as the handler (controller) is really only gotten once.

Make a check on AOP pointcut expression

I need to make a check in my pointcut expression like.
I have this bean:
<bean id="logConfig"
class="com.celfocus.ufe.base.logging.domains.LoggingConfiguration">
<property name="logDetails" value="STANDARD" />
<property name="logLvl" value="COMPLETE" />
</bean>
In my aop pointcut expression i need to make a check to verify the value of bean property "logLvl".
<aop:config>
<aop:aspect ref="ufeLogger">
<aop:pointcut id="complete" expression="execution(* *.*(..)) and bean(logConfig)==COMPLETE" />
<aop:before pointcut-ref="complete" method="logBefore" />
</aop:aspect>
</aop:config>
My expression isn't working... what I can change to make this check?
What makes you think that and bean(logConfig)==COMPLETE is a valid pointcut? Spring AOP uses AspectJ pointcut syntax, no Spring additions. Also you are not even referencing logLvl property, so has is this suppose to work?
Unfortunately to achieve this you must implement check manually. This isn't so intrusive though: simply inject logConfig into ufeLogger aspect and add a simple condition in logBefore() method.
This is the most relevant question, for solution below. I write aspect expressions from time to time. And expression might not work. You need to write right expression, which will be matched with your target method. I found a simple decision to check that everything is clear:
#Around(value="execution(* *.find(..))")
public Entity filterEntity(ProceedingJoinPoint pjp) throws Throwable {
Entity entity = (Entity) pjp.proceed(); // put breakpoint here
}
In Debug mode you can check target method real signature that is used for match with expression, in path pjp.methodInvocation.method
I hope this answer will save your time to find an error.
P.S If there are exists better decision to check expressions, glad to see it

c3p0 useScatteredAquireTask Spring Bean

Hello I wan't to know how can I set up a bean so that it sets the ScatteredAquireTask to "True".
I've been trying:
<bean id="c3p0Props" class="com.mchange.v2.resourcepool.BasicResourcePool.ScatteredAcquireTask" >
<property name="USE_SCATTTERED_ACQUIRE_TASK" value="true" />
</bean>
I also tried ...resourcepool.experimental.useScatteredAcquireTask... didn't worked. I'm not sure how can I set this on spring. I'm using 0.9.1.2, can't go to 0.9.2.prep1 at the moment. Thanks.
That's because USE_SCATTTERED_ACQUIRE_TASK isn't a property of the ScatteredAcquireTask class (i.e. there's no method called setUSE_SCATTTERED_ACQUIRE_TASK), it's an internal static field of the class that's not accessible to Spring.
You're not going to be able to set that values in a Spring bean defintion, you need to find out how to influence that value by some other means.

How to create two instances of the same spring controller, each with a different base #requestMapping

I have a spring controller (MyController) which servers data as json.
With a few configuration changes, I will be able to reuse the same controller and have it serve the same data, but as xml, not json.
I'd love to be able to create myControllerInstanceA, and configure it to use /json as a base url, then create myControllerInstanceB and have it use /xml as a base url.
The only way I can think of to do this is to subclass MyController, and set the subclass's #requestMapping to /xml. I'd rather be able to do some configuration in my springap-servlet.xml to achieve the same effect.
Is this possible?
I'm guessing some of you spring wizards reading this might be thinking "why the heck would he want to do that". So I'll explain the techniques i'm using: I'm creating a controller, which adds simple java beans to a ModelAndView. The controller also ads a view. The view takes the java beans and serializes them to json, or to xml, depending on how the controller was configured. I think there is probably a more Spring-ish way to do this, but this approach seemed simple and straightforward enough. Also, it allows me to work with a JSON library I'm familiar with, rather than the one that Spring seems set up to use. Points for anyone who tells me the Spring way to do it -- how to easily serve the same data as either json or xml, reusing controller code as much as possible.
Use ContentNegotiatingViewResolver to resolve the views. This resolve will use different configured views to render the model based on the request's Accepts Header or extension. By default it uses the MappingJacksonJsonView for JSON and you will have to configure an Xml Marshaller for use with the MarshallingView.
With this configuration you can have each annotated method support infinite data formats.
Check out this example.
I'm not sure if you're asking for this, but Spring 3 has ContentNegotiationResolver which can help to return json or xml:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json"/>
</map>
</property>
</bean>
And in the controller you can map json and xml to the same controller method.
#Controller
class MyClass(){
#RequestMapping(value={"/yourURL.json", "/yourURL.xml"})
public Object yourController(){
return Object
}
}

Resources