Bean的实例化--静态方法

1,Bean2

package com.songyan.factory;

public class Bean2 {

}

2,MyBean2Factory

package com.songyan.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestFactory {
public static void main(String[] args) {
    ApplicationContext  applicationContext=new ClassPathXmlApplicationContext("com/songyan/factory/beans2.xml");
    Bean2 bean=(Bean2)applicationContext.getBean("factory");
    System.out.println(bean);
}
}

3,配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean1" class="com.songyan.ico.Bean1"></bean>
</beans>

4,测试类

package com.songyan.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestFactory {
public static void main(String[] args) {
    ApplicationContext  applicationContext=new ClassPathXmlApplicationContext("com/songyan/factory/beans2.xml");
    Bean2 bean=(Bean2)applicationContext.getBean("factory");
    System.out.println(bean);
}
}

5,输出

猜你喜欢

转载自www.cnblogs.com/excellencesy/p/9103622.html