美文网首页
spring学习13( 泛型依赖注入)

spring学习13( 泛型依赖注入)

作者: 又是那一片天 | 来源:发表于2017-07-25 13:34 被阅读0次
  • 在父类配置的关系子类会继承
  • 会指向泛型对应的子类

父类

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>{

}

相关配置...

    /**
     * 泛型依赖注入
     *  - 在父类配置的关系子类会继承
     *  - 会指向泛型对应的子类
     * */
    public static void testScanGenericity() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("scanb.xml");
        UserService service=(UserService)ctx.getBean("userService");
        service.add();
    }

http://www.icoolxue.com/album/show/223

相关文章

网友评论

      本文标题:spring学习13( 泛型依赖注入)

      本文链接:https://www.haomeiwen.com/subject/onuwkxtx.html