我是春天的新手,所以请耐心等待。
我尝试使用注释和java配置我的应用。但是,数据源不会被注入我的dao impl。
我有一个属性文件,它维护3个不同的配置(dev,uat,prd),可以通过 CommonConfig 访问。
我遇到错误 -
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'dataSource' is required for bean 'jdbcClientFundDao'
DAO -
package com.globeop.goreporting.gonavpack.persistence.dao;
import java.util.ArrayList;
import java.util.List;
public interface ClientFundDao {
public List<String> getClientsForUser(String user);
public ArrayList<String> getFundsForClient(String client);
}
DAO实施者 -
package com.globeop.goreporting.gonavpack.persistence.dao.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.globeop.goreporting.gonavpack.persistence.dao.ClientFundDao;
@Repository
public class JdbcClientFundDao implements ClientFundDao {
private JdbcTemplate jdbcTemplate;
@Required // tried @Autowired also
public void setDataSource(DataSource datasource) {
this.jdbcTemplate = new JdbcTemplate(datasource);
}
public List<String> getClientsForUser(String user) {
List<String> clients = jdbcTemplate.queryForList("select distinct ClientShortName from Client where IsActiveFlag = ?", String.class, 1);
return clients;
}
public ArrayList<String> getFundsForClient(String client) {
// TODO Auto-generated method stub
return null;
}
}
DataScource -
package com.globeop.goreporting.gonavpack.persistence.datasource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.globeop.goreporting.gonavpack.model.CommonConfig;
@Configuration
public class DataSourceForClientFund {
private static final String driverClassName = CommonConfig.DB_PARAMETERMAP_DRIVER;
private static final String url = CommonConfig.DB_PARAMETERMAP_URL;
private static final String dbUsername = CommonConfig.DB_PARAMETERMAP_USER;
private static final String dbPassword = CommonConfig.DB_PARAMETERMAP_PWD;
@Bean
public static BasicDataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
}
WebApplication Context(NavSummaryServlet-context.xml) -
<!-- Contains @Controller, @RequestMapping, view resolver, etc -->
<context:component-scan base-package="com.globeop.goreporting.gonavpack.webconfigs" />
<mvc:annotation-driven />
应用程序上下文(NavSummaryApplication-context.xml) -
<!-- Contains DAO, DAO impl, and DataSource beans -->
<context:annotation-config/>
<context:component-scan base-package="com.globeop.goreporting.gonavpack.persistence" />
的web.xml -
<servlet>
<servlet-name>NavSummaryServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/goNavPackServlet/NavSummaryServlet-context.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>NavSummaryServlet</servlet-name>
<url-pattern>/navSummary.jsp</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/goNavPackApp/NavSummaryApplication-context.xml</param-value>
</context-param>
答案 0 :(得分:1)
您无需初始化班级中的JdbcTemplate
。 IoC容器的重点是让它为您完成。
因此,添加@Bean
方法来创建它。 (另外,不要让你的方法保持静态,在这种情况下不需要)
@Configuration
@ComponentScan(basePackages = "the.name.of.your.package")
public class DataSourceForClientFund {
private static final String driverClassName = CommonConfig.DB_PARAMETERMAP_DRIVER;
private static final String url = CommonConfig.DB_PARAMETERMAP_URL;
private static final String dbUsername = CommonConfig.DB_PARAMETERMAP_USER;
private static final String dbPassword = CommonConfig.DB_PARAMETERMAP_PWD;
@Bean
public BasicDataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(getDataSource()); // notice this is calling the other Bean method
jdbcTemplate.afterPropertiesSet();
return jdbcTemplate;
}
}
然后,您可以直接@Autowired
或@Inject
JdbcTemplate
。
在@Repository
@Repository
public class JdbcClientFundDao implements ClientFundDao {
@Autowired
private JdbcTemplate jdbcTemplate;
...
}
在@Controller
@Controller
public class YourController {
@Autowired
private ClientFundDao clientFundDao; // Spring will inject an `JdbcClientFundDao` instance
}
This Spring documentation应该解释这些配置是如何完成的。