您好我是Spring Framework的新手。我只是在创建POC来学习Spring Concepts。虽然实现相同,但我在加载应用程序时遇到异常。请提供解决此问题的建议。
"使用名称' userServicesDao创建bean时出错:通过方法' setDbConfiguration'表达不满意的依赖关系参数0"
UserServicesDao
package com.pe.dao;
@Component
public class UserServicesDao {
private DBConfiguration dbConfiguration;
@Autowired()
public void setDbConfiguration(DBConfiguration dbConfiguration) {
this.dbConfiguration = dbConfiguration;
}
public List<User> getUser()
{
System.out.println("User Services Dao : "+this.toString());
System.out.println("Connection Info : "+this.dbConfiguration);
return new ArrayList<User>();
}
@Override
public String toString() {
return "UserServicesDao [connection Config=" + dbConfiguration + "]";
}
}
ConnectionConfig.Java
package com.pe.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@Component(value ="dbConfiguration")
public class DBConfiguration {
@Value("com.mysql.jdbc.Driver")
private String driver;
@Value("jdbc:mysql://localhost:3306/mydb")
private String url;
@Value("root")
private String username;
@Value("root")
private String password;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
弹簧servlet.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:component-scan base-package="com.pe.controller,com.pe.dao,com.pe.bean" />
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix" value=".jsp">
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans>
Web.xml中
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/spring</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/predictenergy/bean/dao-config.xml</param-value>
</context-param>
</web-app>
Click to see Class Folder Structure
如果我是通过xml进行修改,它会在修正之前进行自动装配。请参考我之前使用的xml代码。
<bean id ="userServicesDao" class="com.pe.dao.UserServicesDao" autowire="byType">
<property name="dbConfiguration" ref= "dbConfiguration"></property>
</bean>
<bean id ="dbConfiguration" class ="com.pe.bean.DBConfiguration">
<property name="driver" value= "com.mysql.jdbc.Driver"></property>
<property name="url" value= "jdbc:mysql://localhost:3306/mydb"></property>
<property name="username" value= "root"></property>
<property name="password" value= "root"></property>
</bean>
请指导我的解决方案。