Constructor injection!! please - spring

I'm new to full stack and i'm stuck in an assignment
So the question is:
Make a class (Cust)with 4 private attributes: id,name,email,phoneno
Make a class (Member)with 4 private attributes: id,type,visits,cust(data type is Cust)
Cust has to set to the member via constructor injection in the XML file
Create a class called driver with the main method and write the logic to get input from the user such as customer details and membership details and display them.
Design constraints:
Cust class and Member class should be present in the com.spring.app package
Write appropriate setters and getters and constructors
//There are many more info provide to write the code which i have omitted //
Solution which i assembled:
I have done the Cust class and Member class and driver class
The thing I'm stuck on is I know that I have to add beans in the XML file with respect to the contructor and how to set the two classes but what I don't know is how to take user input and put it in the XML file
<constructor-arg>
<value>123</value>
</constructor-arg>
Here the value 123 is put by me in the XML file,what can I do to update the value by user input in the XML file?

Related

Spring return dynamic instance based of String value

Java Spring question:
I have a interface MyInterface with one method
void exec (String str);
I have many implementation of MyInterface, say Oneimpl, anotherimpl yetanotherimpl...and so on and can keep adding new implementations.
how do I obtain an instance of a specific implementation using just the name of the implementing class passed as a STRING value , say "someRandomImpl"
The code should be dynamic and can provide a instance of new implementations without code change.
implements ApplicationContextAware
it will autowired ApplicationContext object
use the object like
context.getBean(beanName)
then you get the bean

Defining string constants in configuration metadata

I often have a need to define string constants in the XML configuration metadata file. These constants are things like Company Name, Fiscal Year, etc; that I need to lookup from various classes of my application. I end up coding them as bean definitions like <bean id="CompanyName" class="java.lang.String" c:_0="Google" />. Is there a better way to define this information?
You can have your properties inside a property file for example.
config.properties
property1=value
property2=value
Then in your class you can use the propety file
#Configuration
#PropertySource("classpath:config.properties")
public class MyClass {
#Value("${property1}")
private String myProperty1;
#Value("${property2}")
private String myProperty2;
}
You can see these two tutorials
http://www.mkyong.com/spring/spring-propertysources-example/
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

XxxxxPersistence null pointer #BeanReference annotation

I try to use the method public XxxxxLocalService getXxxxxLocalService() from xxxxxlocalServiceBaseImpl.java class which was generated by the service builder but I get a null value in the variable xxxxxLocalService!
public xxxxxLocalService getClientLocalService() {
return xxxxxLocalService;
}
this variable is instantiated by the #BeanReference annotation as we can see at the head of the java file:
#BeanReference(type = xxxxxLocalService.class)
protected XxxxxLocalService xxxxxLocalService;
#BeanReference(type = XxxxxPersistence.class)
protected XxxxxPersistence xxxxxPersistence;
the ServieBaseImpl package contains services with #BeanReference annotations that are processed by BeanReferenceAnnotationBeanPostProcessor that uses one of BeanLocators.
I noticed that there is a chicken & egg problem. You cannot load these beans without BeanLocator that is already populated with application context containing #BeanReferenced beans.
But you cannot create application context for BeanLocator to be populated with, because if you do so and BeanReferenceAnnotationBeanPostProcessor is part of the context, it will use BeanLocator that is not initialized.
BeanReferenceAnnotationBeanPostProcessor is declared in base-spring.xml that needs to be loaded with other bean definitions (from portlet-spring.xml etc.), because they depend on each other.
I don't know why this class can't set the variable value? in fact I'm not using the base-spring.xml in my configuration .. so I'm asking which lines I have to add in my ressource file for being able to use the bean reference annotation!!
Thanks

Spring configuration calling more than one method

I have code which looks like the following:
MyContext context = new MyContext();
context.start();
MyEntity entity = context.getEntity();
I want to inject the MyEntity instance into various classes.
But I don't know how to setup my Spring configuration, where I first create an object, then call a method on it and then finally call another method which returns the entity I want to inject.
EDIT 2 - removed the Strings altogether
The most common type of dependencies injected using Spring don't depend on the user input for their construction. This includes data access objects, services etc.,
You are talking about injecting domain objects whose construction depends on the user input either directly or indirectly.
Spring provides #Configurable annotation to inject such domain objects that are created using new operator. You can search for "#Configurable Domain Driven Design" on the internet to get examples of how this can be implemented. I myself used it in one my applications and wrote a simple post here that might help you get started.
Edit:
To create a bean of type MyEntity as per the specification in your updated question, you would need to
define a bean of type MyContext
Create a MyEntityFactory class that would depend on the MyContext bean.
The factory method would take the MyContext bean as argument, calls context.start() on it and returns an instance of MyEntity.
You would define the MyEntity bean using this factory class.
The MyEntityFactory class would be as follows:
public class MyEntityFactory
{
public static MyEntity getMyEntity(MyContext context)
{
context.start();
return context.getEntity();
}
}
The spring bean configuration will be as follows:
<bean id="myContext" class="FQCN.Of.MyContext" />
<bean id="myEntity" class="FQCN.Of.MyEntityFactory" factory-method="getMyEntity">
<constructor-arg ref="myContext" />
</bean>
Since MyEntity is a singleton bean, the factory method will be called only once, btw.
More on creating beans using factory methods here.

Spring property loading depending on class that invokes

I have a class A which contains a property
class A{
String valA;
}
I have two classes B and C which have a reference to class A
class B{
#Autowired
private A aaa;
}
class C{
#Autowired
private A aaa;
}
The valA in class A would be property driven and should depend on which class in actually invoking it.
If class B is invoking it , it should be some thing like b.property defined in a property file
and for class C the value would be c.property
Is this possible to do this without using an XML configuration and only annotation , SPEL etc
Thanks in Advance
You need two different instances of A (one for B and one for C), because you can not change the value of an property in A depended on the way A is invoked*.
Have a look at the concept of qualifies to see how they can be used to distinguishes between two instances of the same class.
footenote * of course you can change the parameter in A depended on how A is invoked, but this requires a lot of technical code and some hacks. And should be not the code you want to have in your spring app.

Resources