【发布时间】:2011-06-21 05:17:51
【问题描述】:
请查看下面的文件并告诉我为什么 Dao 不会自动装配。当将相同的设置器放入控制器时,它会正确自动连接。我把它放在这个类中,它不起作用。
商务舱
@Component
public class AuthenticateUser {
@Autowired
private SecurityDAO securityDAO;
public void setSecurityDAO(SecurityDAO securityDAO) {
this.securityDAO = securityDAO;
}
protected void execute() {
User authenticatedUser = securityDAO.login(get_userName(),
get_password());
}
}
应用程序上下文.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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">
<bean id="myDataSource"
class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/dbname</value>
</property>
<property name="username">
<value>un</value>
</property>
<property name="password">
<value>pw</value>
</property>
<!-- Disable the second-level cache -->
<!-- Echo all executed SQL to stdout -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.projectname.model.SecurityInfo</value>
<value>com.projectname.model.User</value>
<value>com.projectname.model.Post</value>
<value>com.projectname.model.Article</value>
<value>com.projectname.model.Address</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="mySecurityInfoDAO" class="com.projectname.dao.SecurityDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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:annotation-config />
<context:component-scan
base-package="com.projectname" />
<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/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<value>/WEB-INF/messages/messages</value>
</property>
<property name="cacheSeconds" value="60" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>
【问题讨论】:
-
你能显示
AuthenticateUser所在的包名吗? -
或者更好的是,你能确认
AuthenticateUser这个类实际上是由spring实例化的吗? -
“它不起作用”没有帮助。告诉我们会发生什么。
-
提示:如果你用 (@Autowired, @Ressoucce, @Inject) 注释了一个字段,注入会直接进入这个字段,而不是通过 setter。
-
要通过设置器获取值,请将注释放在设置器上而不是字段上。