Implementation function: automatically load the properties content into memory when the service is started, and call the @ bean annotation method loadProps();
If there is no content in the property when using (getData method call) after startup, call again.
The code is as follows:
import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import javax.annotation.Resource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; @Configuration @Component public class TranNameRead { private String Tag = "TranNameRead"; @Resource private BusinessLogger Log; private Properties props; @Resource private PostFactory postFactory; public Properties getInstance() throws IOException{ if(props==null){ this.loadProps(); } return props; } @Bean(name="TranNameRead") public Object loadProps() { Log.log(Tag, "Start loading properties content......."); ......//Ten thousand words are omitted here return trannameList; } public String getData(String key) throws IOException{ return getInstance().getProperty(key); } }
The problem is: after calling @ bean annotation manually, the method cannot be called. Who can tell the reason?
Later, a different way was called to solve the problem.
public class TranNameRead { private String Tag = "TranNameRead"; @Resource private BusinessLogger Log; private Properties props; @Resource private PostFactory postFactory; public Properties getInstance() throws IOException{ if(props==null){ loadPropsAgain(); } return props; } @Bean(name="TranNameRead") public Object loadProps() { return loadProperties(); } public Object loadPropsAgain() { return loadProperties(); } private Object loadProperties() { Log.log(Tag, "Start loading properties content......."); List<Map> trannameList =null;; } }
Just... Thank you for your comments.