spring xml based bean definition, class attribute of `bean` element



docs says:



"The class attribute defines the type of the bean and uses the fully qualified classname. "





You use the Class property in one of two ways:

Typically, to specify the bean class to be constructed in the case where the container itself directly creates the bean by calling its constructor reflectively, somewhat equivalent to Java code using the new operator.
To specify the actual class containing the static factory method that will be invoked to create the object, in the less common case where the container invokes a static factory method on a class to create the bean. The object type returned from the invocation of the static factory method may be the same class or another class entirely.


So how to explain this case:


1)spring bean definition XML classattribute.xml:



<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="minPoolSize" value="${connpool.minSize}"></property>
<property name="maxPoolSize" value="${connpool.maxSize}"></property>
</bean>

<!-- EntityManagerFactory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="${entity.package}"></property>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<!-- other props -->
</props>
</property>
</bean>


2)test case:



import javax.persistence.EntityManagerFactory;
//...

private ApplicationContext ctx;

@Test
public void test(){
Object bean = ctx.getBean("entityManagerFactory");
System.out.println(bean.getClass());
System.out.println(bean instanceof EntityManagerFactory);
System.out.println(new LocalContainerEntityManagerFactoryBean() instanceof EntityManagerFactory);
System.out.println(new LocalContainerEntityManagerFactoryBean().getClass() == bean.getClass());
}


@Before
public void setUp() {
ctx = new ClassPathXmlApplicationContext("classattribute.xml");
}


3)The result:



class $Proxy14
true
false
false


4)I don't think classattribute.xml instantiates the bean entityManagerFactory with a static factory method since factory-method attribut of '' element is not specified.


5)Can anyone explain it? Thanks in advance!


No comments:

Post a Comment