15Spring泛型依赖注入

Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用

BaseService<T>:有RoleService和UserService两的子类

BaseRepepositry<T>:有UserRepository和RoleRepositry两个子类

由于 BaseService<T>和 BaseRepepositry<T> 有关系所以,得出下面的子类也存在这样的关系

package generic.di; 

public class BaseRepository<T> { } 

————————————————————————————————————————————————————————————————————————————————————————————————————————————————
package 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);
    }
}
package generic.di;

import org.springframework.stereotype.Repository;

@Repository
public class RoleRepository extends BaseRepository<Organization> {
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————

package generic.di;
import org.springframework.stereotype.Service;

@Service
public class RoleService extends BaseService<Organization> {
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————
package generic.di;

public class Organization {
}
package generic.di;

public class User {
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————
package generic.di;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository extends BaseRepository<User> {

}

——————————————————————————————————————————————————————————————————————————————————————————————————————————————
package generic.di;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User> {
}
<?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.xsd">
       <context:component-scan base-package="generic.di">
       </context:component-scan>
</beans>
package 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("generic/di/15-1.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        RoleService roleService = (RoleService) ctx.getBean("roleService");
        userService.add();
        roleService.add();
    }
}

时间: 2024-10-14 23:43:46

15Spring泛型依赖注入的相关文章

Spring初学之泛型依赖注入

主要讲泛型依赖注入,所以核心在java文件,配置文件中只需配置扫描包即可,如下: <?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

Spring的泛型依赖注入

Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用,(这样子类和子类对应的泛型类自动建立关系)具体说明: 泛型注入:就是Bean1和Bean2注入了泛型,并且Bean1和Bean2建立依赖关系,这样子类Bean3(继承bean1)和bean4(继承bean2)就会自动建立关系 不是泛型注入:就是说Bean1和Bean2都没有注入泛型,只是建立了关系,子类Bean3(继承bean1)和bean4(继承bean2)也会自动建立关系 泛型依赖注入的实现步骤: 1新建两个泛型类,并

Spring -- 4.0新特性 -- 泛型依赖注入

泛型依赖注入为spring4.0版本新增加的特性. 目录结构 BaseService.java类 public class BaseService<T> { @Autowired private BaseRespository baseRespository; public void save() { System.out.println("Base Class save method"); System.out.println(baseRespository); } }

【串线篇】spring泛型依赖注入原理

spring泛型依赖注入原理 不管三七二十一 servlet :加注解@servlet service:加注解@service dao:加注解@Repository 这相当于在容器中注册这些个类 原文地址:https://www.cnblogs.com/yanl55555/p/11744077.html

Spring之泛型依赖注入

Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用 项目结构: BaseRepository.java public class BaseRepository<T> { } BaseService.java public class BaseService<T>{ @Autowired protected BaseRepository<T> repository; public void add(){ System.out.println(&quo

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 BaseRepositor

Spring_泛型依赖注入

Spring使用教程(三)泛型依赖注入

<?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/sch

泛型依赖注入

BaseService<T> 类中有一个BaseRepository<T>的成员变量,并且利用@Autowired实现了自动装配. UserService 和 UserRepository分别是 BaseService<T> 和 BaseRepository<T>的具体类型的子类. 当UserService 和 UserRepository分别用@Service 和 @Repository装配以后,Spring 可以让这两个子类之间自动实现关联.具体实现参考