您的位置:首页 > 编程语言 > Java开发

Spring笔记3-泛型依赖注入

2017-08-27 00:28 323 查看


springContext.xml

<beans>
<context:componemt-scan base-package="com.test"></context:componemt-scan>
</beans>


package com.test;
public class BaseRepository<T> {
}


package com.test;
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 com.test;
public class User {
}


package com.test;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository extends BaseRepository<User> {
}


package com.test;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService<User> {
}


public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("springContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.add();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: