[JAVA] Rufen Sie Spring Management Bean von Pojo

Was du machen willst

Manchmal möchte ich eine Spring Management Bean aus einer Klasse aufrufen, die nicht von Spring verwaltet wird, daher werde ich die Implementierungsmethode zusammenfassen.

Umgebung

java8 javaee7 spring4.3.9 eclipse4.6

Implementierungsplan

Der Dienst, den Sie anrufen möchten

Der Dienst, den Sie anrufen möchten


package com;

import org.springframework.stereotype.Service;

@Service
public class TestService {
    public void execute(){
        System.out.println("com.TestService#execute");
    }
}

Vorschlag 1 Statischer Zugriff auf Spring Management Bean

Erstellen Sie eine Spring Management Bean, die den ApplicationContext enthält, und greifen Sie über die statische Methode von pojo darauf zu.

Spring Management Bean, die ApplicationContext enthält


package com.bridge;

import com.TestService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextBridge implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Autowired
    private TestService testService;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static SpringContextBridge getInstance() {
        return applicationContext.getBean(SpringContextBridge.class);
    }

    public TestService getTestService() {
        return testService;
    }
}

Pojo-Klasse


package com.bridge;

public class SpringContextBridgeClient {
    public static void main(String[] args) {
        SpringContextBridge.getInstance().getTestService().execute();
    }
}

Referenzseite https://blog.jdriven.com/2015/03/using-spring-managed-bean-in-non-managed-object/

Vorschlag 2 @Configurable

Ersetzen Sie beim Kompilieren mit AspectJ den neuen Teil der Klasse durch @Configurable (wahrscheinlich, weil ich die Quelle nicht überprüft habe).

@Eine Klasse, die mit Configurable zu einer Spring Management Bean wird


package com.configurable;

import com.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

@Configurable
public class ConfigurableClient {

    @Autowired
    private TestService testService;

    public void execute() {
        testService.execute();
    }
}

Pojo-Klasse


package com.configurable;

public class Configurable {
    public static void main(String[] args) {
        //Auch wenn es von new generiert wird, wird es DI sein!
        new ConfigurableClient().execute();
    }
}

application-context.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
					       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
					       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
					       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
    <context:spring-configured/>
    <context:component-scan base-package="com.configurable"/>
    <context:annotation-config />
</beans>

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test1</groupId>
  <artifactId>artifact1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>artifact1</name>
  <url>http://maven.apache.org</url>

  <properties>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<aspectj.version>1.6.12</aspectj.version>
    <spring.version>4.3.9.RELEASE</spring.version>
  </properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>aspectj-maven-plugin</artifactId>
				<version>1.4</version>
				<dependencies>
					<dependency>
						<groupId>org.aspectj</groupId>
						<artifactId>aspectjrt</artifactId>
						<version>${aspectj.version}</version>
					</dependency>
					<dependency>
						<groupId>org.aspectj</groupId>
						<artifactId>aspectjtools</artifactId>
						<version>${aspectj.version}</version>
					</dependency>
				</dependencies>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<aspectLibraries>
						<aspectLibrary>
							<groupId>org.springframework</groupId>
							<artifactId>spring-aspects</artifactId>
						</aspectLibrary>
					</aspectLibraries>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

kompilieren und mit maven ausführen


mvn clean compile
mvn exec:java -Dexec.mainClass="com.configurable.Configurable"

Wenn Sie mit Eclipse anstelle des Befehls maven ausgeführt werden möchten

Wenn Sie pom.xml mit Eclipse öffnen, sollte der folgende Fehler angezeigt werden.

Ausführung von Plugins, die nicht von der Lebenszykluskonfiguration abgedeckt werden: org.codehaus.mojo: Aspektj-Maven-Plugin: 1.4: Kompilieren (Ausführung: Standard, Phase: Kompilieren)

Es ist notwendig, AJDT zu installieren, damit aspilej compile verwendet werden kann.

AJDT-Installation

Jetzt kannst du es schaffen!

Referenzseite http://ryu-htn.hatenablog.com/entry/2013/04/25/151240 https://github.com/kenyattaclark/Spring-Configurable-Example https://tamasgyorfi.net/2013/11/13/spring-dependency-injection-configurable/

Referenzseite (Eclipse-Fehler) http://tyru.hatenablog.com/entry/20140905/eclipse_lifecycle_error

Vorschlag 3 SpringBeanAutowiringInterceptor

Verwenden Sie den Interceptor von JavaEE und den SpringBeanAutowiringInterceptor von Spring. Das folgende Beispiel zeigt die Verwendung der EJB-Klasse als Spring Management Bean. (Natürlich muss es nicht EJB sein. @Stateless und @Remote sind EJB-Anmerkungen, sodass Sie sie löschen können.)

EJB-Klasse


package com.autowired;

import com.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

@Stateless
@Remote
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class TestEjb {

    @Autowired
    private TestService testService;

    public void execute() {
        testService.execute();
    }
}

Auswertung

Recommended Posts

Rufen Sie Spring Management Bean von Pojo
Rufen Sie Chain from Chain in Spring Integration auf
Rufen Sie Java von JRuby aus auf
Lebenszyklus der Frühlingsbohnen
1. Starten Sie Spring Framework von 1
Versuchen Sie Spring Boot von 0 bis 100.
Frühlingsbohne Validaiton ~ Bohne Bari Deshon ~