Can we declare a member variable as static for which we are doing constructor injection? [duplicate] - spring

This question already has answers here:
Why can't we autowire static fields in spring?
(4 answers)
Can you use #Autowired with static fields?
(13 answers)
Closed 5 months ago.
To use a member variable in static function I need to declare that member variable as static. Is there anything wrong in declaring static fields and injecting them via constructor injection in multithreaded environment ?
#Named
public class QueryHelper {
// Anything wrong to declare it static and injecting it via constructor injection ?
private static QueryUtil queryUtil;
#Inject
QueryHelper(final QueryUtil queryUtil) {
this.queryUtil = queryUtil;
}
public static List<Contracts> getContracts() {
// .. some code
queryUtil.someFunction();
// ..some code
return contractList();
}
}

Related

Error An object reference is required for the non-static field, method, or property 'gameManager.CompleteLevel()' [duplicate]

This question already has answers here:
Unity 5 An object reference is required for the non-static field, method, or property error
(2 answers)
Closed 3 years ago.
I'm taking a tutorial by Brackeys for a Unity Game and for some reason my code is returning this Error- An object reference is required for the non-static field, method, or property 'gameManager.CompleteLevel()'
here is some of the code/context
public class gameManager : MonoBehaviour
{
bool gameHasEnded = false;
public float restartdelay = 11f;
public void CompleteLevel()//here is the Error
{
Debug.Log("LEVEL COMPLETED");//printing this to make sure it works
}
public void gameEnd()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", 2f);//here making a delay
}
}
}
Error- An object reference is required for the non-static field, method, or property 'gameManager.CompleteLevel()' happens because you are trying to access something directly, without it being static or by not using a reference. You need to decide if you want this method, or class, to be static or create a reference to it.
You only need to do this to make it static
public static void CompleteLevel()//here is the Error
{
Debug.Log("LEVEL COMPLETED");//printing this to make sure it works
}

Reading Environment variable in SpringBootApplication [duplicate]

This question already has answers here:
Spring: How to inject a value to static field?
(5 answers)
Closed 3 years ago.
I have an environment variable RESOURCES_FOLDER. Which i want to read in a class inside my Springboot application
#Value("${RESOURCES_FOLDER}")
private static String resourcesFolder;
When I try printing the value it gives null instead of printing the actual path in my environment variable. Can someone please help me with this ??
Spring does not allow injecting values into static fields. You have a few options to circumvent this restriction:
Create a non-static setter. This is not a particularly good approach, as resourceFolder will be shared by all instances of a class. Nevertheless, you should be able to achieve it by:
public class SomeClass {
...
public static String resourcesFolder;
#Value("${RESOURCES_FOLDER}")
public void setResourcesFolder(String resourcesFolder) {
this.resourcesFolder = resourcesFolder;
}
...
}
Declare the field as non static. For this, ask yourself: do you really, really need the field to be static? Most of the time non-static field is good enough.
Create a separate #ConfigurationProperties class, declare fields as private non-static, create getters/setters for it and inject the class wherever you need the variable. This is a very composable and testable approach, which I would recommend, especially if you have quite a few related properties.
Alternatively, please refer to the other similar questions: 1, 2.
Add your environment variable name in the application.properties file by:
RESOURCE_FOLDER_PATH =${RESOURCES_FOLDER}
Here RESOURCES_FOLDER is the name of your environment variable.
Then access the environment variable in the java class by using #value annotation.
public class AccessEnvironmentVariable{
#Value("${RESOURCE_FOLDER_PATH}")
private String RESOURCE_FOLDER;
private void displayEnvironmentVariable(){
System.out.println("Your environment variable Resource Folder: "+RESOURCE_FOLDER);
}
}

When calling abstract method from concrete class throwing NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am testing a method in a class. Which is calling a method of abstract class.
Eg:
class abstract Abstract {
public ReturnObject abstractMethod(SomeObject value) {
// do something
return returnObject;
}
}
class Concreate extends Abstract {
public ReturnObject concreteMethod(SomeObject value) {
//do something
returnObject = abstractMethod(value);
return returnObject;
}
}
My UT is
class ConcreateTest {
#InjectMocks
private Concreate conctrete;
#Mock
private Concreate conctrete2;
#Test
public void test_method() {
when(conctrete2.abstractMethod(value)).thenReturn(returnObject);
conctrete.concreteMethod(value);
}
}
This way it is returning me NullPointerException.
From what you are showing, I see very little of your code to tell for sure what's going on, but one thing I see is that you are mocking one Concreate and then injecting that mock into another Concreate. I don't see in the code you are showing anywhere that tells me that a Concreate uses another injected Concreate. This essentially is just pseudo-code. So essentially I am assuming that your main Concreate is being injected in an application context and that your other Concreate is being injected in the first one.
You need #Named to solve this ambiguity or more generically speaking you must give your beans an individual name, even if they are mocked.

How to use 'make_ptr<T>()' while T is a class with private construction? [duplicate]

This question already has answers here:
How do I call ::std::make_shared on a class with only protected or private constructors?
(19 answers)
Closed 7 years ago.
I was working with singleton pattern and shared_ptr.I was trying to make the code like this:
class A{
private:
static std::shared_ptr<A> instance;
A();
public:
static std::shared_ptr<A> creatInstance();
};
std::shared_ptr<A> A::creatInstance(){
if(!instance){
instance=std::make_shared<A>();
}
return instance;
}
But I got a compiler error.
Any thoughts?
I tried to make make_shared be a friend function of class A,but it didn't work.
friend shared_ptr<A> make_shared<A>();
It seems that std::make_shared< A > requires public constructor of A.
Here is a few solutions of this problem - How do I call ::std::make_shared on a class with only protected or private constructors?

Spring - Injecting a list of dates from property file to a bean property [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?
I have a class like this:
public class BankHolidayCalendar {
List<DateTime> bankHolidays;
public BankHolidayCalendar(final List<DateTime> p_bankHolidays) {
bankHolidays = p_bankHolidays;
}
}
and a property file
# holidays.properties
holidayDates=01-01-2012, 13-02-2012, 22-04-2012
How can I read these dates from this property file and inject into the bean constructor?
I am using joda time here.
I would use the #Value annotation and create the DateTime object in the constructor like this:
public class BankHolidayCalendar {
List<DateTime> bankHolidays = new ArrayList<DateTime>();
public BankHolidayCalendar(#Value("holidayDates") String[] p_bankHolidays) {
for (String date : p_bankHolidays) {
bankHolidays.add(...);
}
}
}

Resources