Can serializable class be used as command class - spring

I am using spring mvc 2.5 . I have a serializable class (For instance A class), but now I use this A class as command class.
Can a serializable class be used as command class? If yes, how?
In servlet, I define this A class as command class, in formbacking method, I created object from this A class but in formbacking, when I try to return the object created by this A class, it is not successful.
Additionally, I want to save data coming from jsp file into database by using A.hbm.xml

I have solved the problem. It is not important that class is serilizable class or not. Mapping can be done correctly.

Related

How does spring boot #Value("${somevalue}") annotation work?

I have some #Value annotation in spring-boot project. To Simplify, I have few classes: a restcontroller, service (annotated with #Service) and a pojo.
In each of these classes, I declared a variable as below:
#Value("${somevalue}")
private String somevalueVariable
In the controller class, the value is getting populated as defined in the application.properties. So no problem here.
In the service class, the value is showing up as null. This is my issue, how should i fix it to get the value from the application.properties
In the pojo, the value is showing up as null, I am thinking this is expected behaviour as spring does not manage this class.
Try this:
import org.springframework.beans.factory.annotation.Value;
#Value("#{${somevalue}}")
private String somevalueVariable
ideally service class should have #Service anotation over it, either you missed that or this class is not scanned by spring context, so please add ComponentScan anotation for service class package over main class to scan classes uner this package -
#SpringBootApplication
#ComponentScan({"com.in28minutes.springboot"})
public class Application
It uses Spring Expression Language (SpEL):
https://docs.spring.io/spring-integration/docs/5.3.0.RELEASE/reference/html/spel.html
Also there is 2 #Value : org.springframework.beans.factory.annotation.Value and lombok.Value;
Make sure you are using the right one.
To get value from property try this:
#Value("${systemValue}")
private String systemValue;
For more information I find this useful:
https://www.baeldung.com/spring-value-annotation

How to extend Laravel core class MessageBags

I need to extend Illuminate\Support\MessageBag because of add some help methods but I cant bind my own class App\Support\MyMessageBag to laravel use my class (that extends App\Support\MyMessageBag) instead of original core class.
Do you have any ideas?

Why does spring container create beans even for the class with default access without checking the access restrictions?

I have a package "A" with interface "Shape" and implementing class "Rectangle". Both the interface and class have default access. Now I have another package "B" with class "ShapeTester". In this ShapeTester class, I try to instantiate class Rectangle or try to declare a variable of type Shape and I get the compilation error which is expected.
But in the ShapeTester class, if I use the Spring container to create the bean, it creates the bean for class Rectangle.
Is it not wrong of Spring Framework to create the beans without even checking that the class where the bean is being created for Rectangle doesn't even know that any such Rectangle class even exists?
Spring just create the object instances. It does not matter from which class or package you start the spring container.
The java concept of visibility is more related to the static relationship. For example if you have a class A in package a, then all rules are based on this. It does not matter if class A is instantiated by an Class B from an other package.

Inject code into method with Javassist

I'm trying to add some code to a class that is inside a jar(maven dependecy) and i'm doing it in the following way:
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.get("xyz.abc.ClassInADependecy");
CtMethod method = ctClass.getDeclaredMethod("getSomeValue");
method.insertBefore("{ System.out.println(\"modified\"); }");
I'm using Spring and the above code is being called using a #Configuration annotation.
When i call the method getSomeValue nothing is printed.
Can you help me find out what i'm doing wrong?
Thank you very much.
You are only changing the implementation as it is represented in Javassists type pool. You have to make sure that the class is also loaded by the respective class loader. Also, this must happen before the class is loaded for the first time, i.e. before your Spring application loads that class.
One way to do so is to manipulate the class from a Java agent: https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html

Can a controller use the same instance of command from a controller it extends?

I have multiple SimpleFormController controllers. One controller contains a command object used to filter results in the other controllers. I seem to be missing something simple, but I can't seem to find a way to use the same instance of the command object in the other controllers.
My setup is such that this main controller, let's call it RootController extends SimpleFormController, and the rest of the controllers extend RootController. The idea was that the command object is stored in one place - RootController and the controllers that extend it reuse the same object. However, it doesn't seem to be working that way, other controllers seem to have their own copy of the command object.
Form backing objects are just normal Pojos, so you can inherit it form each other.
public class BaseCommand {
...
}
public class MoreCommand extends BaseComman {
...
}
May you just forget the "update" the commandClass in your Controller Subclasses.
Anyway: notice that SimpleFormController is deprecated in Spring 3.0. Instead the Annotation Style is preferred.
Update: One INSTANCE of an command object, can be handled by only one INSTANCE of an Controller. So you can subclass the Controller (don't miss to call super), but you can not have two instances of the controller and hope that both are invoked.

Resources