Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Friday, January 29, 2016

Decorator Pattern with Spring

The decorator pattern is well known and described.
See for example https://en.wikipedia.org/wiki/Decorator_pattern

 The question is how can we implement easily the pattern with Spring.

 Lets just do an example.
 We have a SearchService which can be configuratively be switch on or off.

We have a simple interface
public interface SearchService{
    List find(Param p);
}
and a simple implementation
@Component
public class DefaultSearchService implements SearchService{
    public List find(Param p){
        repository.find(p);
    }
}
Our decorator logically implements the SearchService interface as well
@Component
public class SearchServiceDecorator implements SearchService{

    private final SearchService searchService;
    private final Config config;

    @Autowired
    public SearchServiceDecorator(SearchService searchService, Config config){
        this.searchService = searchService;
    }

    public List find(Param p){
        if(config.allowed())
            searchSerice.find(p);
    }
}
This will obviously not work. Spring cannot decide which version of SearchService to use. The solution is quite elegant:
@Component
@Primary
public class SearchServiceDecorator implements SearchService{

    private final SearchService searchService;
    private final Config config;

    @Autowired
    public SearchServiceDecorator(@Named("defaultSearchService")SearchService searchService, Config config){
        this.searchService = searchService;
    }

    public List find(Param p){
        if(config.allowed())
            searchSerice.find(p);
    }
}
The @Primary annotation, according to its Javadoc "Indicates that a bean should be given preference when multiple candidates * are qualified to autowire a single-valued dependency." The @Named allows to inject the decorator chain. Thats it :)

Wednesday, October 7, 2015

JavaFx with Spring Boot

I guess I'm not the only one trying to make JavaFx application with Spring Boot. I ended up with the best summary I could find to date (http://stackoverflow.com/questions/28804012/javafx-fxml-how-to-use-spring-di-with-nested-custom-controls) I will only put it here so I can find it again quickly :)
@SpringBootApplication
public class FxBootApplication extends Application {
    private static String[] args;

    public static void main(String[] args) {
        FxBootApplication.args = args;
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {

        // Bootstrap Spring context here.
        ApplicationContext context = SpringApplication.run(FxBootApplication.class, args);

        // Create a Scene
        MainPaneController mainPaneController = context.getBean(MainPaneController.class);
        Scene scene = new Scene((Parent) mainPaneController.getRoot());

        // Set the scene on the primary stage
        primaryStage.setScene(scene);
        // Any other shenanigans on the primary stage...
        primaryStage.show();
    }
}
@Configuration
public class ApplicationConfiguration {

    @Bean
    public MainPaneController mainPaneController() throws IOException {
        return loadController(MainPaneController.VIEW);
    }

    private  T loadController(String url) throws IOException {
        try (InputStream fxmlStream = getClass().getResourceAsStream(url)) {
            FXMLLoader loader = new FXMLLoader();
            loader.load(fxmlStream);
            return loader.getController();
        }
    }
}
public class MainPaneController {
    public static final String VIEW = "/MainPanel.fxml";

    @FXML
    private Node root;
   
    @PostConstruct
    public void init() {}

    public Node getRoot() {
        return root;
    }
}

Monday, May 30, 2011

Spring PersistenceUnitPostProcessor

I needed 2 persistence.xml files, one for testing and one for production.
The only difference was the transaction-type which I had to set to JTA, as we were using Atomikos as a transaction manager for production. But for unit testing, resource-local was a better choice (less configuration).

The solution I found was to use a "PersistenceUnitPostProcessor" which changes the value after loading.




















The interesting part is the persistenceUnitPostProcessors property which registers a post processor.
Notice also the persistenceXmlLocation property. As we deploy on Websphere and make use of JPA 2.0, we can't name our file persistence.xml. It would conflict with Webphere !

The "TransactionTypeSelectorPersistenceUnitPostProcessor" java class is trivial, it just sets the transaction type.


public class TransactionTypeSelectorPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {

private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;

@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
pui.setTransactionType(transactionType);
}

public void setTransactionType(PersistenceUnitTransactionType transactionType) {
this.transactionType = transactionType;
}

public PersistenceUnitTransactionType getTransactionType() {
return transactionType;
}

}

Thursday, December 16, 2010

Accessing spring beans from liferay templates

I had an interesting task, display the version of the portal.
The question which immediately arises is: how do I get from my velocity template to my spring beans?

1. The first stop is the VelocityVariable class from Liferay.
there your fine "serviceLocator" variable which sound just as the right variable to use.
2. In your tpl file add the following


  • $serviceLocator.findService("versionBean").getVersion()



3. You may think now that you only have to configure a Spring bean named "versionBean". Wrong!







The bean name must be augmented with ".velocity" and the bean you are talking to is not yours, but the "ProxyFactoryBean" in which you inject your bean!
4. The Java bean class is up to you then.

Thanks you Martin for your help with this one.