2016-01-08 34 views
0

我遇到了this question中描述的問題,我已經通過在構造函數中添加了@Autowired註解來解決這個問題。現在我想知道,爲什麼它有幫助。是什麼彈簧構造函數依賴注入 - 有vs無自動裝線

public RegistrationController(UserDao userDao) { 
    this.userDao = userDao; 
} 

@Autowired 
public RegistrationController(UserDao userDao) { 
    this.userDao = userDao; 
} 

之間的差異在這兩種情況下,userDAO的注入到控制器。我發現的唯一區別是,使用@persistenceContext標註標記的entityManager僅在第二個示例中注入userDao。但我不知道爲什麼。任何線索?還有其他的區別嗎?

編輯:我的servlet上下文是這樣的:

<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" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <!-- 
    Adds some default beans (HandlerAdapter, HandlerMapping, Binding Initializer...). It also turn on some annotations. 
    Explanation in https://stackoverflow.com/questions/28851306/spring-framework-what-is-the-purpose-of-mvcannotation-driven 

    WITHOUT THIS, @RequestMapping ANNOTATIONS ARE LOADED, BUT MAPPING DO NOT WORK!! 
    --> 
    <mvc:annotation-driven /> 

<!-- Set loading annotations from classes 
    <context:component-scan base-package="com.fido.pia"/>--> 

    <!--manual homepage--> 
    <mvc:view-controller path="/" view-name="home"/> 

    <!--view resolver--> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/view/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <!--static resources - request will be handeled by ResourceHttpRequestHandler--> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <!--database config--> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/pia" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 

    <!--entity manager factory--> 
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="packagesToScan" value="com.fido.pia.*/**" /> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <!--<property name="generateDdl" value="true" />--> 
       <property name="showSql" value="true" /> 
      </bean> 
     </property> 
    </bean> 

    <!-- Transactions --> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <!-- enable the configuration of transactional behavior based on annotations --> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <!--Set loading annotations from classes--> 
    <context:component-scan base-package="com.fido.pia"/> 
</beans> 

編輯2: 控制器:

package com.fido.pia; 

import com.fido.pia.dao.UserDao; 
import com.fido.pia.model.User; 
import java.io.IOException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping("/register") 
public class RegistrationController { 

    private UserDao userDao; 

    @Autowired 
    public RegistrationController(UserDao userDao) { 
     this.userDao = userDao; 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    @Transactional 
    public void registrationSubmit(HttpServletRequest request, HttpServletResponse response) 
      throws IOException{ 
     String username = request.getParameter("first_name"); 

     userDao.save(new User("test")); 

     response.sendRedirect("/"); 
    } 
} 

道:

package com.fido.pia.dao; 

import com.fido.pia.model.User; 
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 
import javax.persistence.PersistenceContextType; 
import org.springframework.stereotype.Component; 
import org.springframework.stereotype.Repository; 


@Repository 
public class UserDao { 

    @PersistenceContext 
    protected EntityManager entityManager; 

    public User save(User row) { 
     if(row.isNew()) { 
      entityManager.persist(row); 
      return row; 
     } else { 
      return entityManager.merge(row); 
     } 
    } 
} 
+0

這不是我的意思的情況下(不好的用法,抱歉)的需求。發佈一個[MCVE],它會證明你的困惑。 –

+0

對不起,現在我添加了控制器和道碼。我想現在應該是完整的,但如果相關,我可以添加更多信息。 – Fido

+0

'@ Autowired'會暴露你在'Application Context'中注入的bean。 – msagala25

回答

-4

存在兩種形式的注入最使用對象!

- First 
    @Autowired, 
- Second in constructor 
@Autowired 
constructor(Object object) 
- other 
@Autowired 
setObjectMehod(Object object) 

接口爲兩個以上的對象來實現,有必要有資格被插入的對象。

總是每對象將被注入到組件,最常見的@Repository,@Services,@Controller和@Component各有其適當範圍。

注射有異曲同工之妙,只有不同的方式,並根據需要由對象

+1

這很難理解。如果您不太會說英語,請讓別人查看您的帖子。 –