Spring4.0开始的泛型依赖

参考资料:

http://www.cnblogs.com/solverpeng/p/5687304.html

 

视频地址:https://edu.51cto.com/course/1956.html

 

一、为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入。

二、泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之间的依赖关系来确定,父类的泛型必须为同一类型。

通俗一点来说:两个子类之间的依赖关系不需要在子类中去声明,而是在父类中进行了声明,而依赖的纽带就是 泛型类型,必须是相同的父类泛型类型才具有依赖关系。

三、UML 类图:

说明:在 BaseService 中通过 @Autowired 注解自动装配了 BaseDao 的实例。而在 UserService 中并没有注入 UserDao 的实例,但是通过父类的泛型类型,

在 UserService 中已经注入了 UserDao 的实例。Department 与之类似。

 

4:maven项目的例子如下:

pom.xml文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yuanqiao</groupId>
    <artifactId>JavaWeb</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>JavaWeb Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.6.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.26</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>JavaWeb</finalName>
    </build>
</project>

 

spirng的配置文件annotation-beans.xml的内容如下:

<?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"
    xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
    xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
        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">


    <!-- 启动spring的注解模式 -->
    <context:annotation-config />

    <context:component-scan base-package="com.yuanqiao.spring.beans" />




</beans>

 

package com.yuanqiao.spring.beans;

public class BaseRepository<T> {
	public void add(){
		System.out.println("如果子类没有重写这个方法,那么会调用父类的该方法,BaseRepository add method...");
	}
}

  

package com.yuanqiao.spring.beans;

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

public class BaseService<T> {

	@Autowired
	protected BaseRepository<T> baseRepository;

	public void add() {
		System.out.println("打印一下当前baseRepository的类型,看是父类还是子类:"+baseRepository);
		baseRepository.add();
	}
}

  

package com.yuanqiao.spring.beans;

public class User {

}

  

package com.yuanqiao.spring.beans;

import org.springframework.stereotype.Component;

@Component
public class UserRepository extends BaseRepository<User> {
	public void add() {
		System.out.println("现在调用的是子类的add方法,而不是父类的方法,UserRepository add...");
	}
}

  

package com.yuanqiao.spring.beans;

import org.springframework.stereotype.Component;

@Component
public class UserService extends BaseService<User> {

}

  

最后测试的main类如下:

package com.yuanqiao.spring.beans;

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


/**
 * 泛型依赖注入,可以大大节省开发量
 * 
 * 视频地址:https://edu.51cto.com/course/1956.html
 * 
 * 
 * 
 * @author yuanqiao
 *
 */
public class Main {
	public static void main(String[] args) {
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("annotation-beans.xml");
		
		UserService userService = (UserService)applicationContext.getBean("userService");
		
		userService.add();
		
		UserRepository userRepository = (UserRepository)applicationContext.getBean("userRepository");
		
		userRepository.add();

	}
}

  

输出结果如下:

打印一下当前baseRepository的类型,看是父类还是子类:com.yuanqiao.spring.beans.UserRepository@693fe6c9
现在调用的是子类的add方法,UserRepository add...
现在调用的是子类的add方法,UserRepository add...

 

posted on 2019-04-27 21:13  坚守梦想  阅读(598)  评论(0编辑  收藏  举报