我正在尝试掌握Spring的FactoryBean,并且遇到了问题.您能否在下面查看我的资料来源并回答.这是我的应用上下文:
I'm trying to grasp Spring's FactoryBean and I've had and issue. Could you please see my sources below and answer. It's my app context:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:context="www.springframework/schema/context" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.1.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.1.xsd"> <context:annotation-config/> <bean id="SHADigest" class="com.dtoryanik.spring.factorybean.MessageDigestFactoryBean"> <property name="algorithmName"> <value>SHA1</value> </property> </bean> <bean id="defaultDigest" class="com.dtoryanik.spring.factorybean.MessageDigestFactoryBean"/> <bean id="digester" class="com.dtoryanik.spring.factorybean.MessageDigester"> <property name="messageDigest1"> <ref local="SHADigest"/> </property> <property name="messageDigest2"> <ref local="defaultDigest"/> </property> </bean> </beans>实际上是工厂bean:
It's a factory bean actually:
public class MessageDigestFactoryBean implements FactoryBean<MessageDigest>{ private String algorithmName = "MD5"; private MessageDigest messageDigest = null; @Override public MessageDigest getObject() throws Exception { System.out.println("<> MessageDigestFactoryBean.getObject()"); return messageDigest; } @Override public Class<?> getObjectType() { System.out.println("<> MessageDigestFactoryBean.getObjectType()"); return MessageDigest.class; } @Override public boolean isSingleton() { System.out.println("<> MessageDigestFactoryBean.isSingleton()"); return true; } @PostConstruct public void postConstructHandler() throws NoSuchAlgorithmException { System.out.println("<> MessageDigestFactoryBean.postConstructHandler()"); messageDigest = MessageDigest.getInstance(algorithmName); } public void setAlgorithmName(String algorithmName) { this.algorithmName = algorithmName; } }还有另一个类-MessageDigester,但是它对主题没有任何帮助.我有一个主方法类:
There is another class - MessageDigester but it does not do anything helpful for topic. And I have a main-method class:
public class MessageDigestDemo { public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:app-ctx.xml"); ctx.refresh(); MessageDigester messageDigester = (MessageDigester) ctx.getBean("digester"); messageDigester.digest("Hello World!"); }}
问题出在我的输出中.看来我有双重实例化.每个bean都会调用两次isSingleton(),getObject()方法(尽管我从工厂只检索了2个实例).为什么会发生?也许我做错了什么?
The issue is in my output. It seems I have a double instantiating. Methods isSingleton(), getObject() are called two times for each bean (although I retrieve only 2 instances from factory). Why does it occur? Maybe I do something wrong?
<> MessageDigestFactoryBean.postConstructHandler() <> MessageDigestFactoryBean.postConstructHandler() <> MessageDigestFactoryBean.isSingleton() <> MessageDigestFactoryBean.getObject() <> MessageDigestFactoryBean.isSingleton() <> MessageDigestFactoryBean.getObject() <> MessageDigestFactoryBean.isSingleton() <> MessageDigestFactoryBean.getObjectType() <> MessageDigestFactoryBean.isSingleton() <> MessageDigestFactoryBean.getObjectType()推荐答案
我修改了您的MessageDigestFactoryBean类,使其也输出algorithmName,这将有助于清除情况.更改之后,输出为:
I've modified your MessageDigestFactoryBean class so that it outputs algorithmName as well, this will help to clear out the case. After that change, the output is:
<> MessageDigestFactoryBean.postConstructHandler() SHA1 <> MessageDigestFactoryBean.postConstructHandler() MD5 <> MessageDigestFactoryBean.isSingleton() SHA1 <> MessageDigestFactoryBean.getObject() SHA1 <> MessageDigestFactoryBean.isSingleton() MD5 <> MessageDigestFactoryBean.getObject() MD5 <> MessageDigestFactoryBean.isSingleton() SHA1 <> MessageDigestFactoryBean.getObjectType() SHA1 <> MessageDigestFactoryBean.isSingleton() MD5 <> MessageDigestFactoryBean.getObjectType() MD5让我们尝试分析此输出.
Let's try to analyze this output.
这就是您呼叫ctx.refresh()时发生的情况.这只是俯视图,显然Spring在幕后做了很多工作,但这就是我所看到的与您的输出有关的一切.希望这能回答您的第一个问题.
That's what happens when you call ctx.refresh(). This is just over the top look and obviously a lot more is done by Spring under the hood, but that's all I can see related to your output. Hope this answers your first question.
第二个问题-不,您没有做错任何事情,您看到的输出仅反映了Spring在内部的功能.
Now for the second question - no, you did not do anything wrong, the output you see just reflects how Spring functions internally.
更多推荐
春天工厂豆
发布评论