#Value annotation is null when I am using it in #RestController [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
I have one #RestController where I want to use a value from application.properties file with #Value annotation, but the value is always null.
How I can achieve this?
I've checked that application.properties file is loaded and the value is there.
#RestController
public class TestController {
#Value("${test}")
private String testValue;
#RequestMapping(
method = RequestMethod.GET,
value = "/test",
produces = { "application/json" }
)
public ResponseEntity<Void> test() {
System.out.println("Test value: ", testValue); // testValue is always null here
}
}

Make sure you are using org.springframework.beans.factory.annotation.Value and not lombok.Value.

Related

How to get Spring bean by name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I get the bean by passing only the class name using BeanFactoryUtils
I'm trying below, but this is not working
import org.springframework.beans.factory.BeanFactoryUtils;
baseDao= BeanFactoryUtils.originalBeanName("RegionDaoImpl");
RegionDao
#Component
public class RegionDaoImpl implements BaseDao<Region> {
...
}
Any suggestions?
You need a ListableBeanFactory, then you call beanOfType(), e.g.:
RegionDaoImpl dao = BeanFactoryUtils.beanOfType(beanFactory, RegionDaoImpl.class);
Generally, the ListableBeanFactory will be an ApplicationContext, so you need the application context of your Spring application. How to get that depends on your application type, and where the code calling beanOfType() is located.
It is usually better to let Spring auto-wire the object into your class, which does the same thing, i.e. lookup the bean by type.
#Component
public class SomeComponent {
#Autowire
private RegionDaoImpl regionDao;
...
}
If you want to lookup by name, you'd call beanFactory.getBean(), but that seems kind of redundant:
RegionDaoImpl dao = beanFactory.getBean("RegionDaoImpl", RegionDaoImpl.class);

SpringBoot - CMS structure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am implementing a Spring Boot application that will be a micro-service.
This micro-service is supposed to define a CRUD #RestController over a CMS.
Is there any standard way of doing this, or any library I can take advantage of?
i.e:
#RequestMapping(value = "/simple/{key}", method = RequestMethod.GET)
public String getKey(#PathVariable final String key) {
return getCmsKey(key);//Retrieve key
}
#RequestMapping(value = "/collection/{key}", method = RequestMethod.GET)
public List<String> getKeys(#PathVariable final String key) {
return getCmsListOfKeys(keys);//Retrieve collection of keys
}

How jsp can access getters and setters in superclass, Spring MVC [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using spring MVC
I have some class with property, and getter\setter to get access to this property
class a{
private String something;
public void setSomething(String something){
this.something = something;}
public String getSomething(){
return something;
}
And I have subclass with some new property, getter and setter in it
class b extends a{
private String newProp;
public void setNewProp(String newProp){
this.newProp = newProp;}
public String getNewProp(){
return newProp;
}
When I trying to get property value in jsp, that defined in superclass like this
${b-inst.something}
I've got "is not readable or had invalid getter" error
Is it possible to get access to that superclass property without changing this property to protected, and writing it's getter\setter in subclass(because I'm losing inheritance benefits in that way)?
You should use correct case for the property: userName instead of username (just like your firstName and lastName properties)

What best way to pass #PathVariable to my all JSP pages without add it to ModelAndView? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I change my path from:
/user/home
/user/history
...
to
/{orgId}/home
/{orgId}/history
...
So for all /{orgId}/* pages I need orgId on my JSP page to construct right links. How to do it without to get #PathVariable in each method and pass it to ModelAndView.
Thanks!
Use ControllerAdvice and ModelAttribute
#ControllerAdvice
class Advice {
#ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("orgId", "value1");
}
}
As of Spring 4, #ControllerAdvice can be customized through annotations(), basePackageClasses(), basePackages() methods to select a subset of controllers.

Looking for method argument validation framework based on AOP and annotations [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm looking for argument validation framework which:
1) Allows specifying argument constraints via annontations (like OVal, JaValid)
2) Validation code automatically injected (during compilation or runtime) into methods (i.e. no explicit call to Validator object is required)
Example of what i'm looking for:
public class Person {
private String name;
....
//Method which arguments should be validated
public void setName(#NotBlank String name){
//<---validating code should be injected here
this.name = name;
}
}
//Example of call to the validated method
...
Person person = new Person();
person.setName("John");
...
Example of code i'm trying to avoid
...
Validator validator = new Validator(...);//glue code
Person person = new Person();
person.setName("John");
validator.validate(person);//glue code
...
Thanks for answers!
I think you meant "automatically injected during compilation or runtime", right?
I had the same problem. My solution was Spring Validation and self-written AOP layer (about three classes).
My validation code looks like this:
#Validational( validators =
{"com.mycompany.MyValidator"} )
public void myMethod( String paramToValidate )

Categories