Spring bean.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-2.5.xsd">
            <!-- 使用类构造器实例化 -->
            <bean id="personService1" class="service.PersonServiceImpl"></bean>
            <!-- 使用静态工厂方法实例化 -->
            <bean id="personService2" class="service.PersonServiceFactory" factory-method="createPersonService"></bean>
            <!-- 使用实例工厂方法实例化 -->
            <bean id="personServiceFactory" class="service.PersonServiceFactory"></bean>
            <bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceD"></bean>
</beans>

依赖注入两种方法:

<bean id="personDao" class="dao.PersonDaoImpl"></bean> 
<bean id="personService1" class="service.PersonServiceImpl">
    <!-- ref注入-->
    <property name="personDao" ref="personDao"></property>
</bean>
<bean id="personService4" class="service.PersonServiceImpl">
                <!-- 内部bean注入 -->
                <property name="personDao">
                    <bean class="dao.PersonDaoImpl"/>
                </property>
            </bean>

其中还可以为Service的基本属性注入值,比如service有属性:private String name;

<bean id="personService4" class="service.PersonServiceImpl">
                <!-- 内部bean注入 -->
    <property name="name" value=“hello”>
     </property>
 </bean>

集合属性的注入

<bean id="personService4" class="service.PersonServiceImpl">
<property name="list">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
</bean>


你可能感兴趣的:(java,xml,String)