2013-08-28 46 views
0

我知道註釋和xml有兩種依賴注入的方式。我已經嘗試了註釋方式,一切正常,但是當我嘗試使用xml方式時,對我來說某些東西似乎沒有意義。從我的控制器中,我需要調用LabSoftDAOImpl對象並調用該方法。在LabSoftDAOImpl類中,我還需要設置數據源,因爲它將進行查詢。現在我很困惑如何調用LabSoftDAOImpl的新實例,然後使用setter注入來設置它的數據源。不帶@Autowired的依賴注入

這是我爲spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

<context:component-scan 
    base-package="com.peep.ehr.dependencyBuilder , com.peep.ehr.versionTool , com.peep.ehr.surescript" /> 

<mvc:annotation-driven /> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 
    <bean id="dataSourceSureScripts" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 

    <property name="driverClassName" value="xxxxxx" /> 
    <property name="url" 
     value="xxxxx" /> 
    <property name="username" value="xxxxx" /> 
    <property name="password" value="xxxx" /> 
</bean> 

<bean id="providerDao" class="com.peep.ehr.surescript.ProviderDAOImpl"> 
    <property name="dataSource" ref="dataSourceSureScripts" /> 
</bean> 

<bean id="artifactDao" class="com.peep.ehr.versionTool.ArtifactDaoImpl"> 
    <property name="dataSource"> 
     <ref local="dataSource" /> 
    </property> 
</bean> 

<bean id="labSoftDao" class="com.peep.ehr.labSoft.LabSoftDAOImpl"> 
    <property name="dataSource"> 
     <ref local="dataSourceSureScripts" /> 
    </property> 
</bean> 

<bean class="org.springframework.context.support.ResourceBundleMessageSource" 
    id="messageSource"> 
    <property name="basename" value="messages" /> 
</bean> 

<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations" value="classpath:labsoft.properties"> 
    </property> 
</bean> 

<bean id="labsoft" class="com.peep.ehr.labSoft.LabSoft"> 
    <property name="ehrKey" value="${labsoft.Key}" /> 
    <property name="ehrPrefix" value="${labsoft.Prefix}" /> 
</bean> 

<bean id="labsoftController" class="com.peep.ehr.labSoft.LabSoftController"> 
    <property name="LabSoft" ref="labsoft" /> 
</bean> 

控制器

@Controller 
public class LabSoftController { 
static LabSoft lb; 

String practiceName; 

LabSoft LabSoft; 

LabSoftDAOImpl labSoftMethods; 


public void setLabSoftDAOImpl(LabSoftDAOImpl labsoftimpl){ 
    labSoftMethods = labsoftimpl; 
} 


public void setLabSoft(LabSoft labsoft){ 
    LabSoft = labsoft; 
} 
    .... 

LabSoftDAOImpl

public class LabSoftDAOImpl implements LabSoftDAO, InitializingBean { 

private DataSource dataSource; 
JdbcTemplate jdbcTemplate; 

@Override 
public void afterPropertiesSet() throws Exception { 
    if (dataSource == null) { 
     throw new BeanCreationException("Must set dataSource on LabSoftDAOImpl"); 
    } 
} 

public void setDataSource(DataSource dataSource) { 
    this.dataSource = dataSource; 
    this.jdbcTemplate = new JdbcTemplate(dataSource); 
} 
    ... 

回答

3
<bean id="labsoftController" class="com.peep.ehr.labSoft.LabSoftController"> 
    <property name="LabSoft" ref="labsoft" /> 
    <property name="labSoftDao" ref="labSoftDao" /> 
</bean> 

LabSoftController

​​
+0

我試過類似的東西,但只是調用LabSoftDAOImpl。爲什麼它可以和接口類一起工作,但不是它的實現?再次感謝! –

+0

@Jaspreet Chauhan你根本沒有在你的xml中爲你的控制器設置labSoftDao。使用接口是很好的做法。 – Alex