How to get Spring bean by name [closed] - java

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);

Related

how to use a post method with a spring data rest process? [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
I'm using a repostiroy rest resource service and currently I need to implement a method to import the data from a excel file into my database. I don't see how I can do with spring repository rest resource? since it's an interface. do I use restController or does it have a way to do both?
The standard flow would be:
Create a repository file
#Repository
public interface RepositoryClass extends JpaRepository<T, ID>{}
where T is the class and ID is the data type of the ID (ex: Long, Integer etc.)
The JpaRepository already have save() and saveAll() methods(1)
In service create a method that read the data and saves it in the database
#Service
public class ServiceClass{
// ...some code
public void importData(// ... parameters){
// open the excel file and import the data in an ArrayList for exemple
repositoryClass.saveAll(arrayWithData);
}
}
The last step is to create a endpoint in the controller which calls the method that is written in service.
That interface have a method called save(). You can save the given object in database like: repository.save(myOjbect)

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
}

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.

Why is it impossible to inject generic classes? [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 9 years ago.
Improve this question
I have a generic abstract template class. I thought if I create type-specific Producers, I could inject some DAO service directly in the generic class. But I can't.
Why? And how could I work around this?
abstract class MyView<T> {
#Inject
MyDao<T> dao;
//some more template methods that make use of the dao
void someMethod() {
dao.use();
}
}
class CustomerView extends MyView<Customer> {
//javax.enterprise.inject.AmbiguousResolutionException: Ambigious resolution
}
class DaoManager {
#Produces
MyDao<Customer> getDaoCustomer() {
return DaoFactory.make(Customer.class);
}
#Produces
MyDao<Product> getDaoProduct() {
return DaoFactory.make(Product.class);
}
}
When I inject eg a #Inject MyDao<Customer> dao; it works perfectly. But not with generics...
When you request
#Inject MyDao<Customer> dao;
the container knows that you want a bean specifically of type MyDao<Customer>. If such a bean exists and its type information is known, then the container can satisfy the injection. For example, the type information is preserved in your #Produces annotated method
#Produces
MyDao<Product> getDaoProduct() {
The container uses reflection to retrieve that parameterized type and can match it to the requested #Inject field.
With
abstract class MyView<T> {
#Inject
MyDao<T> dao;
however, all the container knows is that you want a MyDao. T is a type variable, not a concrete parameterization. The container cannot assume a specific type for it. In your case, both of the #Produces beans would match and there would be ambiguity.
In your example, we know from the context that it really wants a MyDao<Customer>. That doesn't seem to be something your container is capable of doing, ie. trying to resolve the type parameter to a concrete type argument for a parameterized subclass.

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