spring容器中bean的作用域bean的后处理器
来源:互联网 发布:linux arp d 编辑:程序博客网 时间:2024/05/17 23:52
容器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;}*/@Overridepublic void doSome() {System.out.println("执行doSome()方法");}}测试类public class MyTest {@Testpublic 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@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {System.out.println("执行 ----before---()方法");return bean;}@Overridepublic 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() {@Overridepublic 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 {@Overridepublic String doSome() {System.out.println("执行doSome()方法");return "abcde";}@Overridepublic String doOther() {System.out.println("执行doOther()方法");return "fghig";}}
测试类:public class MyTest {@Testpublic 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
- spring容器中bean的作用域bean的后处理器
- Spring的bean后处理器和容器后处理器
- Spring的bean后处理器和容器后处理器
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring的配置依赖 协调不同步的Bean Bean的后处理器 容器的后处理器
- Spring容器Bean的作用域
- 四、Spring容器中bean的作用域
- 容器中 Bean 的作用域
- python3.6 pip重装
- 轻量级数据库——LiteOrm的最基本使用
- 用css3动画来实现物体上下跳动的效果
- 深入剖析 ThreadLocal
- 聊一聊Java中的几种读取properties配置文件的方式
- spring容器中bean的作用域bean的后处理器
- Vue检测变化问题
- Postgresql 归档失败
- QT QML初体验随笔之QQuickView(2)
- 总结C#获取当前路径的7种方法
- 3Sum
- 记录linux安装ssh
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解
- Mysql 备份shell