聊聊spring繼承的問題
spring繼承的問題


為什么輸出是0呢?
因?yàn)槭亲宇惱^承父類,實(shí)例對象調(diào)用的主要是左邊的父類屬性和方法,所以輸出結(jié)果是以左邊對象為主
spring注入有繼承關(guān)系的類
通過配置文件
<bean id="sysActionService" class="com.service.impy.SysActionServiceImpy" parent="baseService" >
<property name="sysActionDao" ref="sysActionDao" />
</bean>
通過注解
只需要在子類上加注解,父類上不用加會自動注入
package com.jeremy.spring.genericityDI;
public class BaseRepository{
}
BaseService:
package com.jeremy.spring.genericityDI;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseService<T> {
@Autowired------//這里告訴IOC容器自動裝配有依賴關(guān)系的Bean
protected BaseRepository baseRepository;--------//這里是子類繼承依賴關(guān)系
public void add(){
System.out.println("add..............");
System.out.println(baseRepository);
}
}
新建一個泛型類
User:
package com.jeremy.spring.genericityDI;
public class User {
}
新建BaseRepository和BaseService的子類
UserRepository:
package com.jeremy.spring.genericityDI;
import org.springframework.stereotype.Component;
@Component
public class UserRepository extends BaseRepository{
}
UserService:
package com.jeremy.spring.genericityDI;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService{
}
在Spring的配置文件中配置自動裝配帶有注解的Bean
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.jeremy.spring.genericityDI"></context:component-scan>
</beans>
測試代碼和結(jié)果
測試代碼:
@Test
public void test() {
ApplicationContext actx=new ClassPathXmlApplicationContext("Bean-genericity-di.xml");
UserService userService=(UserService) actx.getBean("userService");
userService.add();
}
測試結(jié)果:
add..............
com.jeremy.spring.genericityDI.UserRepository@16546ef
從結(jié)果看,雖然子類沒有建立依賴關(guān)系,但userRepository實(shí)例還是被實(shí)例化了,就證明了父類的依賴關(guān)系,子類是可以繼承的
其實(shí)這里也可以說明,就算父類不是被IOC容器管理,但是建立關(guān)系時添加了@Autowired注解,父類的關(guān)系會被繼承下來
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何將二進(jìn)制文件流轉(zhuǎn)化為MockMultipartFile文件
文章主要介紹了如何使用Spring框架中的MockMultipartFile類來模擬文件上傳,并處理上傳邏輯,包括獲取二進(jìn)制文件流、創(chuàng)建MockMultipartFile對象、處理上傳邏輯和返回響應(yīng),還解釋了MockMultipartFile的功能和二進(jìn)制文件流的定義2025-02-02
java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印
這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java中StringUtils與CollectionUtils和ObjectUtil概念講解
這篇文章主要介紹了Java中StringUtils與CollectionUtils和ObjectUtil概念,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
springboot如何將http轉(zhuǎn)https
這篇文章主要介紹了springboot如何將http轉(zhuǎn)https,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
Java實(shí)現(xiàn)文件和base64流的相互轉(zhuǎn)換功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)文件和base64流的相互轉(zhuǎn)換功能,涉及Java文件讀取及base64 轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2018-05-05

