當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring泛型依赖注入
生活随笔
收集整理的這篇文章主要介紹了
Spring泛型依赖注入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、定義基礎倉庫
package com.spring.generic.di;public class BaseRepository<T> {}2、定義基礎服務層
package com.spring.generic.di;import org.springframework.beans.factory.annotation.Autowired;public class BaseService<T> {@Autowiredprotected BaseRepository<T> repository;public void add(){System.out.println("add...");System.out.println(repository);} }3、定義User服務層
package com.spring.generic.di;import org.springframework.stereotype.Service;@Service public class UserService extends BaseService<User>{}4、定義倉庫服務層
package com.spring.generic.di;import org.springframework.stereotype.Repository;@Repository public class UserRepository extends BaseRepository<User>{}5、測試類
package com.spring.generic.di;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-generic.xml");UserService userService = (UserService)ctx.getBean("userService");userService.add();} }總結
以上是生活随笔為你收集整理的Spring泛型依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 买健康险需要体检吗
- 下一篇: 花呗还款使用花呗金是什么意思