参考代码下载github:https://github.com/changwensir/java-ee/tree/master/spring4
Bean 的配置方式:通过全类名(反射)、通过工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean
1-1.通过静态工厂方式配置Bean
/**
* InstanceCarFactory:实例工厂方法
* 实例工厂的方法,即需要先创建工厂本身,再调用工厂的实例方法来返回bean的实例
*
* @author Lcw 2015/11/12
*/
public class InstanceCarFactory {
private Map cars = null;
public InstanceCarFactory() {
cars = new HashMap();
cars.put("audi", new Car("audi", 500000));
cars.put("ford", new Car("ford", 600000));
}
public Car getCar(String brand) {
return cars.get(brand);
}
}
/**
* StaticCarFactory:静态工厂方法:直接调用某一个类的静态方法就可以返回Bean实例
*/
public class StaticCarFactory {
private static Map cars = new HashMap();
static {
cars.put("audi",new Car("audi", 30000));
cars.put("ford",new Car("ford", 40000));
}
//静态工厂方法: 注意:要在配置文件XML里配置Car而不是StaticCarFactory
public static Car getCar(String name) {
return cars.get(name);
}
}
/**
* Bean 的配置方式:-、通过全类名(反射);二、通过工厂方法(静态工厂方法 & 实例工厂方法);三、FactoryBean
*
* 静态工厂方法:直接调用某一个类的静态方法就可以返回Bean实例
* 实例工厂方法:实例工厂的方法,即需要先创建工厂本身,再调用工厂的实例方法来返回bean的实例
*/
@Test
public void testStaticFactory() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring4_IOC/beans-factory.xml");
//测试静态工厂方法
Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1);
//实例工厂方法
Car car2 = (Car) ctx.getBean("car2");
System.out.println(car2);
}
1-3.实现 FactoryBean 接口在 Spring IOC 容器中配置 Bean
/**
* CarFactoryBean:自定义的factoryBean需要实现FactoryBean
*/
public class CarFactoryBean implements FactoryBean {
private String brand;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public CarFactoryBean() {
}
//下面是三个实现方法
//返回bean的对象
public Car getObject() throws Exception {
return new Car("BMW" , 500000);
}
//返回bean的类型Class> 中?表示一个未知的类
public Class> getObjectType() {
return Car.class;
}
public boolean isSingleton() {
return true;
}
}
/**
* Bean 的配置方式:-、通过全类名(反射);二、通过工厂方法(静态工厂方法 & 实例工厂方法);三、FactoryBean
*
* 测试FactoryBean
* Spring 中有两种类型的 Bean, 一种是普通Bean, 另一种是工厂Bean, 即FactoryBean
*/
@Test
public void testFactoryBean() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring4_IOC/beans-beanFactory.xml");
Car car = (Car) ctx.getBean("car"); //注意这个在类是Car!
System.out.println(car);
}
以下 类那放在这个文件下Spring4_IOC.annotation
@Component
public class TestObject {
}
respository接口跟实现类
public interface UserRepository {
void save();
}
//接口实现一般用接口名
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
public void save() {
System.out.println("UserRepository Save...");
}
}
Service层跟controller
@Service
public class UserService {
//@Autowired(required=false) //如果没有这个类,输出为null
@Autowired
private UserRepository userRepository;
public void add() {
System.out.println("UserService add...");
userRepository.save();
}
}
@Controller
public class UserController {
@Autowired
private UserService userService;
public void execute() {
System.out.println("UserController execute...");
userService.add();
}
}
@Test
public void testAnnotation() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring4_IOC/beans-annotation.xml");
TestObject to = (TestObject) ctx.getBean("testObject");
System.out.println(to);
//类UserController第一个字母小写 userController
UserController userController = (UserController) ctx.getBean("userController");
System.out.println(userController);
userController.execute();
UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService);
UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
System.out.println(userRepository);
}
<
context:include-filter
>
和
<
context:exclude-filter
>
子
节点支持多种类型的过滤表达式
public class BaseRepository {
}
public class BaseService {
@Autowired
protected BaseRepository repository;
public void add() {
System.out.println("add...");
System.out.println(repository);
}
}
@Service
public class UserService extends BaseService {
}
public class User {
private String userName;
private List cars;
private String wifeName;
public String getWifeName() {
return wifeName;
}
public void setWifeName(String wifeName) {
System.out.println("setWifhName: " + wifeName);
this.wifeName = wifeName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public List getCars() {
return cars;
}
public void setCars(List cars) {
this.cars = cars;
}
public User() {
System.out.println("User's Construtor...");
}
@Override
public String toString() {
return "User [userName=" + userName + ", cars=" + cars + "]";
}
public void init(){
System.out.println("init method...");
}
public void destroy(){
System.out.println("destroy method...");
}
}
@Repository
public class UserRepository extends BaseRepository{
}
配置文件
/**
* Spring 4.x 新特性:泛型依赖注入
*/
@Test
public void testDI() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring4_IOC/beans-generic.xml");
Spring4_IOC.di.UserService userService = (Spring4_IOC.di.UserService) ctx.getBean("userService");
System.out.println(userService);
userService.add();
}
Spring4_IOC.di.UserService@225a8897 add... Spring4_IOC.di.UserRepository@65bd831f |