<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/login"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean name="jdbcTemplete" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="DataSource" ref="dataSource">
</bean>
上面的代码是xml文件的,我在用spring,然后想把这个改成注解,该怎么写,我moudle里面用的三层结构,我自己是新建了一个
@Component
public class JDBCTemplate {
@Autowired
private DruidDataSource dataSource;
}
然后我在dao层里面注入了
@Autowired
private JdbcTemplate template;
我以为这样可以解决问题,但是并没有,
报错:
Error creating bean with name 'userDaoImpl': Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这个类是注解配置连接数据库的
@Configuration
@PropertySource("db.properties")
//引入外部配置文件
public class JDBCconfig {
@Value("${MyDriverClassName}")
private String myclassname;
@Value("${myUrl}")
private String myurl;
@Value("${myUsername}")
private String myusername;
@Value("${myPassword}")
private String mypassword;
// 创建连接池对象
@Bean
public DruidDataSource DataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(myclassname);
druidDataSource.setUrl(myurl);
druidDataSource.setUsername(myusername);
druidDataSource.setPassword(mypassword);
return druidDataSource;
}
不能。。。。。。。。。。。。。。。。