In my application i want to initialize the bean before hitting application url, and store the common values for drop-down. here it is declaration for bean
<beans:bean id="listService" class="com.system.beans.DropDownList"
init-method="populateMasterList" scope="application"/>
Bean:
public class DropDownList implements InitializingBean
{
private static final Logger logger = LoggerFactory.getLogger(DropDownList.class);
public static Map<String, Map<Integer, String>> listMap = new HashMap<String, Map<Integer, String>>();
#Autowired
private static SystemService systemService;
#Autowired(required = true)
#Qualifier(value = "systemService")
public void setSystemService(SystemService systemService)
{
this.systemService = systemService;
}
#PostConstruct
public static Map<String, Map<Integer, String>> populateMasterList()
{
logger.debug("Calling Institute Info Masters");
List<InstituteInfoMaster> masterList = systemService.listInstituteInfoMasters();
Map<Integer, String> masterMap = new HashMap<Integer, String>();
masterMap.put(0, "---Select---");
masterList.forEach((master) ->
{
masterMap.put(master.getListId(), master.getValue());
});
logger.debug("Created Map for List Masters");
listMap.put("infoList", masterMap);
return listMap;
}
public Map<String, Map<Integer, String>> getListMap()
{
return listMap;
}
public static void setListMap()
{
listMap = populateMasterList();
}
#Override
public void afterPropertiesSet() throws Exception
{
populateMasterList();
}
}
I observed that it does not initializes on Application startup. when i try to update the master by calling DropDownList.setListMap(); it gives NullPointerException. but if i calls the jsp page where i am calling the Map as ${listService.listMap['infoList']} it displays the Drop-down on jsp after it if i tries to save master it executes successfully.
it means when i calls the jsp page where i am showing drop-down that time only it initializing the bean not on Application startup.
The actual problem is that you are not accessing the Spring bean, but the class, staticly. When you use the bean, i.e. the listService instance, Spring will initalize it for you on first access.
You are calling a static method, but when this happens, the dependant beans are not populated. Autowiring works for instances (i.e. in non-static context), so systemService is null in your application.
Update: I have just realized this line:
#Autowired
private static SystemService systemService;
This is fundamentally wrong. You cannot autowire static fields, it makes absolutely no sense in Spring (or in any similar framework). Spring beans are instances, and the framework set the autowired fields to references to other spring beans.
Related
Let's imagine I have the next classes in the project based on Spring framework:
interface I {
String getName()
}
#Component
class I1 implements I {
#Override
String getName() {return "I1"}
}
#Component
class I2 implements I {
#Override
String getName() {return "I1"}
}
And I want to gather them all in the map using the #Autowired method:
#Component
public class A {
private Map<I> map = new HashMap<>()
#Autowired
public registerI(I i) {
map.put(i.getName(), i)
}
}
Should I make this method registerI synchronized? I mean, can Spring call this method in several threads simultaneously? Or this method will be called sequentially?
Thanks
You don't have to use synchronized because Spring bean initialization is single-threaded and thread-safe. You can think of gotchas like thread-scoped or lazy beans but for regular singleton beans initialization happens in one thread.
You might want to use synchronized to make sure that after registerI() method is called your object is safely published, although auto-wired constructor with final field is more readable.
#Component
public class A {
private final Map<String, I> map;
public A(List<I> list) {
map = list.stream().collect(Collectors.toMap(I::getName, i -> i));
}
}
You will get an exception during app startup because Spring cannot determine the correct implementation of interface "I" what you want to inject. You should use #Qualifier.
If you want to accomplish that scenario, this should be enough.
#Component
public static class A {
private Map<String,I> map = new HashMap<>();
public A(List<I> list) {
//map = list.stream().collect(Collectors.toMap(I::getName, x -> x));
for (I i : list) {
map.put(i.getName(), i);
}
}
}
You will end with only one value in the map.
The commented line works if there are not duplicate map keys.
You can autowire context and get all the interested beans from it in a #PostConstruct method and create a hashmap with it.
Or
If you want that Map to be shared amongst multiple classes, make it a #Bean
#Configuration
class SomeConfig{
#Autowire Context context;
#Bean(name = "mapBean")
public Map<String, MyCustomClassName1> mapBean() {
Map<String, MyCustomClassName1> map = new HashMap<>();
//populate the map here - from Context
return map;
}
}
Spring fills List by your beans. After you can create map in postConstruct
#Component
public class A {
#Autowired
private List<I> list;
#Autowired
private Map<String, I> map;
#PostConstruct
private void init(){
map = list.stream()
.collect(Collectors.toMap(I::getName, element->element);
}
}
SpringBoot app fails while trying to initialize this class:
#Component
public class Weather {
private Map<Integer,Double> maxRainyDays;
private Map<Integer, WeatherDays> totalDays;
public Weather(Map<Integer, WeatherDays> totalDays, Map<Integer,Double> maxRainyDays){
this.setMaxRainyDays(maxRainyDays);
this.setTotalDays(totalDays);
}
The error:
Parameter 0 of constructor in SolarSystem.Models.Weather required a
bean of type 'SolarSystem.Utilities.WeatherDays' that could not be
found.
The mentioned bean is already defined (in the same basepackage):
public enum WeatherDays {
RAINY,
MILD,
DRY,
MAX_RAIN}
Workaround:
When I changed to Weather() constructor I solved the issue.Of course I had to use setters to set the properties of the object.
But I need to understand the reasons why happened
Because the collections (Map here) you are injecting via constructor parameter aren't registered as Spring beans. This is why you would annotate class with #service, #repository, etc and have them autowired into other classes. To fix, you can set up a config class like such:
#Configuration
public class BeanConfig {
#Bean
public Map<Integer, WeatherDays> totalDays() {
Map<Integer, WeatherDays> map = new HashMap<>();
map.put(1, WeatherDays.DRY);
return map;
}
#Bean
public Map<Integer, Double> maxRainyDays() {
Map<Integer, Double> map = new HashMap<>();
map.put(1, 0.2);
return map;
}
}
I wanted to set a Hashmap object (of logged in users) at application level. But unable to do the same as with every request my old user list saved in Hashmap object is getting refreshed and returning only current logged in user.
I have tried many example but no luck.
Below is my code
<bean id="applicationContextProvder" class="com.starter.basic.ApplicationContextProvider"/>
<bean id="testBean" class="com.starter.basic.TestBean"/>
TestBean tb = appContext.getApplicationContext().getBean("testBean", TestBean.class)
public class ApplicationContextProvider implements ApplicationContextAware
{
private static ApplicationContext context;
public ApplicationContext getApplicationContext() {
return context;
}
#Override
public void setApplicationContext(ApplicationContext ac)
throws BeansException {
context = ac;
}
}
public class TestBean implements ServletContextAware {
private HashMap hashMap = new HashMap<String ,String >();
}
public HashMap getUsers(){
return hashMap;
}
Framework used Spring MVC 3 + Hibernate 4.0
If anyone not able to understand my points above. What i am trying to do is if some user is already logged in and try to login again from another device user should blocked. I should get that user from Hashmap object on his second login. I could do this from back end flag. But my requirement is to use application context.
You should use application scope for your bean:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-application
You should add a user to a hashmap on every login and remove him on every logout.
You need Singleton Pattern for your HashMap and for the concurrency problem you need use ConcurrentHashMap to make sure thread safe,
You can use TestBean.getHashMap() to get, put or remove when you need:
class TestBean implements ServletContextAware {
private static final HashMap hashMap = new ConcurrentHashMap<String, String>();
public static HashMap getHashMap() {
return hashMap;
}
}
static variable will exist all the time when your program is in runtime.
I want to re-create (new Object) a specific bean at Runtime (no restarting the server) upon some DB changes. This is how it looks -
#Component
public class TestClass {
#Autowired
private MyShop myShop; //to be refreshed at runtime bean
#PostConstruct //DB listeners
public void initializeListener() throws Exception {
//...
// code to get listeners config
//...
myShop.setListenersConfig(listenersConfig);
myShop.initialize();
}
public void restartListeners() {
myShop.shutdownListeners();
initializeListener();
}
}
This code does not run as myShop object is created by Spring as Singleton & its context does not get refreshed unless the server is restarted. How to refresh (create a new object) myShop ?
One bad way I can think of is to create new myShop object inside restartListeners() but that does not seem right to me.
In DefaultListableBeanFactory you have public method destroySingleton("beanName")so you can play with it, but you have to be aware that if your autowired your bean it will keep the same instance of the object that has been autowired in the first place, you can try something like this:
#RestController
public class MyRestController {
#Autowired
SampleBean sampleBean;
#Autowired
ApplicationContext context;
#Autowired
DefaultListableBeanFactory beanFactory;
#RequestMapping(value = "/ ")
#ResponseBody
public String showBean() throws Exception {
SampleBean contextBean = (SampleBean) context.getBean("sampleBean");
beanFactory.destroySingleton("sampleBean");
return "Compare beans " + sampleBean + "=="
+ contextBean;
//while sampleBean stays the same contextBean gets recreated in the context
}
}
It is not pretty but shows how you can approach it. If you were dealing with a controller rather than a component class, you could have an injection in method argument and it would also work, because Bean would not be recreated until needed inside the method, at least that's what it looks like. Interesting question would be who else has reference to the old Bean besides the object it has been autowired into in the first place,because it has been removed from the context, I wonder if it still exists or is garbage colected if released it in the controller above, if some other objects in the context had reference to it, above would cause problems.
We have the same use-case. As already mentioned one of the main issues with re-creating a bean during runtime is how to updating the references that have already been injected. This presents the main challenge.
To work around this issue I’ve used Java’s AtomicReference<> class. Instead of injecting the bean directly, I’ve wrapped it as an AtomicReference and then inject that. Because the object wrapped by the AtomicReference can be reset in a thread safe manner, I am able to use this to change the underlying object when a database change is detected. Below is an example config / usage of this pattern:
#Configuration
public class KafkaConfiguration {
private static final String KAFKA_SERVER_LIST = "kafka.server.list";
private static AtomicReference<String> serverList;
#Resource
MyService myService;
#PostConstruct
public void init() {
serverList = new AtomicReference<>(myService.getPropertyValue(KAFKA_SERVER_LIST));
}
// Just a helper method to check if the value for the server list has changed
// Not a big fan of the static usage but needed a way to compare the old / new values
public static boolean isRefreshNeeded() {
MyService service = Registry.getApplicationContext().getBean("myService", MyService.class);
String newServerList = service.getPropertyValue(KAFKA_SERVER_LIST);
// Arguably serverList does not need to be Atomic for this usage as this is executed
// on a single thread
if (!StringUtils.equals(serverList.get(), newServerList)) {
serverList.set(newServerList);
return true;
}
return false;
}
public ProducerFactory<String, String> kafkaProducerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.CLIENT_ID_CONFIG, "...");
// Here we are pulling the value for the serverList that has been set
// see the init() and isRefreshNeeded() methods above
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, serverList.get());
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<>(configProps);
}
#Bean
#Lazy
public AtomicReference<KafkaTemplate<String, String>> kafkaTemplate() {
KafkaTemplate<String, String> template = new KafkaTemplate<>(kafkaProducerFactory());
AtomicReference<KafkaTemplate<String, String>> ref = new AtomicReference<>(template);
return ref;
}
}
I then inject the bean where needed, e.g.
public MyClass1 {
#Resource
AtomicReference<KafkaTemplate<String, String>> kafkaTemplate;
...
}
public MyClass2 {
#Resource
AtomicReference<KafkaTemplate<String, String>> kafkaTemplate;
...
}
In a separate class I run a scheduler thread that is started when the application context is started. The class looks something like this:
class Manager implements Runnable {
private ScheduledExecutorService scheduler;
public void start() {
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(this, 0, 120, TimeUnit.SECONDS);
}
public void stop() {
scheduler.shutdownNow();
}
#Override
public void run() {
try {
if (KafkaConfiguration.isRefreshNeeded()) {
AtomicReference<KafkaTemplate<String, String>> kafkaTemplate =
(AtomicReference<KafkaTemplate<String, String>>) Registry.getApplicationContext().getBean("kafkaTemplate");
// Get new instance here. This will have the new value for the server list
// that was "refreshed"
KafkaConfiguration config = new KafkaConfiguration();
// The set here replaces the wrapped objet in a thread safe manner with the new bean
// and thus all injected instances now use the newly created object
kafkaTemplate.set(config.kafkaTemplate().get());
}
} catch (Exception e){
} finally {
}
}
}
I am still on the fence if this is something I would advocate doing as it does have a slight smell to it. But in limited and careful usage it does provide an alternate approach to the stated use-case. Please be aware that from a Kafka standpoint this code example will leave the old producer open. In reality one would need to properly do a flush() call on the old producer to close it. But that's not what the example is meant to demonstrate.
I have a class called Drives which dynamically instantiates FsAccess beans.
I have a service class called ServersAccessService which finds FsAccess beans and stores them in a map using #autowired. Is there a way to have the service class initiate the #autowired after Drives is finished instantiating the FsAccess beans?
Service Class:
public class ServersAccessService implements DisposableBean {
protected static final Log log = LogFactory.getLog(ServersAccessService.class);
protected static Map<String, FsAccess> servers = new HashMap<String, FsAccess>();
I
protected Map<String, FsAccess> restrictedServers = new HashMap<String, FsAccess>();
protected boolean isInitialized = false;
protected static Map<String, DrivesCategory> drivesCategories = new HashMap<String, DrivesCategory>();
#Autowired
public void setServers(List<FsAccess> servers) {
for(FsAccess server: servers) {
this.servers.put(server.getDriveName(), server);
}
}
Drives class:
MyBeanFactory mbf = new MyBeanFactory();
//loop through each drive in driveList
for(String name:driveList)
{
String fullUri = "smb://naz-fs3/home/"+name;
String icon = "/esup-portlet-stockage/img/drives/root.png";
VfsAccessImpl drive = mbf.createInstance();
//Set attribute information
drive.setDriveName(name);
drive.setIcon(icon);
drive.setUri(fullUri);
drive.setContextToken(name);
}
If the Drives bean is instantiating the FsAccess beans in it's initialization phase, you may declare the dependency of the ServersAccessService bean via depends-on to the Drives bean. This forces the initialization of the Drives bean before the initialization of the ServersAccessService bean.
I believe what you want is to make your drives class a #Configuration bean, and make the method that returns a List annotated with #Bean. That way the spring container knows about the FsAccess list and it can be available for injection via #Autowired.
EDIT:
Reference: http://blog.springsource.com/2006/11/28/a-java-configuration-option-for-spring/