我需要将一些对象放入ActiveMQ然后反序列化它。该对象包含瞬态字段,应在反序列化时初始化。根据这个文档(http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable),它应该在Spring中自动运行,但它并没有。反序列化后,CloudBean始终为null。
以下是我的课程:
package core.utilities.handlers;
//imports...
@Component
@Configurable
public class InitialCopyHandler implements JobHandler {
private static final long serialVersionUID = 2617677940515776720L;
private static Logger logger = Logger.getLogger(InitialCopyHandler.class);
private String username;
private String password;
@Autowired private transient CloudBean cloudBean;
public InitialCopyHandler() {
}
public InitialCopyHandler( String username, String password ) {
this.username = username;
this.password = password;
}
@Override
public void handle() {
try {
logger.info("Copy from default to " + username);
String webCloudDataDir = cloudBean.getWebCloudDataDir();
Sardine defaultSardine = cloudBean.getDefaultSardine();
InputStream is = defaultSardine.get(webCloudDataDir + FILE_COUNTRIES_LIST);
cloudBean.getUserSardine(username,password).put(webCloudDataDir + FILE_COUNTRIES_LIST, is);
} catch (IOException e) {
e.printStackTrace();
}
}
}
CloudBean.java:
package core.domain.cloud;
//imports...
@Component
public class CloudBean {
protected Sardine defaultSardine;
protected Sardine userSardine;
protected String webCloudDataDir;
protected String webCloud;
protected static String defaultCar;
protected static String defaultCarPassword;
protected HttpHeaders headers;
@Autowired private ConfigurationFactory configuration;
public CloudBean() {
}
// init block
@PostConstruct
private void init() {
webCloudDataDir = configuration.getConfigurationValue(DEFAULT_CLOUD_WEBDAV_LOCATION) + DIR_CLOUD_DATA;
webCloud = configuration.getConfigurationValue(DEFAULT_CLOUD_LOCATION);
defaultCar = configuration.getConfigurationValue(DEFAULT_CLOUD_ACCOUNT);
defaultCarPassword = configuration.getConfigurationValue(DEFAULT_CLOUD_ACCOUNT_PASSWORD);
defaultSardine = SardineFactory.begin(defaultCar, defaultCarPassword);
defaultSardine.enableCompression();
headers = HttpUtilities.initHeaders(defaultCar,defaultCarPassword);
}
/**
* Initializes Sardine for the particular user
* @param username
* @param password
* @return Sardine
*/
public Sardine getUserSardine( String username, String password ) {
userSardine = SardineFactory.begin(username,password);
userSardine.enableCompression();
return userSardine;
}
public Sardine getDefaultSardine() {
return defaultSardine;
}
public String getWebCloudDataDir() {
return webCloudDataDir;
}
}
摘自Consumer.java(反序列化部分):
@Override
public void onMessage(Message message) {
if( message instanceof ObjectMessage ) {
ObjectMessage objectMessage = (ObjectMessage)message;
try {
JobHandler msg = (JobHandler)objectMessage.getObject();
es.execute(new Runnable() {
@Override
public void run() {
msg.handle();
}
});
} catch (JMSException e) {
e.printStackTrace();
}
}
}
有什么问题?
答案 0 :(得分:1)
在你的代码中Spring不参与反序列化,因此它不能注入任何依赖项。
在链接弹簧文档中,pojos由弹簧工厂创建。您无法使用该方法,因为您通过反序列化来创建实例。
你能做什么:
在您的Consumer类中注入一个bean工厂:
myProg
反序列化对象后,自动装配它:
@Autowired
AutowireCapableBeanFactory beanFactory;
这就是全部。