populate enum fields with application.properties and java configuration - spring

with spring xml configuration, it is possible to define the following in app context xml to populate the status field in the TestEnum:
app context xml
<bean id="blue" class="com.example.demo.test.TestEnum" factory-method="valueOf">
<property name="status" value="${testnum.blue.status}"/>
<constructor-arg>
<value>BLUE</value>
</constructor-arg>
</bean>
<bean id="red" class="com.example.demo.test.TestEnum" factory-method="valueOf">
<property name="status" value="${testnum.red.status}"/>
<constructor-arg>
<value>RED</value>
</constructor-arg>
</bean>
enum class
public enum TestEnum {
BLUE,
RED;
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
mystatus.properties
testnum.blue.status=good
testnum.red.status=bad
Is this possible with spring boot using application.properties + java configuration without xml?

Yes, a simple environment configuration class (annotated with #ConfigurationProperties for example), will map and do the conversion of your enum automatically.
Example:
#Configuration
#Validated
#ConfigurationProperties(prefix = "my.property.path")
public class TestConfiguration{
public enum TestEnum {
BLUE, RED
}
#NotNull
private TestEnum type;
public TestConfiguration() {
}
public #NotNull TestEnum getType() {
return this.type;
}
public void setType(#NotNull TestEnum type) {
this.type = type;
}
}

Related

Setter Dependency Injection with Array of String

To test the setter dependency injection with an Array of string, I wrote the below-given code -
Pojo class is -
package com.abhishek.ioc.array;
public class Person {
private int id;
private String name;
private String[] hobbies;
public void showHobbies() {
System.out.println("Person name is - "+name+", id is - "+id);
for (int i = 0; i < hobbies.length; i++) {
System.out.println(hobbies[i]);
}
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
}
spring.xml file is -
<beans>
<bean id="person" class="com.abhishek.ioc.array.Person">
<property name="id" value="1"></property>
<property name="name" value="Abhisshek"></property>
<property name="hobbies">
<set>
<value>Playing cricket</value>
<value>Coding</value>
<value>Reading books</value>
</set>
</property>
</bean>
</beans>
Client code is -
package com.abhishek.ioc.array;
public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("resources/spring.xml");
System.out.println("Creating Person object");
Person person = (Person)applicationContext.getBean("person");
System.out.println("Person object created");
person.showHobbies();
}
}
In spring.xml file, I tried tags <array>, <list> and <set> to inject data in array and all three tags are giving the correct result. How?

Autowire based on a condition when xml bean configuration is used

I am working on Spring auto wiring using spring configuration file(xml configuration). I want to inject beans based on a condition.
Let me go into details.
There are two classes 'EmailSender' and 'SmsSender' which implement the interface IMessageSender. Beans are configured for both classes in the configurations file.
I have another class SenderUser which has a instance variable of type IMessageSender in it.
package org.pradeep.core;
public class SenderUser {
private String name;
private String Type;
private IMessageSender msg;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public IMessageSender getMsg() {
return msg;
}
public void setMsg(IMessageSender msg) {
this.msg = msg;
}
}
I want to inject IMessageSender into bean of SenderUser based on the value of SenderUser.getType(). That means first SernderUser.type should be set and then based on it's value (if value is 'email' then bean with the name 'email' should be wired else bean with the name 'sms' should be wired.) msg should be wired.
Please help me resolve the issue.
I believe the best approach is to implement a BeanFactory for SenderUser. Take a look at this post for an idea.
Interfaces can't be injected, they aren't beans/instances.
you may use SPEL
<bean id="user" class="SenderUser" autowire="byType">
<property name="type" value="email"/>
<property name="msg" value="#{type != null && type == 'email' ? email : sms}"/>
</bean>
<bean id="email" class="EmailSender"/>
<bean id="sms" class="SmsSender"/>

Implement interface in runtime Java EE and Spring

I would like to implement the interface at runtime based on variable.
Example :
Class A implements interface1 {
public getValue() {}
}
Class B implements interface1 {
public getValue() {}
}
So I would like to have variable sitting in configuration..., for example ClasstoImplement=A
So, if ClasstoImplement=A, then I need to call Class A.getValue()
If ClasstoImplement=B, then I need to call Class B.getValue() at runtime. And I should be able to change value of ClasstoImplement at runtime.
My application is Spring based and runs in Tomcat.
Can someone please help me to find out if there is any way??
There are many possible solutions. The one of them is to use org.springframework.aop.target.HotSwappableTargetSource.
take a look at implementation that could be considered:
public class CustomSwappable<T> implements Interface1 {
private HotSwappableTargetSource targetSource;
private String key;
private Map<String, T> swappableBeans;
#PostConstruct
private void init() {
targetSource = new HotSwappableTargetSource(swappableBeans.values().iterator().next()); // first is the default
}
// you need to track changes in config and call this method if any modifications were done
public void configChanged(String key, String value) {
if (!this.key.equals(key)) {
return;
}
if (!swappableBeans.containsKey(value)) {
return;
}
targetSource.swap(swappableBeans.get(value));
}
#Override
public String getValue() {
return ((Interface1)targetSource.getTarget()).execute();
}
#Required
public void setConfigurationKey(String key) {
this.key = key;
}
#Required
public void setSwappableBeans(Map<String, T> swappableBeans) {
this.swappableBeans = swappableBeans;
}
}
and bean declaration should as follows:
<bean id="Interface1Swappable" class="path.to.CustomSwappable">
<property name="configurationKey" value="swappableKey"/>
<property name="swappableBeans">
<util:map value-type="path.toInterface1">
<!-- first is default -->
<entry key="classA">
<bean class="path.to.class.A"/>
</entry>
<entry key="classB">
<bean class="path.to.class.B"/>
</entry>
</util:map>
</property>
</bean>

HttpMessageConverter not found in Spring restTemplate

I have got error when I retrieve JSON using String restTempate. Error says
Could not extract response: no suitable HttpMessageConverter found for response type [class au.org.jeenee.mdm.models.PhoneResponse] and content type [application/json]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:107)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:492)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:447)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:295)
at au.org.jeenee.mdm.services.EccClientServiceImpl.findPhoneByImei(EccClientServiceImpl.java:51)
at au.org.jeenee.mdm.controllers.DeviceController.showEditForm(DeviceController.java:308)
I found out the message means there is no JSON converter registered but I have Jackson message converter in my xml.
applicationContext.xml
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="httpClient"/>
</bean>
</constructor-arg>
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
And here is my code to use RestTemplate.
public class MyRestClientService {
#Override
public List<DeviceHolder> findDeviceHoldersByUserId(String userId) {
String uri = "/web/getpersonlist?userId=" + userId;
try {
DeviceHolderResponse response = restClient.getRestTemplate().postForObject(restClient.createUrl(uri), "", DeviceHolderResponse.class);
if (response!=null && response.isOk() && response.getResult()!=null){
DeviceHolder[] deviceHolders = response.getResult();
return Arrays.asList(deviceHolders);
}
} catch (Exception e) {
e.printStackTrace();
}
return Collections.emptyList();
}
#Override
public Phone findPhoneByImei(String imei) {
log.info("findPhoneByImei:" + imei);
Phone phone = null;
String uri = "/ecc/getphoneplan?imei=" + imei;
try {
PhoneResponse response = restClient.getRestTemplate().postForObject(restClient.createUrl(uri), "", PhoneResponse.class);
if (response.getResult()!=null)
phone = response.getResult();
} catch (Exception e) {
e.printStackTrace();
}
return phone;
}
}
PhoneResponse.java
public class Phone implement Serializable {
private boolean ok;
private String message;
private Phone result;
//getters and setters
}
Phone.java
public class Phone implements Serializable {
#JsonProperty(value="phoneid")
private long phoneId;
private Plan plan;
private String sim;
private String imei;
#JsonProperty(value="phonetype")
private String phoneType;
#JsonProperty(value="phonenumber")
private String phoneNumber;
private String label;
#JsonProperty(value="connecteddate")
private String connectedDate;
//getters and setters
}
Plan.java
public class Plan implements Serializable {
#JsonProperty(value="planid")
private long planId;
#JsonProperty(value="planname")
private String planName;
private double billingIncrement;
private double owiStdUnitCost;
private double owiFlagFall;
private double stdCap;
private double dataCap;
private double smsCap;
private double owiDataUnitCost;
private double owiSms;
//getters and setters
}
And the response packet is like following:
{
"ok": true,
"message": "",
"result":
{
"phoneid": 600003,
"phonenumber": 478439503,
"phonetype": "Samsung Galaxy S2",
"imei": "1111111111",
"sim": "1111111111",
"label": "Person name",
"connecteddate": "2012-09-19 00:00:00.0",
"plan":
{
"planid": 34,
"planname": "$59 Plan",
"billingIncrement": 30,
"owiStdUnitCost": 81.8181818181818,
"owiFlagFall": 0,
"stdCap": 636.3636,
"dataCap": 227.2665,
"smsCap": 1363.638,
"owiDataUnitCost": 0.022194,
"owiSms": 22.7272727272727
}
}
}
Strangely, there is no error when I call findDeviceHoldersByUserId method but error for findPhoneByImei method. And this has been working up to just before.
I tried again and again but I still have the error. Please help to fix the problem.
Thanks.
Couple of reasons, I found, that can cause this issue are -
Data type of the setters/getters are different from that of the actual properties (also mentioned in comment of sunghun)
If there are overloaded methods that may look like setter/getter of a field - same name as setField or getField where field is a property of the class.
I had a field private boolean success and 2 setter methods -
public void setSuccess(List<Object> dataList);
public void setSuccess(boolean success);
On debugging, I found that class com.fasterxml.jackson.databind.deser.BeanDeserializerFactory was throwing an exception. This exception was suppressed within Jackson's lib and the exception thrown by the RestTemplate was the same as the subject.
java.lang.IllegalArgumentException: Conflicting setter definitions for property "failure": com.test.dto.JsonResponse#setFailure(1 params) vs com.test.dto.JsonResponse#setFailure(1 params)
I changed the method to public void setSuccessData(List<Object> dataList); and it worked fine.
Hope this helps someone.

Autowiring String property through application context

I want to autowire a String bean as below
<bean id="name" class="java.lang.String" autowire="byName">
<constructor-arg value="Aravind"/>
</bean>
<bean id="employee" class="Employee" autowire="byName"/>
public Class Employee
{
private String name;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
When I try to access the name attribute in the employee is null
Employee emp=(Employee)getApplicationContext().getBean("employee");
System.out.println(emp.getName()==null);
It prints true.
Can someone help on this?
You still need to set the property on the Employee somehow.
Setting the name can be done in multiple ways.
XML configuration.
<bean id="employee" class="Employee" autowire="byName">
<property name="name">
<ref bean="name" />
</property>
</bean>
Using #Autowired
public Class Employee {
#Autowired
private String name;
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
}

Resources