Spring4.0學習筆記(9) —— 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> {

    @Autowired
    protected 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();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM