package generic_di;
import org.springframework.stereotype.Repository;
@Repository
public class BaseRepository<T> {
public void save() {
System.out.println("Repository save ...");
}
}
package generic_di;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository extends BaseRepository<User> {
}
package generic_di;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
@Service
public class BaseService<T> {
@Autowired
protected BaseRepository<T> baseRepository;
public void add() {
System.out.println("Service add ...");
System.out.println(baseRepository);
}
}
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//application.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.add();
}
}