<?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"> <!-- Spring提供的实例化方法,第一种,在Spring配置全路径,Spring会调用类的空参构造方法实例化对象--> <bean id="springBean" class="com.powernode.spring6.bean.SpringBean"/> <!-- Spring的实例化方法,通过简单工厂模式,需要在Spring配置文件中告诉Spring框架,通过哪个类的哪个方法获取Bean--> <!-- 指定静态方法--> <bean id="starBean" class="com.powernode.spring6.bean.StarFactory" factory-method="get"/> <!-- Spring提供的实例化对象第三种,通过工厂方法模式,通过factory-bean属性+factory-method属性来共同完成,Spring要调用哪个对象的哪个方法来获取Bean--> <!-- 指定实例方法--> <bean id="gunFactory" class="com.powernode.spring6.bean.GunFactory"/> <!-- 以下的配置很关键,factory-bean属性告诉Spring调用哪个对象,factory-method告诉Spring调用对象的哪个方法--> <bean id="Gun" factory-bean="gunFactory" factory-method="get"/> </beans>
<?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">
<!-- Spring提供的实例化方法,第一种,在Spring配置全路径,Spring会调用类的空参构造方法实例化对象-->
<bean id="springBean" class="com.powernode.spring6.bean.SpringBean"/>
<!-- Spring的实例化方法,通过简单工厂模式,需要在Spring配置文件中告诉Spring框架,通过哪个类的哪个方法获取Bean-->
<!-- 指定静态方法-->
<bean id="starBean" class="com.powernode.spring6.bean.StarFactory" factory-method="get"/>
<!-- Spring提供的实例化对象第三种,通过工厂方法模式,通过factory-bean属性+factory-method属性来共同完成,Spring要调用哪个对象的哪个方法来获取Bean-->
<!-- 指定实例方法-->
<bean id="gunFactory" class="com.powernode.spring6.bean.GunFactory"/>
<!-- 以下的配置很关键,factory-bean属性告诉Spring调用哪个对象,factory-method告诉Spring调用对象的哪个方法-->
<bean id="Gun" factory-bean="gunFactory" factory-method="get"/>
</beans>
package com.powernode.spring6.bean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class StarFactory { //简单工厂模式,静态工厂方法模式 private static final Logger logger = LoggerFactory.getLogger(StarFactory.class); public StarFactory() { logger.info("工厂开工"); } { logger.info("明星工厂存在了"); } public static Star get() { //这个Star对象最终创建时还是我们new出来的对象 logger.info("造星方法"); return new Star(); } }
package com.powernode.spring6.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StarFactory
{
//简单工厂模式,静态工厂方法模式
private static final Logger logger = LoggerFactory.getLogger(StarFactory.class);
public StarFactory()
{
logger.info("工厂开工");
}
{
logger.info("明星工厂存在了");
}
public static Star get()
{
//这个Star对象最终创建时还是我们new出来的对象
logger.info("造星方法");
return new Star();
}
}
package com.powernode.spring6.bean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Gun { private static final Logger logger = LoggerFactory.getLogger(Gun.class); //工厂方法模式当中的具体产品角色 public Gun() { logger.info("一把枪"); } }
package com.powernode.spring6.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Gun
{
private static final Logger logger = LoggerFactory.getLogger(Gun.class);
//工厂方法模式当中的具体产品角色
public Gun()
{
logger.info("一把枪");
}
}
package com.powernode.spring6.bean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class GunFactory { private static final Logger logger = LoggerFactory.getLogger(GunFactory.class); //工厂方法模式中的具体工厂角色中的方法时:实例对象 public GunFactory() { logger.info("枪械工厂开工"); } public Gun get() { logger.info("枪械工厂造枪"); return new Gun(); } }
package com.powernode.spring6.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GunFactory
{
private static final Logger logger = LoggerFactory.getLogger(GunFactory.class);
//工厂方法模式中的具体工厂角色中的方法时:实例对象
public GunFactory()
{
logger.info("枪械工厂开工");
}
public Gun get()
{
logger.info("枪械工厂造枪");
return new Gun();
}
}
package com.powernode.spring6.bean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SpringBean { private static final Logger logger = LoggerFactory.getLogger(SpringBean.class); public SpringBean() { logger.info("Bean的无参构造器已经启动"); } }
package com.powernode.spring6.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SpringBean
{
private static final Logger logger = LoggerFactory.getLogger(SpringBean.class);
public SpringBean()
{
logger.info("Bean的无参构造器已经启动");
}
}
package com.powernode.spring6.bean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Star { private final static Logger logger = LoggerFactory.getLogger(Star.class); public Star() { logger.info("Star无参构造方法启动"); } }
package com.powernode.spring6.bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Star
{
private final static Logger logger = LoggerFactory.getLogger(Star.class);
public Star()
{
logger.info("Star无参构造方法启动");
}
}
package com.powernode.spring6.test; import com.powernode.spring6.bean.Gun; import com.powernode.spring6.bean.SpringBean; import com.powernode.spring6.bean.Star; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { @org.junit.Test public void TestInstantiation() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); SpringBean SpringBean = applicationContext.getBean("springBean", SpringBean.class); System.out.println(SpringBean); Star star = applicationContext.getBean("starBean", Star.class); System.out.println(star); } @org.junit.Test public void TestInstantiation1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); Gun gun = applicationContext.getBean("Gun", Gun.class); System.out.println(gun); } }
package com.powernode.spring6.test;
import com.powernode.spring6.bean.Gun;
import com.powernode.spring6.bean.SpringBean;
import com.powernode.spring6.bean.Star;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
@org.junit.Test
public void TestInstantiation()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
SpringBean SpringBean = applicationContext.getBean("springBean", SpringBean.class);
System.out.println(SpringBean);
Star star = applicationContext.getBean("starBean", Star.class);
System.out.println(star);
}
@org.junit.Test
public void TestInstantiation1()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Gun gun = applicationContext.getBean("Gun", Gun.class);
System.out.println(gun);
}
}