I need only a single instance of Twitter class in Twitter4j in my Spring container. My problem is that I can see no way to set the oauth consumer in the Twitter class. As I far as I know,you can only wire using a setter method which only takes in one parameter. Not two. For example there is think there is no way I can wire something like this:
Twitter twitter = new Twitter();
twitter.setOAuthConsumer([consumer key],[consumer secret]);
Of course I want to avoid hard-coding the consumer key and consumer secret that defeats using D.I I guess.
My solution is to encapsulate the twitter class in another class so I can wire the consumer key and consumer secret one by one:
public class TwitterAuth {
private Twitter twitter;
public TwitterAuth(Twitter twitter, consumerKey, consumerSecret) {
this.twitter=twitter;
twitter.setOauthConsumer(consumerKey,consumerSecret);
}
public void getTwitter(){
return twitter;
}
}
Although it does solve my problem, it presents me with another one. I don't need the TwitterAuth anymore once the twitter class is injected. How do I discard the TwitterAuth?
Better yet is there a better way to wire this? Thanks! :)