Java – inject EJB into eclipse link sessioncustomizer to provide Oracle schema name

In the Java EE 6 application running on GlassFish (3.1.2.2b5), suppose you have a configurationservice, which will read some property files and output property values accordingly:

@Local
public interface ConfigurationService { ... }
@Singleton  
public class ConfigurationServiceImpl implements ConfigurationService { ... }

There is also an eclipse link sessioncustomizer, because the schema name of a persistence unit (Oracle database) in the application needs to be set programmatically, that is, it can be configured from the previously mentioned property file Sessioncustomizer is in persistence XML, which contains a reference to configurationservice:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"...
    <persistence-unit name="myPU" transaction-type="JTA">
        <property name="eclipselink.session.customizer" value="MySessionCustomizer"/>
        ...
public class MySessionCustomizer implements SessionCustomizer {
    @EJB
    private ConfigurationService configurationService;
    @Override
    public void customize(Session session) {
        session.getLogin().setTableQualifier(configurationService.getSchemaName());
        ...

Can configurationservice be injected in this way to be available when sessioncustomizer is instantiated? The above failed because the configurationservice instance is still empty, that is, the injection has not yet occurred This observation corresponds to the log entry of the server It seems that the dependency injection mechanism is always started after the persistence unit, so the sessioncustomizer is customized I've screwed up all kinds of comments (@ startup, @ dependson (...),...), but it's useless Is my conclusion correct or are there more EJB instantiation and injection methods?

Solution

Since the session customization program was created by eclipse link (not by your container), the container is not responsible for injecting dependencies

Use JNDI to find

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>