java spring中抽象类无法注入

 场景

springboot通过单例模式获取抽象类下的实现类。

问题

通过spring @Autowired注入service,获取不到对象。

问题引入

首先明确一个问题:抽象类不能生成实例对象,spring无法注入。

原因:spring的原理是启动服务器时读取配置文件,取得类名后利用反射机制在spring上下文中生成一个单例的对象,由spring注入属性并维护此对象的状态,抽象类在反射生成对象时就已经失败了,后面的不会进行

如何解决

方案1

限于springboot方式启动,
我们编写一个SpringBeanLoader的类,在应用启动时加载org.springframework.context.ApplicationContext,对外通过ApplicationContext提供获取bean的方法,代码如下:

应用启动时设置applicationContext

public class Application {

    public static void main(String[] args) {

        ApplicationContext applicationContext = SpringApplication.run(Application.class, args);

        SpringBeanLoader.setApplicationContext(applicationContext);
    }
}

SpringBeanLoader结构

public class SpringBeanLoader {

    private static ApplicationContext applicationContext;

    /**
     * 获取SpringApplicationContext
     *
     * @return ApplicationContext
     */

    private static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 设置SpringApplicationContext
     *
     * @param applicationContext
     */
    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringBeanLoader.applicationContext = applicationContext;
    }

    /**
     * 获取Spring中注册的Bean
     *
     * @param beanClass
     * @param beanId
     * @return
     */
    public static <T> T getSpringBean(String beanId, Class<T> beanClass) {
        return getApplicationContext().getBean(beanId, beanClass);
    }

    /**
     * 获取Spring中注册的Bean
     *
     * @param beanClass
     * @return
     */
    public static <T> T getSpringBean(Class<T> beanClass) {
        return getApplicationContext().getBean(beanClass);
    }
}

下面测试一下
定义一个HelloService和HelloServiceImpl

public interface HelloService {
    String echo(String str);
}
@Component
public class HelloServiceImpl implements HelloService {
    @Override
    public String echo(String str) {
        return str;
    }
}

抽象类

public abstract class AbstractService {

    public String testSpringIoc() {

        HelloService helloService = SpringBeanLoader.getSpringBean(HelloService.class);
        return helloService.echo("test");
    }
}

调用类

public class TestService extends AbstractService{

    public String test(){
        return testSpringIoc();
    }

}

编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
public class AbstractServiceTest {

    @Before
    public void init() {
        ApplicationContext applicationContext = new SpringApplicationBuilder(Application.class).web(true).run();
        SpringBeanLoader.setApplicationContext(applicationContext);
    }

    @Test
    public void test_Ioc() {
        TestService testService = new TestService();
        System.out.println(testService.testSpringIoc());
    }
}

输出结果:

test

 

方案2

通过构造函数将org.springframework.context.ApplicationContext注入到抽象类中,编辑一个ApplicationContext的持有类:ApplicationContextHolder

@Component
public class ApplicationContextHolder implements ApplicationContextAware {

    private ApplicationContext applicationContext;

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

    /**
     * 获取Spring中注册的Bean
     *
     * @param beanClass
     * @param beanId
     * @return
     */
    public <T> T getSpringBean(String beanId, Class<T> beanClass) {
        return applicationContext.getBean(beanId, beanClass);
    }

    /**
     * 获取Spring中注册的Bean
     *
     * @param beanClass
     * @return
     */
    public <T> T getSpringBean(Class<T> beanClass) {
        return applicationContext.getBean(beanClass);
    }
}

测试:

抽象类:

public abstract class AbstractService2 {

    private final ApplicationContextHolder applicationContextHolder;

    public AbstractService2(ApplicationContextHolder applicationContextHolder) {
        this.applicationContextHolder = applicationContextHolder;
    }

    public String testSpringIoc() {

        HelloService helloService = applicationContextHolder.getSpringBean(HelloService.class);
        return helloService.echo("test");
    }
}

调用类:

public class TestService2 extends AbstractService2{

    public TestService2(ApplicationContextHolder applicationContextHolder) {
        super(applicationContextHolder);
    }

    public String test(){
        return testSpringIoc();
    }
}

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestService2Test {

    @Resource
    private ApplicationContextHolder applicationContextHolder;

    @Test
    public void test_ioc(){
        TestService2 testService2 = new TestService2(applicationContextHolder);
        System.out.println(testService2.test());
    }

}

输出:

test

 

总结

  • 方案1仅适用于引入springboot的项目,方式比较简单。
  • 方案2使用与springboot项目或通过其他容器加载的项目,方式相比方案1来说复杂一下,需要提供调用方的构造器。
  • 笔者推荐在springboot项目下选择方案1.

原文地址

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抽象类注入`JdbcTemplate`时,需要注意一些细节。如果您在抽象类注入`JdbcTemplate`,并且在子类使用它,可能会出现`NullPointerException`异常。 这是因为在注入`JdbcTemplate`时,Spring并不会知道应该将哪个`JdbcTemplate`注入到哪个子类。因此,您需要通过在子类使用`@Qualifier`注解来指定要注入的`JdbcTemplate`。 以下是一个示例代码,展示了如何在抽象类注入`JdbcTemplate`并在子类使用它: ```java @Component public abstract class AbstractRepository { @Autowired @Qualifier("jdbcTemplate") private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } } ``` 在上面的代码,我们在抽象类`AbstractRepository`注入了`JdbcTemplate`,并在`getJdbcTemplate()`方法返回它。请注意,我们使用了`@Qualifier`注解来指定要注入的`JdbcTemplate`。 现在,让我们看看一个子类`UserRepository`的例子,它继承了`AbstractRepository`,并使用了`getJdbcTemplate()`方法来执行SQL查询: ```java @Repository public class UserRepository extends AbstractRepository { public List<User> findAll() { String sql = "SELECT * FROM users"; return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<>(User.class)); } } ``` 在子类,我们可以调用`getJdbcTemplate()`方法来获取父类注入的`JdbcTemplate`,并使用它来执行SQL查询。 这样,我们就可以在抽象类注入`JdbcTemplate`并在子类使用它,同时避免了`NullPointerException`异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值