2013-02-06 8 views
7

मेरे पास एक सर्वलेट 3.0 वेब ऐप है जो स्प्रिंग और जर्सी दोनों का उपयोग करता है। मैंने वर्तमान में इसे web.xml में फ़िल्टर के रूप में कॉन्फ़िगर किया गया स्प्रिंगसेर्लेट का उपयोग करके स्थापित किया है, और @Path और @Component दोनों के साथ एनोटेटेड संसाधन वर्ग। यहाँ web.xml टुकड़ा है:केवल एनोटेशन का उपयोग करके वसंत के साथ जर्सी को कॉन्फ़िगर करें

<filter> 
    <filter-name>jersey-serlvet</filter-name> 
    <filter-class> 
     com.sun.jersey.spi.spring.container.servlet.SpringServlet 
    </filter-class> 
    <init-param> 
     <param-name> 
      com.sun.jersey.config.property.packages 
     </param-name> 
     <param-value>com.foo;com.bar</param-value> 
    </init-param> 
    <init-param> 
     <param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name> 
     <param-value>true</param-value> 
    </init-param> 
</filter> 

<filter-mapping> 
    <filter-name>jersey-serlvet</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

इस सेटअप काम करता है, लेकिन मैं वास्तव में यह केवल एनोटेशन के साथ सेट प्राप्त करना चाहते हैं - कोई web.xml config। इस पर मेरा पहला प्रयास उपरोक्त स्प्रिंगसेर्लेट कॉन्फ़िगरेशन को हटाने और Application तक फैला हुआ एक वर्ग बनाना था। यहाँ इस बात का एक टुकड़ा है:

@ApplicationPath("/*") 
public class MyApplication extends PackagesResourceConfig { 

    public MyApplication() { 
     super("com.foo;com.bar"); 

     HashMap<String, Object> settings = new HashMap<String, Object>(1); 
     settings.put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, true); 
     this.setPropertiesAndFeatures(settings); 
    } 
} 

इस में काम करता है कि JAX-आरएस संसाधनों पंजीकृत हैं और मैं उन्हें अपने URL पर हिट कर सकते हैं, लेकिन वे NullPointerExceptions फेंक जब वे कोशिश करते हैं और उनके autowired गुण का उपयोग करें ... इस बनाता है समझ में आता है क्योंकि मैं संसाधनों का अनुमान लगा रहा हूं अब जर्सी द्वारा लोड किया जा रहा है और स्प्रिंग प्रबंधित बीन्स नहीं हैं, इसलिए कोई ऑटोवॉयरिंग नहीं है।

चारों ओर खोज करने के काफी हद तक मुझे जर्सी संसाधनों को केवल एनोटेशन के साथ वसंत बीन्स के रूप में लोड करने का कोई तरीका नहीं मिल रहा है। क्या ऐसा कोई तरीका है? मैं वास्तव में वसंत संदर्भ मैन्युअल रूप से लाने के लिए संसाधनों के लिए कोड का एक गुच्छा लिखना नहीं चाहता हूं और यदि मैं इसकी सहायता कर सकता हूं तो DI को आह्वान करना चाहता हूं।

यदि टिप्पणियां केवल काम नहीं करने जा रही हैं, तो मैं वेब.एक्सएमएल में फ़िल्टर कॉन्फ़िगरेशन के साथ रह सकता हूं यदि मैं Application कक्षा को स्कैन करने के लिए संकुल की सूची के बजाय लोड करने के लिए निर्दिष्ट कर सकता हूं। अगर मैं वहां पैकेज सूची से छुटकारा पा सकता हूं और केवल Application क्लास इंस्टेंस निर्दिष्ट कर सकता हूं तो मैं सामग्री दूंगा।

स्पष्ट रूप से यह बहुत अच्छा होगा अगर किसी के पास मेरे लिए एक निश्चित उत्तर था, लेकिन मैं किसी भी पॉइंटर्स या संकेतों के लिए आभारी हूं कि मैं कहां देख सकता हूं या कोशिश करने के लिए चीजें।

धन्यवाद, मैट

उत्तर

1

मैं अपने आदर्श परिणाम प्राप्त करने में सक्षम नहीं किया गया है, लेकिन मैं कुछ प्रगति करने के लिए सक्षम किया गया है, इसलिए मैं यहाँ मामले में पोस्ट करेंगे यह किसी और में मदद करता है। मैं अपने आवेदन वर्ग को निर्दिष्ट करने के लिए स्प्रिंग सर्वलेट का उपयोग करने में सक्षम था, जिससे वेब.एक्सएमएल से पैकेज सूची को हटा दिया गया।

web.xml आवश्यक परिवर्तन (फिल्टर मानचित्रण नहीं दिखाया गया है, लेकिन अभी भी आवश्यक है) init पैरामीटर में हैं:

<filter> 
    <filter-name>jersey-serlvet</filter-name> 
    <filter-class> 
     com.sun.jersey.spi.spring.container.servlet.SpringServlet 
    </filter-class> 
    <init-param> 
     <param-name>javax.ws.rs.Application</param-name> <!-- Specify application class here --> 
     <param-value>com.foo.MyApplication</param-value> 
    </init-param> 
</filter> 

और फिर आवेदन कक्षा में मैं जिस तरह से मैं बुलाया बदलना पड़ा थोड़ा सुपर निर्माता:

public MyApplication() { 
    super("com.foo", "com.bar"); // Pass in packages as separate params 

    HashMap<String, Object> settings = new HashMap<String, Object>(1); 
    settings.put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, true); 
    this.setPropertiesAndFeatures(settings); 
} 

फिर भी वास्तव में क्या नहीं मैं के बाद था, लेकिन कम से कम इस जावा कोड में और web.xml, जो मेरे लिए महत्वपूर्ण है के रूप में मैं छिपाने की कोशिश कर रहा हूँ से बाहर एक छोटे से अधिक config खींचती यह विवरण

1

दो विकल्प वसंत करने के लिए वसंत (कोई इरादा नहीं है)।

  1. हो सकता है कि आप अपने खुद के वर्ग के साथ SpringServlet का विस्तार करने और इसे करने के लिए उचित सर्वलेट 3.0 एनोटेशन जोड़ सकते हैं।
  2. SpringServlet से Application कक्षा में स्विच करने के आपके दृष्टिकोण के साथ-साथ, आप स्प्रिंग बिल्ड-टाइम या लोड-टाइम बाइटकोड बुनाई को सक्षम करके नो-ऑटोवॉयरिंग समस्या को हल कर सकते हैं।इससे स्प्रिंग द्वारा बनाई गई वस्तुओं की बजाय कहीं भी ऑब्जेक्ट को इंजेक्ट करने के लिए वसंत को सक्षम किया जाता है। "Using AspectJ to dependency inject domain objects with Spring" देखें।
1

सबसे पहले, सर्वलेट 3.0 कंटेनर में आपको वास्तव में web.xml की आवश्यकता नहीं है।

लेकिन जर्सी 2.0 के साथ आप टिप्पणी किए गए संसाधनों के लिए पूरे वेब अनुप्रयोग स्कैन करने के लिए ध्वज सेट कर सकते हैं:

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>jersey</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>jersey.config.servlet.provider.webapp</param-name> 
     <param-value>true</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

स्प्रिंग स्वचालित रूप से सक्षम हो जाएगा अगर आप इस जार में शामिल हैं:

<dependency> 
     <groupId>org.glassfish.jersey.ext</groupId> 
     <artifactId>jersey-spring3</artifactId> 
     <version>2.3.1</version> 
    </dependency> 
+0

आपके उत्तर के लिए धन्यवाद, लेकिन मैं जर्सी 1 का उपयोग कर रहा हूँ और मैं किसी भी मुसीबत हो रही है की जरूरत नहीं है जर्सी संसाधन लोड और चल रहा है। मुद्दा यह है कि मैं उन्हें वसंत द्वारा प्रबंधित करना चाहता हूं इसलिए मुझे ऑटोवॉयरिंग मिलती है। – Doughnuts

+0

ठीक है, उस स्थिति में यह आपके लिए समाधान नहीं है। लेकिन बस स्पष्ट होना: यह वसंत autowiring सक्षम बनाता है। – rustyx

2

नीचे है मेरे ऐप का हिस्सा, जो सर्वलेट 3.0, स्प्रिंग, जर्सी 1.8 का उपयोग करता है और इसमें कोई वेब.एक्सएमएल नहीं है:

public class WebAppInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) throws ServletException { 
    final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
    context.setConfigLocation("com.myapp.config"); 

    final FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter()); 
    characterEncodingFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); 
    characterEncodingFilter.setInitParameter("encoding", "UTF-8"); 
    characterEncodingFilter.setInitParameter("forceEncoding", "true"); 

    final FilterRegistration.Dynamic springSecurityFilterChain = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy()); 
    springSecurityFilterChain.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*"); 

    servletContext.addListener(new ContextLoaderListener(context)); 
    servletContext.setInitParameter("spring.profiles.default", "production"); 

    final SpringServlet servlet = new SpringServlet(); 

    final ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", servlet); 
    appServlet.setInitParameter("com.sun.jersey.config.property.packages", "com.myapp.api"); 
    appServlet.setInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", "com.myapp.api.SizeLimitFilter"); 
    appServlet.setLoadOnStartup(1); 

    final Set<String> mappingConflicts = appServlet.addMapping("/api/*"); 

    if (!mappingConflicts.isEmpty()) { 
     throw new IllegalStateException("'appServlet' cannot be mapped to '/' under Tomcat versions <= 7.0.14"); 
    } 
} 

}

0

मैंने स्प्रिंगएमवीसी का उपयोग कर अपने पहले बनाए गए प्रोजेक्ट के साथ जर्सी का उपयोग किया। मैंने Spring's official documentation पर अपना कोड आधारित किया।

public class WebAppInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) { 
    // Don't create the Listener that Jersey uses to create. 
    // There can only be one linstener 
    servletContext.setInitParameter("contextConfigLocation", "<NONE>"); 
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 

    // Add app config packages 
    context.setConfigLocation("config.package"); 

    // Add listener to the context 
    servletContext.addListener(new ContextLoaderListener(context)); 

    // Replacing: 
    //  <servlet-name>ServletName</servlet-name> 
    //  <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> 
    //  <init-param> 
    //   <param-name>com.sun.jersey.config.property.packages</param-name> 
    //   <param-value>webservices.packages</param-value> 
    //  </init-param> 
    //  <load-on-startup>1</load-on-startup> 
    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); 

    ServletRegistration.Dynamic appServlet = servletContext.addServlet("ServletName", new DispatcherServlet(dispatcherContext)); 
    appServlet.setInitParameter("com.sun.jersey.config.property.packages", "org.sunnycake.aton.controller"); 
    appServlet.setLoadOnStartup(1); 
    appServlet.addMapping("/RootApp"); 

} 
} 

config.package में विन्यास वर्ग हैं:

// Specifies that there will be bean methods annotated with @Bean tag 
// and will be managed by Spring 
@Configuration 

// Equivalent to context:component-scan base-package="..." in the xml, states 
// where to find the beans controlled by Spring 
@ComponentScan(basePackages = "config.package") 
public class AppConfig { 

    /** 
    * Where will the project views be. 
    * 
    * @return ViewResolver como el XML 
    */ 
    @Bean 
    public ViewResolver viewResolver() { 
     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
     return viewResolver; 
    } 

} 

हाइबरनेट विन्यास

// Specifies that there will be bean methods annotated with @Bean tag 
// and will be managed by Spring 
@Configuration 
// Equivalent to Spring's tx in the xml 
@EnableTransactionManagement 

// Equivalent to context:component-scan base-package="..." in the xml, states 
// where to find the beans controlled by Spring 
@ComponentScan({"config.package"}) 

// Here it can be stated some Spring properties with a properties file 
@PropertySource(value = {"classpath:aplicacion.properties"}) 
public class HibernateConfig { 

    /** 
    * Inyected by Spring based on the .properties file in the 
    * \@PropertySource tag. 
    */ 
    @Autowired 
    private Environment environment; 

    /** 
    * Here it's created a Session Factory, equivalent to the Spring's config file one. 
    * 
    * @return Spring Session factory 
    */ 
    @Bean 
    public LocalSessionFactoryBean sessionFactory() { 
     LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 

     // Uses the datasource 
     sessionFactory.setDataSource(dataSource()); 

     // Indicates where are the POJOs (DTO) 
     sessionFactory.setPackagesToScan(new String[]{"dto.package"}); 
     // Se asignan las propiedades de Hibernate 
     sessionFactory.setHibernateProperties(hibernateProperties()); 

     return sessionFactory; 
    } 

    /** 
    * Propiedades de la base de datos (Según environment) 
    * 
    * @return Nuevo DataSource (Configuración de la base de datos) 
    */ 
    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); 
     dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); 
     dataSource.setUsername(environment.getRequiredProperty("jdbc.username")); 
     dataSource.setPassword(environment.getRequiredProperty("jdbc.password")); 
     return dataSource; 
    } 

    /** 
    * Hibernate properties 
    * 
    * @return Properties set with the configuration 
    */ 
    private Properties hibernateProperties() { 
     Properties properties = new Properties(); 
     // Dialect (Mysql, postgresql, ...) 
     properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); 
     // Show SQL query 
     properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql")); 
     properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql")); 
     return properties; 
    } 

    /** 
    * Inyected by sessionFactory 
    */ 
    @Bean 
    @Autowired 
    public HibernateTransactionManager transactionManager(SessionFactory s) { 
     HibernateTransactionManager txManager = new HibernateTransactionManager(); 
     txManager.setSessionFactory(s); 
     return txManager; 
    } 
} 
संबंधित मुद्दे

 संबंधित मुद्दे