Spring使用教程(三)泛型依赖注入

<?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-4.0.xsd">
    <context:component-scan base-package="com.test.spring.beans"></context:component-scan>

</beans>
View Code
package com.test.spring.beans;

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

public class BaseService<T> {
    @Autowired
    protected BaseRepository repository;
    public void add() {
        System.out.println("add....");
        System.out.println(repository);
    }
}
View Code
package com.test.spring.beans;

public class BaseRepository<T> {

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

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService<User>{

}
View Code
package com.test.spring.beans;

public class User {

}
View Code
package com.test.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext con=new ClassPathXmlApplicationContext("beans_fanxing.xml");
        
        UserService u=(UserService)con.getBean("userService");
        
        u.add(); 
    }
}
View Code

 

posted on 2015-06-16 22:37  kevin~kai  阅读(388)  评论(0)    收藏  举报