- 在父類配置的關(guān)系子類會繼承
- 會指向泛型對應(yīng)的子類
父類
package note.genericity;
public class BaseRepository<T> {
}
package note.genericity;
import org.springframework.beans.factory.annotation.Autowired;
public class BaseService<T> {
//在父類指定裝配
@Autowired
protected BaseRepository<T> repository;
public void add() {
System.out.println("Service Add");
System.out.println(repository);
}
}
pojo:
package note.genericity;
public class User {
}
子類:
package note.genericity;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository extends BaseRepository<User> {
}
package note.genericity;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService<User>{
}
相關(guān)配置...
/**
* 泛型依賴注入
* - 在父類配置的關(guān)系子類會繼承
* - 會指向泛型對應(yīng)的子類
* */
public static void testScanGenericity() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("scanb.xml");
UserService service=(UserService)ctx.getBean("userService");
service.add();
}