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

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

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;
	}

	
}

通过FactoryBean配置Bean的实例 

实现FactoryBean接口 

public class CarFactoryBean implements org.springframework.beans.factory.FactoryBean<Car>{

	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;
	}
}

//基于注解的方式配置

 

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);
		
		
	}

}
<?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.3.xsd">
	<!-- 指定IOC要扫描的包 -->
	<!-- 可以通过resource-pattern指定扫描的资源 -->
	<!-- 
	<context:component-scan
		base-package="cn.zzuli.spring.beans.annotation"
		resource-pattern="repository/*.class">
	</context:component-scan>
	 -->
	<!-- context:exclude-filter子节点指定排除哪些指定表达式的组件,annotation目标类是否标注了某个注解进行过滤 -->
	<!-- context:exclude-filter子节点指定包含哪些表达式的组件组件 ,需要和 use-default-filters="false配合使用 -->
	<!-- assignable目标类是否继承或扩展了某个类进行过滤 -->
	<context:component-scan
		base-package="cn.zzuli.spring.beans.annotation"
		use-default-filters="false">
		<!-- 
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
		 -->
		 <!-- 
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
		 -->
		 <!-- 
		<context:exclude-filter type="assignable" 
			expression="cn.zzuli.spring.beans.annotation.repository.UserRepository"/>
		 -->
		 <context:include-filter type="assignable"
		 	expression="cn.zzuli.spring.beans.annotation.repository.UserRepository"/>
	</context:component-scan>
</beans>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值