Several ways to read the bean configuration file in Spring

xiaoxiao2021-03-06  35

BeanFactory allows inputStream as a parameter of the constructor, or ORG.SPRINGFRAMEWORK.CORE.IO.Resource interface. Below this example is the parameters with ClassPathResource as a parameter:

Resource resource = new classpathResource ("bean.xml"); beanfactory factory = new xmlbeanfactory; actionBean action = (ActionBean) Factory.getBean ("ActionBean");

If the same bean has multiple beans definitions in the configuration file, all objects are obtained with the following method:

Resource resource = new classpathResource ("bean.xml"); listablebeanfactory factory = new xmlbeanfactory; map hellobeans = factory.getBeansoftype (ActionBean.class, false, false);

There are often many beans in an app to be placed in the configuration file. If all beans are placed in a file, it is quite troubles to maintain it. At this time, we can use each module to put each module to becomes different. Profile, Spring provides a corresponding method:

BeanDefinitionRegistry reg = new DefaultListableBeanFactory (); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader (reg); // Loading bean definition file reader.loadBeanDefinitions (new ClassPathResource ( "bean1.xml")); reader.loadBeanDefinitions (new ClassPathResource ( "bean2.xml ")); .... // Get beanbeanfactory bf = (beanfactory) reg; object o = bf.getBean (" ActionBean ");

转载请注明原文地址:https://www.9cbs.com/read-67273.html

New Post(0)