Java 将map设置为springbean
我需要将地图设置为SpringBean。我有一个在init方法中使用@PostConstruct关键字初始化的映射Java 将map设置为springbean,java,spring,spring-mvc,dictionary,Java,Spring,Spring Mvc,Dictionary,我需要将地图设置为SpringBean。我有一个在init方法中使用@PostConstruct关键字初始化的映射 public class CustomerService { @Autowired TestService testService; static Map<String, String> someMap; @PostConstruct public void initIt() throws Exception {
public class CustomerService
{
@Autowired
TestService testService;
static Map<String, String> someMap;
@PostConstruct
public void initIt() throws Exception {
someMap = new HashMap<String, String>();
someMap = // some code by calling testService
}
@PreDestroy
public void cleanUp() throws Exception {}
}
我从applicationContext.xml将其称为bean
<bean id="customerService" class="test.com.customer.CustomerService"/>
初始化映射工作正常,我需要将这个映射的值分配到bean中,以便在应用程序的其他地方访问bean的值
我找到了将map设置为bean的示例,但所有这些都是在XML配置中完成的。
我怎样才能完成这项任务。任何知识都将受到高度赞赏
您应该为该映射创建一个getter。您可以@autowire将您的bean连接到其他地方,并调用yourbean.getMap
你会得到它的
在其他课程中,您可以:
@Autowired
CustomerService customerService;
当然,在地图的客户服务中使用一种更好的getter方法。
然后在控制器或其他服务中,您应该使用上面的注释自动连接bean。然后在您的方法中使用它,如下所示:
Map m = customerService.getMap();
当你创建一个bean来保存地图时,你可以用这种方法在你的应用程序中创建一个Flyweight设计图案
谢谢@gen.Strash。我如何自动连线我的豆子?一个示例代码将是伟大的。再次谢谢你明白了吗?谢谢@gen.Strash。我正在看。我将更新状态。再次感谢你们,感谢斯特拉什将军提出的解决方案。还有其他的方法吗?很高兴知道。