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();
}
}