6
A
回答
10
不,你不能。如果您需要参数,则必须事先将其注入为字段。
样品豆
public class Foo{
@Autowired
private Bar bar;
public void init(){
bar.doSomething();
}
}
示例XML:
<bean class="Foo" init-method="init" />
2
,当你不能改变你想创建在以前的答案类,但你是相当有工作,此方法特别有用一个API并且必须照原样使用提供的bean。
你总是可以创建一个类(MyObjectFactory)实现的FactoryBean和getObject()方法中,你应该写:
@Autowired
private MyReferenceObject myRef;
public Object getObject()
{
MyObject myObj = new MyObject();
myObj.init(myRef);
return myObj;
}
在春天的context.xml想得简单:
<bean id="myObject" class="MyObjectFactory"/>
0
protected void invokeCustomInitMethod(String beanName, Object bean, String initMethodName)
throws Throwable {
if (logger.isDebugEnabled()) {
logger.debug("Invoking custom init method '" + initMethodName +
"' on bean with beanName '" + beanName + "'");
}
try {
Method initMethod = BeanUtils.findMethod(bean.getClass(), initMethodName, null);
if (initMethod == null) {
throw new NoSuchMethodException("Couldn't find an init method named '" + initMethodName +
"' on bean with name '" + beanName + "'");
}
if (!Modifier.isPublic(initMethod.getModifiers())) {
initMethod.setAccessible(true);
}
initMethod.invoke(bean, (Object[]) null);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
看到Spring是soruce代码Method initMethod = BeanUtils.findMethod(bean.getClass(), initMethodName, null);
init方法是发现和param为null
相关问题
- 1. Spring portlet mvc初始化方法
- 2. 初始化方法
- 3. Spring初始化bean初始化
- 4. Spring初始化方法和销毁方法
- 5. 切片类型的初始化方法无法初始化
- 6. NSArray的初始化方法
- 7. NSMutableArray初始化方法
- 8. 初始化方法调用
- 9. dll初始化方法
- 10. Ruby对象初始化:params,hash,vanilla?
- 11. 无法初始化使用Spring
- 12. Tapestry - Spring - 无法初始化上下文
- 13. Spring MVC无法初始化类com.mongodb.MongoClient
- 14. 用SpringMVC初始化Spring-Security
- 15. Spring XML初始化命令
- 16. Spring上下文初始化
- 17. 从web.xml初始化spring bean
- 18. Spring的静态初始化
- 19. 登录Spring初始化
- 20. 用Spring初始化Log4J?
- 21. Spring bean懒初始化
- 22. spring mvc servlet初始化
- 23. Spring + Apache CXF:SearchContextProvider未初始化
- 24. Spring DefaultMessageListenerContainer MDP初始化
- 25. Spring Boot - java.lang.IllegalStateException:ApplicationEventMulticaster未初始化
- 26. 的Objective-C初始化/初始化方法
- 27. Guice在初始化对象后调用初始化方法
- 28. 使用超类方法初始化与初始化方法之间的区别
- 29. Spring bean初始化两次 - Spring Integration
- 30. Spring MVC的 - @初始化粘合剂的方法不叫
好的,谢谢你的信息 – Radu