Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Polish

Wstęp

Każda wtyczka ma własny kontekst aplikacji (ApplicationContext), w którym rejestrowane są jej komponenty, kontrolery, importowane serwisy etc. Dzięki temu wtyczka może być pisana tak jak każda inna aplikacja wykorzystująca SpringFramework.

Kontekst może być konfigurowany na 2 sposoby:

  1. przez plik plugin-context.xml w katalogu /META-INF/spring/
  2. przez klasę z adnotacją @Configuration

Konfiguracja XML

Jeżeli wtyczka zawiera plik /META-INF/spring/plugin-context.xml, to na jego podstawie tworzony jest XmlOsgiPluginContext.

Info

Jeżeli projekt buduje Maven, plik powinien znajdować się w src/main/resources/META-INF/spring/plugin-context.xml

 

Plik konfiguracji jest standardowym plikiem konfiguracji kontekstu aplikacji springframework i może wyglądać następująco:

Code Block
languagehtml/xml
titleplugin-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	
	<!-- Włączenie skanowania wtyczki w poszukiwaniu @Component -->
	<context:component-scan base-package="com.suncode.plugin.tutorial" />
	
</beans>

Tip

Konfiguracja poprzez plik XML ma swoje plusy i minusy. W ostateczności wszystko zależy od gustu autora wtyczki.

Konfiguracja Java

Od SpringFramework 3 możliwa jest konfiguracja kontekstu aplikacji w całości używając klas Java oznaczonych adnotacją @Configuration.

Jeżeli mechanizm wtyczek nie znajdzie pliku /META-INF/spring/plugin-context.xml to stworzy domyślny kontekst aplikacji, który skanuje całą wtyczkę w poszukiwaniu klas @Component (a więc również @Configuration).

Info

Klasa konfiguracyjna w takim przypadku nie jest w ogóle potrzebna, jednak zazwyczaj chcemy dodatkowe funkcjonalności jak dostęp do bazy danych, transakcyjność etc.

Przykładowa konfiguracja (rejestruje tylko 1 bean klasy String):

Code Block
languagejava
titleJava Configuration
package com.suncode.plugin.tutorial;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean
    public String someString() {
        return "asd";
    }
}

 

 

 

 

English

Introduction

Each plugin has its own application context (ApplicationContext), where its components, controllers, imported services etc. are registered. This allows the plug-in to be written just like any other application using SpringFramework.

The context can be configured in 2 ways:

  1. via the plugin-context.xml file in the /META-INF/spring/ directory
  2. via a class with the @Configuration annotation

XML configuration

If the plugin contains the /META-INF/spring/plugin-context.xml file XmlOsgiPluginContext is created based on it.

Info

If the project builds Maven, the file should be in src/main/resources/META-INF/spring/plugin-context.xml

 

The configuration file is a standard springframework application context configuration file and can look like this:

Code Block
languagehtml/xml
titleplugin-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	
	<!-- Enabling plugin scanning for @Component -->
	<context:component-scan base-package="com.suncode.plugin.tutorial" />
	
</beans>

Tip

Configuration via an XML file has its pros and cons. Ultimately, it all depends on the skills of the plug-in author.

Java configuration

Since SpringFramework 3, it is possible to configure the application context entirely using Java classes marked with the @Configuration annotation.

If the plugin engine does not find the /META-INF/spring/plugin-context.xml file it will create a default application context that scans the entire plugin for @Component (and thus also @Configuration).

Info

The configuration class in such a case is not needed at all, but usually we want additional functionality like database access, transactivity, etc.

Example configuration (registers only 1 bean of class String):

Code Block
languagejava
titleJava Configuration
package com.suncode.plugin.tutorial;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean
    public String someString() {
        return "asd";
    }
}