spring容器中bean的作用域bean的后处理器
2017-05-15 14:49
295 查看
容器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 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());
}
}
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());
}
}
相关文章推荐
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
- Spring容器Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring系列:第三篇-Spring容器三种实例化bean的方式以及Bean的作用域
- Spring容器中Bean的作用域
- 【Spring学习23】容器扩展点:后置处理器BeanPostProcessor
- spring中bean的作用域与懒加载及初始化方法和销毁方法以及spring容器的正常关闭
- Spring容器中Bean的作用域
- spring中bean的作用域与懒加载及初始化方法和销毁方法以及spring容器的正常关闭
- BeanFactory作为Spring容器,程序需要手动获取Bean后处理器,然后手动注册!
- 【Spring学习24】容器扩展点:后置处理器BeanFactoryPostProcessor
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- Spring容器中Bean的作用域
- 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;
- Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)
- Spring的配置依赖 协调不同步的Bean Bean的后处理器 容器的后处理器
- 7.5.2: Spring容器中的Bean---容器中Bean的作用域
- Spring容器中Bean的作用域之singleton和prototype