Search in sources :

Example 1 with XmlBeanFactory

use of org.springframework.beans.factory.xml.XmlBeanFactory in project tutorials by eugenp.

the class BeanFactoryWithClassPathResourceIntegrationTest method createBeanFactoryAndCheckEmployeeBean.

@Test
public void createBeanFactoryAndCheckEmployeeBean() {
    Resource res = new ClassPathResource("beanfactory-example.xml");
    BeanFactory factory = new XmlBeanFactory(res);
    Employee emp = (Employee) factory.getBean("employee");
    assertTrue(factory.isSingleton("employee"));
    assertTrue(factory.getBean("employee") instanceof Employee);
    assertTrue(factory.isTypeMatch("employee", Employee.class));
    assertTrue(factory.getAliases("employee").length > 0);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 2 with XmlBeanFactory

use of org.springframework.beans.factory.xml.XmlBeanFactory in project lzl_workspace by hpulzl.

the class test method xmlBeanFactory.

public static void xmlBeanFactory() {
    // 加载配置文件
    ClassPathResource resource = new ClassPathResource("ApplicationContext.xml");
    // 解析文件
    BeanFactory beanFactory = new XmlBeanFactory(resource);
    // 创建对应bean,依赖注入过程
    Person p = (Person) beanFactory.getBean("person");
    System.out.println("p = " + p);
}
Also used : XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) Person(entity.Person) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 3 with XmlBeanFactory

use of org.springframework.beans.factory.xml.XmlBeanFactory in project twitter4j by yusuke.

the class SpringCompatibilityTest method setUp.

@BeforeEach
protected void setUp() throws Exception {
    writeFile("./twitter4j.properties", "user=one" + "\n" + "password=pasword-one");
    Resource res = new ClassPathResource("spring-beans.xml");
    beanFactory = new XmlBeanFactory(res);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with XmlBeanFactory

use of org.springframework.beans.factory.xml.XmlBeanFactory in project ignite by apache.

the class GarHelloWorldTask method split.

/**
 * {@inheritDoc}
 */
@Override
public Collection<? extends ComputeJob> split(int gridSize, String arg) throws IgniteException {
    // Create Spring context.
    AbstractBeanFactory fac = new XmlBeanFactory(new ClassPathResource("org/apache/ignite/spi/deployment/uri/tasks/gar-spring-bean.xml", getClass().getClassLoader()));
    fac.setBeanClassLoader(getClass().getClassLoader());
    // Load imported bean from GAR/lib folder.
    GarHelloWorldBean bean = (GarHelloWorldBean) fac.getBean("example.bean");
    String msg = bean.getMessage(arg);
    assert msg != null;
    // Split the passed in phrase into multiple words separated by spaces.
    List<String> words = Arrays.asList(msg.split(" "));
    Collection<ComputeJob> jobs = new ArrayList<>(words.size());
    // Use imperative OOP APIs.
    for (String word : words) {
        // Every job gets its own word as an argument.
        jobs.add(new ComputeJobAdapter(word) {

            /*
                 * Simply prints the job's argument.
                 */
            @Nullable
            @Override
            public Serializable execute() {
                System.out.println(">>>");
                System.out.println(">>> Printing '" + argument(0) + "' on this node from grid job.");
                System.out.println(">>>");
                // This job does not return any result.
                return null;
            }
        });
    }
    return jobs;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ComputeJobAdapter(org.apache.ignite.compute.ComputeJobAdapter) ClassPathResource(org.springframework.core.io.ClassPathResource) ComputeJob(org.apache.ignite.compute.ComputeJob) AbstractBeanFactory(org.springframework.beans.factory.support.AbstractBeanFactory) XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with XmlBeanFactory

use of org.springframework.beans.factory.xml.XmlBeanFactory in project ignite by apache.

the class GridUriDeploymentSpringParser method parseTasksDocument.

/**
 * Converts given input stream expecting XML inside to
 * {@link GridUriDeploymentSpringDocument}.
 * <p>
 * This is a workaround for the {@link InputStreamResource} which does
 * not work properly.
 *
 * @param in Input stream with XML.
 * @param log Logger
 * @return Grid wrapper for the input stream.
 * @throws org.apache.ignite.spi.IgniteSpiException Thrown if incoming input stream could not be
 *      read or parsed by {@code Spring} {@link XmlBeanFactory}.
 * @see XmlBeanFactory
 */
static GridUriDeploymentSpringDocument parseTasksDocument(InputStream in, IgniteLogger log) throws IgniteSpiException {
    assert in != null;
    // Note: use ByteArrayResource instead of InputStreamResource because InputStreamResource doesn't work.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        U.copy(in, out);
        XmlBeanFactory factory = new XmlBeanFactory(new ByteArrayResource(out.toByteArray()));
        return new GridUriDeploymentSpringDocument(factory);
    } catch (BeansException | IOException e) {
        throw new IgniteSpiException("Failed to parse spring XML file.", e);
    } finally {
        U.close(out, log);
    }
}
Also used : XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) BeansException(org.springframework.beans.BeansException)

Aggregations

XmlBeanFactory (org.springframework.beans.factory.xml.XmlBeanFactory)5 ClassPathResource (org.springframework.core.io.ClassPathResource)4 BeanFactory (org.springframework.beans.factory.BeanFactory)2 Resource (org.springframework.core.io.Resource)2 Person (entity.Person)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 ComputeJob (org.apache.ignite.compute.ComputeJob)1 ComputeJobAdapter (org.apache.ignite.compute.ComputeJobAdapter)1 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)1 Nullable (org.jetbrains.annotations.Nullable)1 Test (org.junit.Test)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 BeansException (org.springframework.beans.BeansException)1 AbstractBeanFactory (org.springframework.beans.factory.support.AbstractBeanFactory)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1