IOC容器中bean的生命周期方法和通过工厂方法配置bean

// 基于xml文件的方式加载配置

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第1张图片IOC容器中bean的生命周期方法和通过工厂方法配置bean_第2张图片

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第3张图片

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第4张图片

public class MyBeanPostProcesser implements BeanPostProcessor{

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessAfterInitialization   " + beanName);
		Car car = new Car();
		car.setBrand("booma");
		return car;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("postProcessBeforeInitialization   " + beanName);
		return bean;
	}

	
}

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第5张图片IOC容器中bean的生命周期方法和通过工厂方法配置bean_第6张图片

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第7张图片

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第8张图片

通过FactoryBean配置Bean的实例 

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第9张图片

实现FactoryBean接口 

public class CarFactoryBean implements org.springframework.beans.factory.FactoryBean{

	private String brand;
	
	public void setBrand(String brand) {
		this.brand=brand;
	}
	//获得bean本身
	@Override
	public Car getObject() throws Exception {
		return new Car(brand,500000);
	}
	//获得bean的class type
	@Override
	public Class getObjectType() {
		return null;
	}
	//返回是否是单例
	@Override
	public boolean isSingleton() {
		return true;
	}
}

//基于注解的方式配置

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第10张图片IOC容器中bean的生命周期方法和通过工厂方法配置bean_第11张图片

IOC容器中bean的生命周期方法和通过工厂方法配置bean_第12张图片

 

package cn.zzuli.spring.beans.annotation;

import org.springframework.stereotype.Component;

@Component
public class TestObject {

}
package cn.zzuli.spring.beans.annotation.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {

	public void execute(){
		System.out.println("UserController execute....");
	}
}
package cn.zzuli.spring.beans.annotation.repository;

public interface UserRepository {
	void save();
}
package cn.zzuli.spring.beans.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{

	@Override
	public void save() {
		System.out.println("UserRepositoryImpl save....");
	}
}
package cn.zzuli.spring.beans.annotation.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

	public void add() {
		System.out.println("UserService add...");
	}
}
package cn.zzuli.spring.beans.annotation;

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

import cn.zzuli.spring.beans.annotation.controller.UserController;
import cn.zzuli.spring.beans.annotation.repository.UserRepository;
import cn.zzuli.spring.beans.annotation.service.UserService;

public class Main {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans_annotation.xml");
		
		TestObject to = (TestObject) ctx.getBean("testObject");
		System.out.println(to);
		
		UserController uc = (UserController)ctx.getBean("userController");
		System.out.println(uc);
		
		UserService userService = (UserService)ctx.getBean("userService");
		System.out.println(userService);
		
		UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
		System.out.println(userRepository);
		
		
	}

}


	
	
	
	
	
	
	
		
		 
		 
		 
	

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java)