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

  • 在父类配置的关系子类会继承
  • 会指向泛型对应的子类

父类

package note.genericity;

public class BaseRepository {

}
package note.genericity;

import org.springframework.beans.factory.annotation.Autowired;

public class BaseService {
    //在父类指定装配
    @Autowired
    protected BaseRepository 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 {

}

package note.genericity;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService{

}

相关配置...

    /**
     * 泛型依赖注入
     *  - 在父类配置的关系子类会继承
     *  - 会指向泛型对应的子类
     * */
    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( 泛型依赖注入))