新手在学ssm,整合的时候发现好像死活都注入不了
但是我注释也扫了,也加了@注释
jdk1.8
下面的tomcat(9.0.16)的运行日志
18-Mar-2020 01:22:49.871 淇℃伅 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.FrameworkServlet.initServletBean Initializing Servlet 'dispatcherServlet'
18-Mar-2020 01:22:50.984 璀﹀憡 [RMI TCP Connection(3)-127.0.0.1] org.springframework.context.support.AbstractApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountController': Unsatisfied dependency expressed through field 'accountService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountService': Unsatisfied dependency expressed through field 'AccountDao'; n**ested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'all.dao.AccountDao' available: expected at least 1 bean which qualifies as autowire candidate. **Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
package all.service.impl;
import all.dao.AccountDao;
import all.domain.Account;
import all.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
@Override
public void save() {
Account account = new Account();
accountDao.save(account);
}
}
```package all.controller;
import all.domain.Account;
import all.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
@RequestMapping("/findAll")
public String findAll(Model model){
List<Account> accounts = accountService.findAll();
model.addAttribute("accounts",accounts);
return "success";
}
@RequestMapping("/save")
public void save(Account account){
accountService.save();
}
}
package all.dao;
import all.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface AccountDao {
@Select("select * from account")
public List<Account> findAll();
@Insert("insert into account (name,money) values(#{name},#{money})")
public void save(Account account);
}
<!-- spring整合mybatis框架-->
<!-- 配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///ssm?serverTimezone=UTC"/>
<property name="user" value="root"/>
<property name="password" value="0759"/>
</bean>
<!--配置工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置接口所在的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="all.dao"></property>
</bean>
配置文件中定义了
<context:component-scan base-package="">
吗?
为什么要多写这个方法呢setAccountService