发表于: 2017-12-14 11:19:48
1 661
今天完成的事
【dubbo完美配置成功】
看似一张很简单的图,涉及的知识点相当的多。
明天再总结。总结不完就不总结了。
收获
User类调用一个service, 这个service又调用一个tool。
有时我们希望User是多例的,service是单例的,而tool又是多例的。
<bean id="user" class="modle.User" scope="prototype">
<property name="service" ref="userservice"></property>
</bean>
<bean id="userservice" class="service.userService" >
<property name="tool" ref="tool"></property>
</bean>
<bean id="tool" class="service.ToolImpl" scope="prototype">
</bean>
However, suppose you want the singleton-scoped bean to acquire a new instance of the prototype-scoped bean repeatedly at runtime. You cannot dependency-inject a prototype-scoped bean into your singleton bean, because that injection occurs only once, when the Spring container is instantiating the singleton bean and resolving and injecting its dependencies. If you need a new instance of a prototype bean at runtime more than once, see Section 4.4.6, “Method injection”
由于不使用spring的自动注入,set方法要去掉!
public class userService implements BeanFactoryAware {
private Tool tool;
private BeanFactory factory;
public void service(){
this.tool = (Tool)factory.getBean("tool");
System.out.println(this+":service");
tool.work();
}
public Tool getTool() {
return tool;
}
// public void setTool(Tool tool) {//
// this.tool = (Tool)factory.getBean("tool");// }
public void setBeanFactory(BeanFactory f) throws BeansException {
factory = f;
}
}
配置文件,不能再使用注入。因此要把tool对象的注入去掉!
<bean id="user" class="modle.User" scope="prototype">
<property name="service" ref="userservice"></property>
</bean>
<bean id="userservice" class="service.userService" >
</bean>
<bean id="tool" class="service.ToolImpl" scope="prototype">
</bean>
public interface Tool {
public void work();
}
public class ToolImpl implements Tool{
public void work() {
System.out.println(this+":Tool Work");
}
}
测试类:
public class Test {
public static void main(String[] args) {
ClassPathResource res = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
User user = (User)factory.getBean("user");
User user2 = (User)factory.getBean("user");
System.out.println(user);
user.getService().service();
System.out.println();
System.out.println(user2);
user2.getService().service();
}
}
控制台输出
modle.User@42552cservice.userService@19e15c:serviceservice.ToolImpl@11a75a2:Tool Work
service.userService@19e15c:service
service.ToolImpl@170888e:Tool Work
遇到的问题
第一个bug
解决方案:使用Dubbo进行数据传递时,需让作为消息传递的类序列化。
第二个bug
百度文档http://blog.csdn.net/shutingwang/article/details/6594703
问题:
1.在Setter注入方式中,在beans包下的ProductBean中只加入一个带参
数的构造方法会如何?
答:将会产生异常Spring异常与JVM异常
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'productBean' defined in file
[/home/tarena/corejava/JSP/SpringPrj/./src/ioc/common/config/config.xm
l]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class
[ioc.common.beans.ProductBean]:
No default constructor found; nested exception is
java.lang.NoSuchMethodException:
ioc.common.beans.ProductBean.<init>()
//JVM异常
Caused by: java.lang.NoSuchMethodException:
ioc.common.beans.ProductBean.<init>()
Tips:强烈建议有不带参数的构造器
至于为什么
javabean相当于一个类,在它不定义任何构造方法的时候,系统提供了一个默认的无参数的构造方法。为何自己还要定义一个无参数的构造方法?
我的理解是。
Bean实例在被Spring容器初始化的时候,就会被实例化,默认调用无参数的构造方法。(高亮注意,貌似和单例模式有关?)
当你创建含有参数的构造器时,系统不会生成默认的构造器,所以需要自己创建。
我解释的够清楚了吧。。。
明天的计划
任务8
禅道链接
评论