问题1:明明设置了这个名字的bean类,但是就是找不到
解决:可能是在使用注解配置的时候,生成的不是想要的 例如: configerable 和 configertion
//配置注解的格式以及扫描注解的格式很重要
//设置当前为配置类
//@Configurable //用这个注解的话,在扫描的时候不需要{}
@Configuration //使用这个注解的话,扫描注解需要有{}
//开启注解扫描
@ComponentScan("com.by")
方法二:实现层(Impl)没有设置repository 或者是component的注解
@Component("UserDao")
//@Repository
//要一个就行
public class UserDaoImpl implements UserDao {
方法三: 可能是:在写这里的时候写错了
ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
// ApplicationContext ac = new AnnotationConfigApplicationContext("application.xm;");
方法四:
用方法类的话,写成.class类型的,不能直接使用接口,使用接口的实现类
@Test
public void test01(){
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
OrderService orderService = ac.getBean(OrderService.class);
orderService.save02();
OrderDao orderDao = (OrderDao) ac.getBean("orderDaoImpl");
orderDao.save01();
}
方法四:使用纯注解开发模式的时候,扫描配置文件的时候,加上了双引号
//错误格式
// ApplicationContext ac = new AnnotationConfigApplicationContext("OrderConfig.class");
ApplicationContext ac = new AnnotationConfigApplicationContext("OrderConfig.class");
问题2 : @PostContract 和 @PreDestroy 无法导入的原因是 :没有将javax.connection的架包导进来
解决方法:在xml的配置文件中添加导包
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
问题三: jdbcTemplate的使用,在进行测试的时候,测试名字不能和包名一样。
解决方法:
会出现系统无法识别是包名还是类名,将测试的类名改了就可以了
还有一种找错的方法,看导入的包的种类,如果没有使用想要的包名,肯定是在本类里边关于导包的地方出现错误
问题四: 无法加载应用程序上下文
解决方法:
问题五:java.lang.IllegalStateException: Mapped class was not specified
Java . lang . illegalstateexception:未指定映射类
解决方法:添加实体类即可
问题六:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.by.service.AccountService' available
org . spring framework . beans . factory . nosuchbean definitionexception:没有“com.by.service.AccountService”类型的合格bean可用
解决方法:在是实现接口的时候,没有给名字
@Service("AccountService")//加上这个就可以了
public class AccountServiceImpl implements AccountService {
问题七:Parameter index out of range (2 > number of parameters, which is 1).
参数越界问题,根据id删除,只需要写出所需要的参数,例如当数据库中还有name时,不需要回去name的值
public void delete(Account account) {
String sql ="delete from account where id = ?";
Object [] acc ={account.getId()};
//错误写法
//Object [] acc ={account.getId(),account.getname};
int delete = jdbcTemplate.update(sql, acc);
System.out.println(delete);
ApplicationContext ac= new ClassPathXmlApplicationContext("application.xml");
AccountService accountService = ac.getBean(AccountService.class);
Account account = new Account();
account.setId(1);
accountService.delete(account);
将数据库的配置文件单独提取出来一个文件在配置
在application.xml的文件中,增加content的配置
<?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"
xsi:schemaLocation="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">
相对于上边的文件,只是更改了红色方框里的内容
ClassPathXmlApplicationContext 和 AnnotationConfigApplicationContext 的区别使用
当我们使用注解配置时,就不能再用之前的ClassPathXmlApplicationContext()构造方法创建ioc容器了,而是要使用AnnotationConfigApplicationContext()方法创建ioc容器
当使用xml的文件的时候,使用ClassPathXmlApplicationContext()构造方法去建立ioc容器;
当使用注解的时候,使用AnnotationConfigApplicationContext()方法建立ioc容器
现在所学的注解的作用:
@PostContract 和 @PreDestroy 需要配置导包才能使用
@Component("UserDao")和@Repository :自动注入所需要的属性
@Autowired:自动装配
@service :这个的后边建议加上名字
@Repose