码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-06-17 00:26:42      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
<?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

 

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

标签:

原文地址:http://www.cnblogs.com/img-zoom/p/4582013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!