0

我想在没有 DM 的情况下从 Spring bean 访问 OSGI 服务。目前我有 2 个捆绑包,捆绑包 A 只是公开服务,捆绑包 B 是带有 JSF 和 Spring Security 的 Web 应用程序。以下是项目结构:

-webapp
|
-OSGI-INF/蓝图/蓝图.xml

<?xml version="1.0" encoding="UTF-8"?>  
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

    <bean id="sampleBean" class="com.test.bundleb.bean.SampleBean" init-method="create" activation="eager">
        <property name="bundleAService" ref="bundleAService"></property>
    </bean>
    <reference id="bundleAService" interface="com.test.bundlea.service.BundleAService"/>
</blueprint>  

|
-WEB-INF/faces-config.xml

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
    version="2.1">
    <application>
        <message-bundle>messages</message-bundle>=
        <el-resolver>com.test.bundleb.listener.OsgiELResolver</el-resolver>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>  

请注意,对于从 jsf bean 访问 osgi bean,以下解决方案按原样使用并且它正在工作。
将蓝图 OSGi 服务注入 JSF/PrimeFaces bean

|
-WEB-INF/web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0">

    <description>Test Web Application</description>

    <context-param>
        <param-name>facelets.VIEW_MAPPINGS</param-name>
        <param-value>*.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <!-- FIXME This should not be required, but Jetty does not pick up the
        listener from a TLD at the moment. -->
    <listener>
        <listener-class>org.apache.myfaces.webapp.StartupServletContextListener
        </listener-class>
    </listener>

    <!-- For Spring Security. -->
    <listener>
        <listener-class>com.bundleb.listener.ServiceLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  
</web-app>  

|
-WEB-INF/spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
                             http://www.springframework.org/schema/beans/spring-beans.xsd
                             http://www.springframework.org/schema/security
                             http://www.springframework.org/schema/security/spring-security.xsd">

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/secured" access="ROLE_USER"/>
        <intercept-url pattern="/scripts" access="isAuthenticated()"/>
        <form-login login-page="/login.xhtml" default-target-url="/secured/welcome.xhtml"
                authentication-failure-url="/login.xhtml?status=error"/>
        <logout logout-success-url="/login.xhtml?status=logout"/>
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="abc" password="ABC" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

我可以从 JSF bean 访问 OSGI 服务,也可以初始化 spring 上下文并且安全工作正常。但是,我无法从 Spring bean 访问 OSGI 服务。有没有办法在不使用 Spring DM 的情况下从 spring bean 访问它?

4

0 回答 0