@Autowired按类型自动装配的,但在我写的代码中并没有表现出来,请老师帮我看看,是什么情况
dao层代码如下:
package com.imooc.ioc.dao;
public interface IUserDao {
void insert();
}
package com.imooc.ioc.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao implements IUserDao {
public UserDao() {
System.out.println("执行UserDao构造方法" + this);
}
@Override
public void insert() {
System.out.println("插入一条用户数据" + this);
}
}
package com.imooc.ioc.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserMySqlDao implements IUserDao {
public UserMySqlDao() {
System.out.println("执行UserMySqlDao构造方法" + this);
}
@Override
public void insert() {
System.out.println("向MySQL数据库插入一条数据" + this);
}
}
package com.imooc.ioc.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserOracleDao implements IUserDao {
public UserOracleDao() {
System.out.println("执行UserOracleDao构造方法" + this);
}
@Override
public void insert() {
System.out.println("向Oracle数据库插入一条数据" + this);
}
}
service层代码如下:
package com.imooc.ioc.service;
import com.imooc.ioc.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private IUserDao userDao;
@Autowired
private IUserDao userMySqlDao;
@Autowired
private IUserDao userOracleDao;
// @Autowired
private IUserDao iUserDao;
public void insert(){
System.out.println("-------------------");
userDao.insert();
userMySqlDao.insert();
userOracleDao.insert();
iUserDao.insert();
}
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
public IUserDao getUserMySqlDao() {
return userMySqlDao;
}
public void setUserMySqlDao(IUserDao userMySqlDao) {
this.userMySqlDao = userMySqlDao;
}
public IUserDao getUserOracleDao() {
return userOracleDao;
}
public void setUserOracleDao(IUserDao userOracleDao) {
this.userOracleDao = userOracleDao;
}
}
程序入口代码如下:
package com.imooc.ioc;
import com.imooc.ioc.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.insert();
}
}
applicationContext.xml配置信息如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.imooc"/>
</beans>
pom.xml配置信息如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>TestAutowired</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
以上就是我写的全部代码,执行的结果依然是没有报老师演示的错误
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 9400 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星