Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无@Autowired的依赖注入_Java_Spring_Spring Mvc - Fatal编程技术网

Java 无@Autowired的依赖注入

Java 无@Autowired的依赖注入,java,spring,spring-mvc,Java,Spring,Spring Mvc,我知道有两种方法可以使用注释和xml实现依赖内射。我尝试过注释方式,一切都很好,但是当我尝试xml方式时,有些东西似乎对我来说没有意义。从我的控制器中,我需要调用LabSoftDAOImpl对象并调用该方法。在LabSoftDAOImpl类中,我还需要设置数据源,因为它将执行查询。现在我对如何调用LabSoftDAOImpl的新实例,然后使用setter注入设置其数据源感到困惑 这是我的spring-servlet.xml <?xml version="1.0" encoding="UTF

我知道有两种方法可以使用注释和xml实现依赖内射。我尝试过注释方式,一切都很好,但是当我尝试xml方式时,有些东西似乎对我来说没有意义。从我的控制器中,我需要调用LabSoftDAOImpl对象并调用该方法。在LabSoftDAOImpl类中,我还需要设置数据源,因为它将执行查询。现在我对如何调用LabSoftDAOImpl的新实例,然后使用setter注入设置其数据源感到困惑

这是我的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>
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);
}
    ...

我尝试了类似的方法,但只是调用了LabSoftDAOImpl。为什么它与接口类一起工作,而不是它的实现?再次感谢@Jaspreet Chauhan您根本没有在xml中为控制器设置labSoftDao。使用接口是一种很好的做法。我尝试了类似的方法,但只是调用了LabSoftDAOImpl。为什么它与接口类一起工作,而不是它的实现?再次感谢@Jaspreet Chauhan您根本没有在xml中为控制器设置labSoftDao。使用接口是很好的实践。
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);
}
    ...
<bean id="labsoftController" class="com.peep.ehr.labSoft.LabSoftController">
    <property name="LabSoft" ref="labsoft" />
    <property name="labSoftDao" ref="labSoftDao" />
</bean>
private LabSoftDAO labSoftDao;

public void setLabSoftDao(LabSoftDAO labSoftDao){
    this.labSoftDao = labSoftDao;
}