spring容器中bean的作用域bean的后处理器

容器bean中的作用域

applicationcontext.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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 注册Service,
    	scope="prototype" 原型模式,其对象的创建时机不是在Spring容器初始化时创建,而是在代码中真正访问时才创建
    	scope="singleton" 单例模式,其对象的创建时机是在Spring容器初始化时创建,是默认值
     -->
    <bean id="myService" class="com.bjpowernode.ba04.SomeServiceImpl" scope="singleton"/>

</beans>
接口:

public interface ISomeService {
	void doSome();
}
工厂类:
public class ServiceFactory {
	public static ISomeService getSomeService() {
		return new SomeServiceImpl();
	}
}




实现类:

public class SomeServiceImpl implements ISomeService {
	private int a;
	
	public SomeServiceImpl() {
		System.out.println("执行无参构造器");
	}
	
	/*
	public SomeServiceImpl(int a) {
		this.a = a;
	}
	*/
	
	@Override
	public void doSome() {
		System.out.println("执行doSome()方法");
	}


}
测试类

public class MyTest {
	
	@Test
	public void test01() {
		// 创建容器对象,加载Spring配置文件
		String resource = "com/bjpowernode/ba03/applicationContext.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
		ISomeService service = (ISomeService) ac.getBean("myService");
		service.doSome();
	}
	
}

后处理器:

applicationcontext.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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 注册Service -->
    <bean id="myService" class="com.bjpowernode.ba05.SomeServiceImpl"/>
    <bean id="myService2" class="com.bjpowernode.ba05.SomeServiceImpl"/>
    
    <!-- 注册Bean后处理器 -->
    <bean class="com.bjpowernode.ba05.MyBeanPostProcessor"/>

</beans>

处理器类:

通过处理改变输出字母的大小写

public class MyBeanPostProcessor implements BeanPostProcessor {

	// bean:表示当前正在进行初始化的Bean对象
	// beanName:表示当前正在进行初始化的Bean对象的id
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("执行 ----before---()方法");
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(final Object bean, String beanName)
			throws BeansException {
		System.out.println("执行 ----after---()方法");
		if ("myService".equals(beanName)) {
			Object obj = Proxy.newProxyInstance(bean.getClass()
					.getClassLoader(), bean.getClass().getInterfaces(),
					new InvocationHandler() {

						@Override
						public Object invoke(Object proxy, Method method,
								Object[] args) throws Throwable {
							Object invoke = method.invoke(bean, args);
							if ("doSome".equals(method.getName())) {
								return ((String) invoke).toUpperCase();
							}
							return invoke;
						}
					});
			return obj;
		}
		return bean;
	}

}

接口类

public interface ISomeService {
	String doSome();
	String doOther();
}
实现类

public class SomeServiceImpl implements ISomeService {
	
	@Override
	public String doSome() {
		System.out.println("执行doSome()方法");
		return "abcde";
	}
	
	@Override
	public String doOther() {
		System.out.println("执行doOther()方法");
		return "fghig";
	}


}

测试类:

public class MyTest {
	
	@Test
	public void test01() {
		// 创建容器对象,加载Spring配置文件
		String resource = "com/bjpowernode/ba05/applicationContext.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
		
		ISomeService service = (ISomeService) ac.getBean("myService");
		System.out.println(service.doSome());
		System.out.println(service.doOther());
		
		System.out.println("======================");
		
		ISomeService service2 = (ISomeService) ac.getBean("myService2");
		System.out.println(service2.doSome());
		System.out.println(service2.doOther());
	}
	
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值