PostConstruct注解简单使用

本文介绍如何在Spring框架中实现一个接口的多种实现,并通过配置文件动态选择具体的实现类。通过PostConstruct注解初始化一个Map,存储不同实现类,实现灵活的服务调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个接口多个实现类

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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>com.zmq</groupId>
    <artifactId>database-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.build.timestamp.format>yyMMdd</maven.build.timestamp.format>
        <runtime.version>1.8</runtime.version>
        <autoconfig-plugin-version>1.2</autoconfig-plugin-version>
        <spring-version>4.2.1.RELEASE</spring-version>
        <spring-amqp-version>1.5.1.RELEASE</spring-amqp-version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring-version}</version>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!--oracle-->
        <dependency>
            <groupId>ojdbc</groupId>
            <artifactId>ojdbc</artifactId>
            <version>6</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</version>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
            <scope>provided</scope>
        </dependency>

        <!--日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.13</version>
        </dependency>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

接口

package mapper;

public interface IService {
    String getcode();
    
    String getString();
}

实现类1

package mapper;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.stereotype.Service;

@Service
@Getter
@Setter
@ToString
public class FirstService implements IService {
    
    private String string;
    
    @Override
    public String getcode() {
        return "First";
    }

    @Override
    public String getString() {
        return "First";
    }
}

实现类2

package mapper;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.stereotype.Service;

@Service
@Getter
@Setter
@ToString
public class SecondService implements IService{

    private String string;

    @Override
    public String getcode() {
        return "Second";
    }

    @Override
    public String getString() {
        return "Second";
    }
}

获取不同实现类

package mapper;

import lombok.ToString;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service("serviceMap")
@ToString
public class ServiceMap {
    @Resource
    private IService[] services;
    @Resource
    private Map<String, IService> maps = new HashMap<>();

    @PostConstruct
    public void init() {
        for (IService iiService : services) {

            maps.put(iiService.getcode(), iiService);
        }


    }

    public IService getIService(String code) {
        IService iService = maps.get(code);
        return iService;
    }
}

配置文件

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

">


	<context:component-scan base-package="mapper"/>


</beans>

测试基类

import mapper.ServiceMap;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public abstract class AbcTest {
    protected ServiceMap serviceMap;
    private ApplicationContext ctx = null;
    
    @Before
    public void init() {
        /*PropertyConfigurator.configure(ResolverUtil.Test.class.getClassLoader().getResource("log4j.properties"));
        BasicConfigurator.configure();*/
        ctx = new ClassPathXmlApplicationContext("classpath:/dao-test.xml");
        serviceMap = (ServiceMap)ctx.getBean("serviceMap");
    }

    @Test
    public abstract void testMethods();
    
}

测试类



import mapper.IService;



public class MapperTest extends AbcTest {




	private void select() {
		IService first = serviceMap.getIService("First");
		String string = first.getString();
		System.out.println(string);
		IService second = serviceMap.getIService("Second");
		String string2 = second.getString();
		System.out.println(string2);
    }



	@Override
	public void testMethods() {
        select();
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值