{"id":10008050,"date":"2023-07-06T16:59:16","date_gmt":"2023-07-06T08:59:16","guid":{"rendered":"https:\/\/ityet.com\/?p=10008050"},"modified":"2023-07-06T16:59:16","modified_gmt":"2023-07-06T08:59:16","slug":"%e4%b8%80%e9%81%93%e7%bb%8f%e5%85%b8%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9abeanfactory-%e5%92%8c-factorybean-%e6%9c%89%e4%bd%95%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008050","title":{"rendered":"\u4e00\u9053\u7ecf\u5178\u9762\u8bd5\u9898\uff1aBeanFactory \u548c FactoryBean \u6709\u4f55\u533a\u522b\uff1f"},"content":{"rendered":"<p>\u8fd9\u4e5f\u662f Spring \u9762\u8bd5\u65f6\u4e00\u9053\u7ecf\u5178\u7684\u9762\u8bd5\u95ee\u9898\uff0c\u4eca\u5929\u6211\u4eec\u6765\u804a\u4e00\u804a\u8fd9\u4e2a\u8bdd\u9898\u3002<\/p>\n<p>\u5176\u5b9e\u4ece\u540d\u5b57\u4e0a\u5c31\u80fd\u770b\u51fa\u6765\u4e2a\u4e00\u4e8c\uff0cBeanFactory \u662f Factory \u800c FactoryBean \u662f\u4e00\u4e2a Bean\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e0b\u603b\u7ed3\uff1a<\/p>\n<ul>\n<li>BeanFactory \u662f Spring \u6846\u67b6\u7684\u6838\u5fc3\u63a5\u53e3\u4e4b\u4e00\uff0c\u7528\u4e8e\u7ba1\u7406\u548c\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 Bean \u5b9e\u4f8b\u3002\u5b83\u662f\u4e00\u4e2a\u5de5\u5382\u6a21\u5f0f\u7684\u5b9e\u73b0\uff0c\u8d1f\u8d23\u521b\u5efa\u3001\u914d\u7f6e\u548c\u7ba1\u7406 Bean \u5bf9\u8c61\u3002BeanFactory \u662f Spring IoC \u5bb9\u5668\u7684\u57fa\u7840\uff0c\u5b83\u53ef\u4ee5\u4ece\u914d\u7f6e\u5143\u6570\u636e\uff08\u5982 XML \u6587\u4ef6\uff09\u4e2d\u8bfb\u53d6 Bean \u7684\u5b9a\u4e49\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5b9e\u4f8b\u5316\u548c\u63d0\u4f9b\u8fd9\u4e9b Bean\u3002<\/li>\n<li>FactoryBean \u662f\u4e00\u4e2a\u7279\u6b8a\u7684 Bean\uff0c\u5b83\u662f\u4e00\u4e2a\u5de5\u5382\u5bf9\u8c61\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u5176\u4ed6 Bean \u7684\u5b9e\u4f8b\u3002FactoryBean \u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u79cd\u521b\u5efa Bean \u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u5728 Bean \u7684\u521b\u5efa\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9e\u73b0 FactoryBean \u63a5\u53e3\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684 Bean \u5b9e\u4f8b\uff0c\u6216\u8005\u5728 Bean \u5b9e\u4f8b\u5316\u4e4b\u524d\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u903b\u8f91\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u533a\u522b\u5728\u4e8e\uff0cBeanFactory \u662f Spring \u6846\u67b6\u7684\u6838\u5fc3\u63a5\u53e3\uff0c\u7528\u4e8e\u7ba1\u7406\u548c\u63d0\u4f9b Bean \u5b9e\u4f8b\uff0c\u800c FactoryBean \u662f\u4e00\u4e2a\u7279\u6b8a\u7684 Bean\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u5176\u4ed6 Bean \u7684\u5b9e\u4f8b\u3002FactoryBean \u5728 Bean \u7684\u521b\u5efa\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u80fd\u529b\uff0c\u5141\u8bb8\u8fdb\u884c\u989d\u5916\u7684\u903b\u8f91\u5904\u7406\u3002<\/p>\n<p>\u53ef\u80fd\u6709\u7684\u5c0f\u4f19\u4f34\u770b\u7684\u8fd8\u4e0d\u662f\u5f88\u6e05\u695a\uff0c\u6211\u4eec\u518d\u6765\u8be6\u7ec6\u770b\u4e0b\u3002<\/p>\n<h2>1. BeanFactory<\/h2>\n<p>BeanFactory \u770b\u540d\u5b57\u5c31\u77e5\u9053\u8fd9\u662f\u4e00\u4e2a Bean \u5de5\u5382\uff0c\u5c0f\u4f19\u4f34\u4eec\u77e5\u9053\uff0cSpring IoC \u5bb9\u5668\u5e2e\u6211\u4eec\u5b8c\u6210\u4e86 Bean \u7684\u521b\u5efa\u3001\u7ba1\u7406\u7b49\u64cd\u4f5c\uff0c\u90a3\u4e48\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u79bb\u4e0d\u5f00 BeanFactory\u3002<\/p>\n<p>\u6211\u4eec\u6765\u7b80\u5355\u770b\u4e0b BeanFactory \u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">public interface BeanFactory {\n    String FACTORY_BEAN_PREFIX = &quot;&amp;&quot;;\n    Object getBean(String name) throws BeansException;\n    &lt;T&gt; T getBean(String name, Class&lt;T&gt; requiredType) throws BeansException;\n    Object getBean(String name, Object... args) throws BeansException;\n    &lt;T&gt; T getBean(Class&lt;T&gt; requiredType) throws BeansException;\n    &lt;T&gt; T getBean(Class&lt;T&gt; requiredType, Object... args) throws BeansException;\n    &lt;T&gt; ObjectProvider&lt;T&gt; getBeanProvider(Class&lt;T&gt; requiredType);\n    &lt;T&gt; ObjectProvider&lt;T&gt; getBeanProvider(ResolvableType requiredType);\n    boolean containsBean(String name);\n    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;\n    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;\n    boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;\n    boolean isTypeMatch(String name, Class&lt;?&gt; typeToMatch) throws NoSuchBeanDefinitionException;\n    @Nullable\n    Class&lt;?&gt; getType(String name) throws NoSuchBeanDefinitionException;\n    @Nullable\n    Class&lt;?&gt; getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;\n    String[] getAliases(String name);\n\n}<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u57fa\u672c\u4e0a\u90fd\u89c1\u540d\u77e5\u4e49\uff1a<\/p>\n<ul>\n<li>FACTORY\/_BEAN\/_PREFIX\uff1a\u8fd9\u4e2a\u53d8\u91cf\u5176\u5b9e\u662f\u8bf4\uff0c\u5982\u679c\u5f53\u524d Bean \u4e0d\u662f\u50cf User\u3001Book \u8fd9\u79cd\u666e\u901a Bean\uff0c\u800c\u662f\u4e00\u4e2a FactoryBean\uff0c\u5c31\u7ed9\u8fd9\u4e2a Bean \u540d\u5b57\u52a0\u4e00\u4e2a <code>&amp;<\/code> \u524d\u7f00\uff0c\u8fd9\u4e2a\u6211\u5728\u7b2c\u4e8c\u5c0f\u8282\u548c\u5c0f\u4f19\u4f34\u4eec\u6f14\u793a\u3002<\/li>\n<li>getBean\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f\u6839\u636e Bean \u7684\u540d\u5b57\u3001\u7c7b\u578b\u7b49\u53bb\u67e5\u8be2 Bean\u3002<\/li>\n<li>getBeanProvider\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a ObjectProvider\uff0cObjectProvider \u662f Spring \u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u63a5\u53e3\uff0c\u7528\u4e8e\u83b7\u53d6 Bean \u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5ef6\u8fdf\u52a0\u8f7d Bean \u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u52a8\u6001\u5730\u83b7\u53d6 Bean \u5b9e\u4f8b\uff08\u61d2\u52a0\u8f7d\uff09\u3002<\/li>\n<li>containsBean\uff1a\u5224\u65ad\u662f\u5426\u5305\u542b\u67d0\u4e2a Bean\u3002<\/li>\n<li>isSingleton\uff1a\u5224\u65ad\u67d0\u4e2a Bean \u662f\u5426\u662f\u5355\u4f8b\u7684\u3002<\/li>\n<li>isPrototype\uff1a\u5224\u65ad\u67d0\u4e2a Bean \u662f\u5426\u662f\u591a\u4f8b\u7684\u3002<\/li>\n<li>isTypeMatch\uff1a\u5224\u65ad\u67d0\u4e00\u4e2a Bean \u7684\u7c7b\u578b\u662f\u5426\u662f\u7ed9\u5b9a\u7c7b\u578b\u3002<\/li>\n<li>getType\uff1a\u83b7\u53d6 Bean \u7684\u7c7b\u578b\u3002<\/li>\n<li>getAliases\uff1a\u83b7\u53d6 Bean \u7684\u522b\u540d\u3002<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5f88\u591a\u90fd\u662f\u5927\u5bb6\u65e5\u5e38\u5f00\u53d1\u4e2d\u5e38\u89c1\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u5f88\u591a\u5c0f\u4f19\u4f34\u521a\u5f00\u59cb\u63a5\u89e6 Spring \u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u7528\u5230\u4e00\u4e2a\u5bf9\u8c61 <code>ClassPathXmlApplicationContext<\/code>\uff0c\u8fd9\u5176\u5b9e\u5c31\u662f BeanFactory \u7684\u4e00\u4e2a\u5b50\u7c7b\u3002\u6211\u4eec\u6765\u770b\u4e0b BeanFactory \u7684\u7ee7\u627f\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043977755\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7ee7\u627f\u7c7b\u6bd4\u8f83\u591a\uff0c\u6211\u8bf4\u51e0\u4e2a\u5927\u5bb6\u53ef\u80fd\u6bd4\u8f83\u719f\u6089\u7684\uff1a<\/p>\n<ol>\n<li>ClassPathXmlApplicationContext\uff1a\u8fd9\u4e2a\u662f Spring \u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u4ece\u5f53\u524d\u7c7b\u8def\u5f84\u4e0b\u53bb\u52a0\u8f7d XML \u914d\u7f6e\u6587\u4ef6\uff0c\u53c2\u6570\u5c31\u662f classpath \u4e0b XML \u7684\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<li>FileSystemXmlApplicationContext\uff1a\u8fd9\u4e2a\u662f Spring \u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u4ece\u6587\u4ef6\u7cfb\u7edf\u4e2d\u53bb\u52a0\u8f7d XML \u914d\u7f6e\u6587\u4ef6\uff0c\u53c2\u6570\u4e00\u4e2a\u7edd\u5bf9\u8def\u5f84\u3002<\/li>\n<li>AnnotationConfigApplicationContext\uff1a\u8fd9\u4e2a\u662f\u5982\u679c\u6211\u4eec\u4f7f\u7528 Java \u4ee3\u7801\u53bb\u505a Spring \u5bb9\u5668\u7684\u914d\u7f6e\u7684\u8bdd\uff0c\u901a\u8fc7\u8fd9\u4e2a\u914d\u7f6e\u7c7b\u53bb\u52a0\u8f7d Java \u914d\u7f6e\u7c7b\u3002<\/li>\n<li>DefaultListableBeanFactory\uff1a\u8fd9\u4e2a\u9ed8\u8ba4\u5b9e\u73b0\u4e86 ListableBeanFactory \u548c BeanDefinitionRegistry \u63a5\u53e3\uff0c\u662f\u4e00\u4e2a\u6bd4\u8f83\u6210\u719f\u7684 BeanFactory\u3002<\/li>\n<\/ol>\n<p>\u597d\u5566\uff0c\u8fd9\u5c31\u662f BeanFactory \u7684\u7279\u70b9\uff0c\u5927\u5bb6\u660e\u767d\u4e86\u5427\uff5e<\/p>\n<h2>2. FactoryBean<\/h2>\n<h3>2.1 \u5206\u6790<\/h3>\n<p>FactoryBean \u5176\u5b9e\u5f88\u591a\u5c0f\u4f19\u4f34\u53ef\u80fd\u90fd\u89c1\u8fc7\uff0c\u53ea\u662f\u53ef\u80fd\u6ca1\u53bb\u603b\u7ed3\u5f52\u7eb3\u3002\u6211\u7ed9\u5c0f\u4f19\u4f34\u4eec\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u3002<\/p>\n<p>\u5728 SSM \u9879\u76ee\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u8981\u914d\u7f6e MyBatis \u5230\u9879\u76ee\u4e2d\uff0c\u4e00\u822c\u9700\u8981\u914d\u7f6e\u4e0b\u9762\u8fd9\u4e2a Bean\uff1a<\/p>\n<pre><code class=\"xml\">&lt;bean class=&quot;org.mybatis.spring.SqlSessionFactoryBean&quot; id=&quot;sqlSessionFactoryBean&quot;&gt;\n    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;\/&gt;\n    &lt;property name=&quot;typeAliasesPackage&quot; value=&quot;org.javaboy.shirodemo.model&quot;\/&gt;\n    &lt;property name=&quot;mapperLocations&quot;&gt;\n        &lt;list&gt;\n            &lt;value&gt;classpath*:org\/javaboy\/shirodemo\/mapper\/*.xml&lt;\/value&gt;\n        &lt;\/list&gt;\n    &lt;\/property&gt;\n&lt;\/bean&gt;<\/code><\/pre>\n<p>\u6211\u4eec\u5728\u914d\u7f6e Shiro \u7684\u65f6\u5019\uff0c\u4e00\u822c\u90fd\u8981\u914d\u7f6e\u5982\u4e0b Bean\uff1a<\/p>\n<pre><code class=\"xml\">&lt;bean class=&quot;org.apache.shiro.spring.web.ShiroFilterFactoryBean&quot; id=&quot;shiroFilter&quot;&gt;\n    &lt;property name=&quot;securityManager&quot; ref=&quot;securityManager&quot;\/&gt;\n    &lt;property name=&quot;loginUrl&quot; value=&quot;\/login&quot;\/&gt;\n    &lt;property name=&quot;successUrl&quot; value=&quot;\/index&quot;\/&gt;\n    &lt;property name=&quot;unauthorizedUrl&quot; value=&quot;\/unauthorizedUrl&quot;\/&gt;\n    &lt;property name=&quot;filterChainDefinitions&quot;&gt;\n        &lt;value&gt;\n            \/index=anon\n            \/doLogin=anon\n            \/hello=user\n            \/**=authc\n        &lt;\/value&gt;\n    &lt;\/property&gt;\n&lt;\/bean&gt;<\/code><\/pre>\n<p>\u5982\u679c\u6211\u4eec\u524d\u7aef\u4f20\u9012\u7684\u53c2\u6570\u662f key-value \u683c\u5f0f\uff0c\u5e76\u4e14\u6709\u4e00\u4e2a\u65e5\u671f\uff0c\u90a3\u4e48\u5c0f\u4f19\u4f34\u4eec\u77e5\u9053\uff0c\u670d\u52a1\u7aef SpringMVC \u9ed8\u8ba4\u65e0\u6cd5\u5904\u7406\u8fd9\u4e2a\u65e5\u671f\uff0c\u9700\u8981\u914d\u7f6e\u4e00\u4e2a\u65e5\u671f\u8f6c\u6362\u5668\uff0c\u4e00\u822c\u6211\u4eec\u5728 Spring \u5bb9\u5668\u4e2d\u6dfb\u52a0\u5982\u4e0b Bean\uff08\u5bf9\u8fd9\u4e2a\u4e0d\u719f\u6089\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u3010\u6c5f\u5357\u4e00\u70b9\u96e8\u3011\u540e\u53f0\u56de\u590d ssm\uff0c\u6709\u677e\u54e5\u5f55\u5236\u7684\u514d\u8d39\u5165\u95e8\u89c6\u9891\uff09\uff1a<\/p>\n<pre><code class=\"xml\">&lt;bean class=&quot;org.springframework.format.support.FormattingConversionServiceFactoryBean&quot; id=&quot;conversionService&quot;&gt;\n    &lt;property name=&quot;converters&quot;&gt;\n        &lt;set&gt;\n            &lt;ref bean=&quot;myDateConverter&quot;\/&gt;\n        &lt;\/set&gt;\n    &lt;\/property&gt;\n&lt;\/bean&gt;\n&lt;mvc:annotation-driven conversion-service=&quot;conversionService&quot;\/&gt;<\/code><\/pre>\n<p>\u5c0f\u4f19\u4f34\u4eec\u89c2\u5bdf\u4e0a\u9762\u4e09\u4e2a Bean \u6709\u4e00\u4e2a\u5171\u540c\u7684\u7279\u70b9\uff0c<strong>\u90a3\u5c31\u662f Bean \u7684\u540d\u5b57\u90fd\u662f xxxFactoryBean\u3002<\/strong><\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u7528 xxxFactoryBean \u800c\u4e0d\u76f4\u63a5\u628a\u9700\u8981\u7684 Bean \u6ce8\u5165\u5230 Spring \u5bb9\u5668\u4e2d\u53bb\u5462\uff1f\u4ee5 MyBatis \u4e3a\u4f8b\uff1a<\/p>\n<p>\u624b\u52a8\u914d\u7f6e\u8fc7 MyBatis \u7684\u5c0f\u4f19\u4f34\u5e94\u8be5\u90fd\u77e5\u9053\uff0cMyBatis \u6709\u4e24\u4e2a\u91cd\u8981\u7684\u7c7b\uff0c\u4e00\u4e2a\u662f SqlSessionFactory\uff0c\u8fd8\u6709\u4e00\u4e2a\u662f SqlSession\uff0c\u901a\u8fc7 SqlSessionFactory \u53ef\u4ee5\u83b7\u53d6\u5230\u4e00\u4e2a SqlSession\u3002\u4f46\u662f\u4e0d\u77e5\u9053\u5c0f\u4f19\u4f34\u4eec\u662f\u5426\u8fd8\u8bb0\u5f97\u914d\u7f6e\u4ee3\u7801\uff0c\u624b\u52a8\u914d\u7f6e\u4ee3\u7801\u5982\u4e0b\uff08\u5bf9\u8fd9\u4e2a\u4e0d\u719f\u6089\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u3010\u6c5f\u5357\u4e00\u70b9\u96e8\u3011\u540e\u53f0\u56de\u590d ssm\uff0c\u6709\u677e\u54e5\u5f55\u5236\u7684\u514d\u8d39\u5165\u95e8\u89c6\u9891\uff09\uff1a<\/p>\n<pre><code class=\"java\">public class SqlSessionFactoryUtils {\n    private static SqlSessionFactory SQLSESSIONFACTORY = null;\n    public static SqlSessionFactory getInstance() {\n        if (SQLSESSIONFACTORY == null) {\n            try {\n                SQLSESSIONFACTORY = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(&quot;mybatis-config.xml&quot;));\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n        }\n        return SQLSESSIONFACTORY;\n    }\n}\npublic class Main {\n    public static void main(String[] args) {\n        SqlSessionFactory factory = SqlSessionFactoryUtils.getInstance();\n        SqlSession sqlSession = factory.openSession();\n        List&lt;User&gt; list = sqlSession.selectList(&quot;org.javaboy.mybatis01.mapper.UserMapper.getAllUser&quot;);\n        for (User user : list) {\n            System.out.println(&quot;user = &quot; + user);\n        }\n        sqlSession.close();\n    }\n}<\/code><\/pre>\n<p>\u5c0f\u4f19\u4f34\u4eec\u770b\u5230\uff0c\u65e0\u8bba\u662f SqlSessionFactory \u8fd8\u662f SqlSession\uff0c\u90fd\u4e0d\u662f\u6b63\u7ecf new \u51fa\u6765\u7684\uff0c\u5176\u5b9e\u8fd9\u4e24\u4e2a\u90fd\u662f\u63a5\u53e3\uff0c\u663e\u7136\u4e0d\u53ef\u80fd new \u51fa\u6765\uff0c\u524d\u8005\u901a\u8fc7\u5efa\u9020\u8005\u6a21\u5f0f\u53bb\u914d\u7f6e\u5404\u79cd\u5c5e\u6027\uff0c\u6700\u540e\u751f\u6210\u4e00\u4e2a SqlSessionFactory \u7684\u5b9e\u4f8b\uff0c\u540e\u8005\u5219\u901a\u8fc7\u524d\u8005\u8fd9\u4e2a\u5de5\u5382\u53bb\u751f\u6210\uff0c\u6700\u7ec8\u62ff\u5230\u7684\u90fd\u662f\u8fd9\u4e24\u4e2a\u63a5\u53e3\u7684\u5b50\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5bf9\u4e8e SqlSessionFactory \u548c SqlSession \u5c31\u6ca1\u6cd5\u5728 Spring \u5bb9\u5668\u4e2d\u76f4\u63a5\u8fdb\u884c\u914d\u7f6e\uff0c\u90a3\u4e48\u5bf9\u4e8e\u8fd9\u6837\u7684 Bean\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7 xxxFactoryBean \u6765\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n<p>\u6211\u4eec\u6765\u770b\u4e0b SqlSessionFactoryBean \u7c7b\uff0c\u6e90\u7801\u5f88\u957f\uff0c\u6211\u6311\u4e86\u91cd\u8981\u7684\u51fa\u6765\uff1a<\/p>\n<pre><code class=\"java\">public class SqlSessionFactoryBean implements FactoryBean&lt;SqlSessionFactory&gt;, InitializingBean, ApplicationListener&lt;ApplicationEvent&gt; {\n\n  private SqlSessionFactory sqlSessionFactory;\n\n  @Override\n  public SqlSessionFactory getObject() throws Exception {\n    if (this.sqlSessionFactory == null) {\n      afterPropertiesSet();\n    }\n\n    return this.sqlSessionFactory;\n  }\n  @Override\n  public Class&lt;? extends SqlSessionFactory&gt; getObjectType() {\n    return this.sqlSessionFactory == null ? SqlSessionFactory.class : this.sqlSessionFactory.getClass();\n  }\n  @Override\n  public boolean isSingleton() {\n    return true;\n  }\n}<\/code><\/pre>\n<p>\u5927\u5bb6\u770b\u4e00\u4e0b\uff0cSqlSessionFactoryBean \u9700\u8981\u5b9e\u73b0 FactoryBean \u63a5\u53e3\uff0c\u5e76\u4e14\u5728\u5b9e\u73b0\u63a5\u53e3\u7684\u65f6\u5019\u6307\u5b9a\u6cdb\u578b\u662f SqlSessionFactory\uff0c\u4e5f\u5c31\u662f SqlSessionFactoryBean \u6700\u7ec8\u4ea7\u51fa\u7684 Bean \u662f SqlSessionFactory\u3002\u5b9e\u73b0\u4e86 FactoryBean \u63a5\u53e3\u4e4b\u540e\uff0c\u5c31\u9700\u8981\u5b9e\u73b0\u63a5\u53e3\u4e2d\u7684\u4e09\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>getObject\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u7684\u5bf9\u8c61\uff0c\u5c31\u662f\u771f\u6b63\u8981\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\uff0c\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6309\u7167\u5404\u79cd\u65b9\u5f0f\u5bf9 Bean \u8fdb\u884c\u5404\u79cd\u914d\u7f6e\u4e86\u3002<\/li>\n<li>getObjectType\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\u7c7b\u578b\u3002<\/li>\n<li>isSingleton\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u7528\u6765\u8fd4\u56de\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\u7684 Bean \u662f\u5426\u662f\u5355\u4f8b\u7684\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u5c31\u662f FactoryBean \u7684\u7279\u70b9\uff0c\u7531\u4e8e\u67d0\u4e00\u4e2a Bean \u7684\u521d\u59cb\u5316\u8fc7\u4e8e\u590d\u6742\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u901a\u8fc7 FactoryBean \u6765\u5e2e\u52a9\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\u53bb\u3002<\/p>\n<h3>2.2 \u5b9e\u8df5<\/h3>\n<p>\u677e\u54e5\u518d\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u7ed9\u5c0f\u4f19\u4f34\u4eec\u4f53\u9a8c\u4e00\u628a FactoryBean\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u6709\u5982\u4e0b\u7c7b\uff1a<\/p>\n<pre><code class=\"java\">public class Author {\n\n    private String name;\n    private Integer age;\n\n    private Author() {\n    }\n\n    public static Author init(String name, Integer age) {\n        Author author = new Author();\n        author.setAge(age);\n        author.setName(name);\n        return author;\n    }\n    \/\/\u7701\u7565 getter\/setter\/toString\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7c7b\u7684\u7279\u70b9\u5c31\u662f\u6784\u9020\u65b9\u6cd5\u662f\u79c1\u6709\u7684\uff0c\u4f60\u6ca1\u6cd5\u4ece\u5916\u9762\u53bb new\uff0c\u73b0\u5728\u6211\u60f3\u5c06\u8fd9\u4e2a\u7c7b\u7684\u5bf9\u8c61\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\uff0c\u90a3\u4e48\u6211\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a AuthorFactoryBean\uff1a<\/p>\n<pre><code class=\"java\">public class AuthorFactoryBean implements FactoryBean&lt;Author&gt; {\n    @Override\n    public Author getObject() throws Exception {\n        return Author.init(&quot;javaboy&quot;, 99);\n    }\n\n    @Override\n    public Class&lt;?&gt; getObjectType() {\n        return Author.class;\n    }\n\n    @Override\n    public boolean isSingleton() {\n        return true;\n    }\n}<\/code><\/pre>\n<p>\u7136\u540e\u5728 Spring \u5bb9\u5668\u4e2d\u914d\u7f6e AuthorFactoryBean \u5373\u53ef\uff1a<\/p>\n<pre><code class=\"xml\">&lt;bean class=&quot;org.javaboy.bean.AuthorFactoryBean&quot; id=&quot;author&quot;\/&gt;<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece\u5bb9\u5668\u4e2d\u53bb\u83b7\u53d6 Author \u5bf9\u8c61\u4e86\uff0c\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u901a\u8fc7 author \u8fd9\u4e2a\u540d\u5b57\u62ff\u5230\u7684\u662f Author \u5bf9\u8c61\uff0c\u800c\u4e0d\u662f AuthorFactoryBean \u5bf9\u8c61\uff0c\u5982\u679c\u60f3\u8981\u83b7\u53d6\u5230 AuthorFactoryBean \u5bf9\u8c61\uff0c\u90a3\u4e48\u8981\u901a\u8fc7 <code>&amp;author<\/code> \u8fd9\u4e2a\u540d\u5b57\u53bb\u83b7\u53d6\uff08\u56de\u987e\u7b2c\u4e00\u5c0f\u8282\u6240\u8bb2\u5185\u5bb9\uff09\u3002<\/p>\n<pre><code class=\"java\">public class Main {\n    public static void main(String[] args) {\n        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);\n        Object author = ctx.getBean(&quot;author&quot;);\n        Object authorFactoryBean = ctx.getBean(&quot;&amp;author&quot;);\n        System.out.println(&quot;author.getClass() = &quot; + author.getClass());\n        System.out.println(&quot;authorFactoryBean.getClass() = &quot; + authorFactoryBean.getClass());\n    }\n}<\/code><\/pre>\n<p>\u6765\u770b\u4e0b\u6700\u7ec8\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043977756\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8ddf\u6211\u4eec\u6240\u60f3\u7684\u4e00\u81f4\u5427\uff5e<\/p>\n<h2>3. \u5c0f\u7ed3<\/h2>\n<p>\u7ecf\u8fc7\u524d\u9762\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u5c0f\u4f19\u4f34\u4eec\u5df2\u7ecf\u80fd\u591f\u533a\u5206 BeanFactory \u548c FactoryBean \u4e86\uff0c\u518d\u6765\u56de\u987e\u4e00\u4e0b\u672c\u6587\u5f00\u5934\u7684\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>BeanFactory \u662f Spring \u6846\u67b6\u7684\u6838\u5fc3\u63a5\u53e3\u4e4b\u4e00\uff0c\u7528\u4e8e\u7ba1\u7406\u548c\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 Bean \u5b9e\u4f8b\u3002\u5b83\u662f\u4e00\u4e2a\u5de5\u5382\u6a21\u5f0f\u7684\u5b9e\u73b0\uff0c\u8d1f\u8d23\u521b\u5efa\u3001\u914d\u7f6e\u548c\u7ba1\u7406 Bean \u5bf9\u8c61\u3002BeanFactory \u662f Spring IoC \u5bb9\u5668\u7684\u57fa\u7840\uff0c\u5b83\u53ef\u4ee5\u4ece\u914d\u7f6e\u5143\u6570\u636e\uff08\u5982 XML \u6587\u4ef6\uff09\u4e2d\u8bfb\u53d6 Bean \u7684\u5b9a\u4e49\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5b9e\u4f8b\u5316\u548c\u63d0\u4f9b\u8fd9\u4e9b Bean\u3002<\/li>\n<li>FactoryBean \u662f\u4e00\u4e2a\u7279\u6b8a\u7684 Bean\uff0c\u5b83\u662f\u4e00\u4e2a\u5de5\u5382\u5bf9\u8c61\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u5176\u4ed6 Bean \u7684\u5b9e\u4f8b\u3002FactoryBean \u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u79cd\u521b\u5efa Bean \u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u5728 Bean \u7684\u521b\u5efa\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9e\u73b0 FactoryBean \u63a5\u53e3\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684 Bean \u5b9e\u4f8b\uff0c\u6216\u8005\u5728 Bean \u5b9e\u4f8b\u5316\u4e4b\u524d\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u903b\u8f91\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u533a\u522b\u5728\u4e8e\uff0cBeanFactory \u662f Spring \u6846\u67b6\u7684\u6838\u5fc3\u63a5\u53e3\uff0c\u7528\u4e8e\u7ba1\u7406\u548c\u63d0\u4f9b Bean \u5b9e\u4f8b\uff0c\u800c FactoryBean \u662f\u4e00\u4e2a\u7279\u6b8a\u7684 Bean\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u5176\u4ed6 Bean \u7684\u5b9e\u4f8b\u3002FactoryBean \u5728 Bean \u7684\u521b\u5efa\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u80fd\u529b\uff0c\u5141\u8bb8\u8fdb\u884c\u989d\u5916\u7684\u903b\u8f91\u5904\u7406\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043977753\">https:\/\/segmentfault.com\/a\/1190000043977753<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e5f\u662f Spring \u9762\u8bd5\u65f6\u4e00\u9053\u7ecf\u5178\u7684\u9762\u8bd5\u95ee\u9898\uff0c\u4eca\u5929\u6211\u4eec\u6765\u804a\u4e00\u804a\u8fd9\u4e2a\u8bdd\u9898\u3002 \u5176\u5b9e\u4ece\u540d\u5b57\u4e0a\u5c31\u80fd\u770b\u51fa\u6765\u4e2a\u4e00\u4e8c\uff0cBe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008050","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008050"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008050\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}