Spring 4.x 新特性:泛型依赖注入

 Spring 允许通过 将多个配置文件引入到一个文件中,进行配置文件的集成。这样在启动 Spring 容器时,仅需要指定这个合并好的配置文件就可以。

import 元素的 resource属性支持 Spring 的标准的路径资源

Main

package com.spring.beans.generic.di;

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

public class Main {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans-generic-di.xml");
		UserService ur = (UserService) ac.getBean("userService");
		ur.add();
	}

}
/*
add
com.spring.beans.generic.di.UserRepository@57f23557
*/

beans-generic-di.xml



	
	
	
	

BaseService

package com.spring.beans.generic.di;

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

public class BaseService {
	@Autowired
	 private BaseRepository repository;
	 public void add() {
		 System.out.println("add");
		 System.out.println(repository);
	 }
}

UserService

package com.spring.beans.generic.di;

import org.springframework.stereotype.Service;

@Service
public class UserService extends BaseService{
	
}

BaseRepository

package com.spring.beans.generic.di;

public class BaseRepository {

}
UserRepository
package com.spring.beans.generic.di;

import org.springframework.stereotype.Repository;

@Repository
public class UserRepository extends BaseRepository{
	
}

User

package com.spring.beans.generic.di;

public class User {

}


你可能感兴趣的:(Spring)