首页 > 代码库 > spring注入bean的五种方式
spring注入bean的五种方式
1.属性注入
2.构造方法注入
3.静态工厂注入
package com.voole.factorybeans; import com.voole.beans.TestBean; public class StaticFactoryBean { public static TestBean createInstance(){ return new TestBean(); } }
<bean id="staticFactoryTestBean" class="com.voole.factorybeans.StaticFactoryBean" factory-method="createInstance" scope="prototype"></bean>
4.实例工厂注入
package com.voole.factorybeans; import com.voole.beans.TestBean; public class InstanceFactoryBean { public TestBean createTestBean(){ return new TestBean(); } } <bean id="instanceFactoryBean" class="com.voole.factorybeans.InstanceFactoryBean"></bean> <bean id="instanceFactoryTestBean" factory-bean="instanceFactoryBean" factory-method="createTestBean" scope="prototype"></bean>
5.FactoryBean注入(这种方式最为简单)
package com.voole.factorybeans; import org.springframework.beans.factory.FactoryBean; import com.voole.beans.TestBean; public class TestFactoryBean implements FactoryBean<TestBean>{ @Override public TestBean getObject() throws Exception { return new TestBean(); } @Override public Class<?> getObjectType() { return TestBean.class; } @Override public boolean isSingleton() { return false; } }
<bean id="testBean" class="com.voole.factorybeans.TestFactoryBean"></bean>
spring注入bean的五种方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。