ioc控制反转是通过Spring容器的getBean()方法来创建对象,而不是用户通过new来创建对象,Spring容器就像一个map集合,键:id 值:Object
getBean():通过Spring容器的键获取对象,创建对象的原理是反射机制
控制反转 1、spring读取配置文件,将扫描包路径下的Bean或配置在配置文件中的Bean 2、将这些Bean的类路劲通过key:id value:classpath 键值对方式管理 3、在需要用的地方通过getBean(id)实例化(1)什么是依赖注入 DI (dependency injection) 依赖注入 含义:就是给对象的属性设置值. 原来给对象的属性设置值: set方法给对象设置值 构造方法给对象初始化的时候设置值. (2)property标签 set方式设置属性(掌握) 让spring调set方法,前提条件类中必须有set方法
(1)对象比较多的话,开启注解扫描 (2)只有标记有注解的类,才会被创建对象且添加到ioc容器中 (3)四个注解
@Component //其他层
@Repository //Dao层
@Service //Service层
@Controller(“xxx”)//Controller层
注解默认id首字母小写
applicationContext.XML配置
<?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"> <!-- 使用注解方式进行创建对象 1.开启注解扫描 含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包中所有的类 查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象, 放到spring容器中 --> <context:component-scan base-package="com.wzx.annotation"/> </beans>(1)注入是什么? 就是查找之后,进行赋值 (2)三种注入方式
1 @Autowired @Qualifier(“bean的id”)
2 @Value("#{bean的id}")
3 @Resource(name=“bean的id值”)
/* 第一种注入方式: * 注入UserDao的实现类 * @Autowired 自动注入,写在类的成员变量上, 可以根据UserDao接口,创建该接口的实现类,在自动的设置到该成员变量上 */ @Autowired private Userdao uerDao; /** * 第二种注入方式: * @Value("#{bean的id}") */ //@Value("#{userDaoImpl02}") //private UserDao dao; @Value("柳岩") private String name; /** * 第三种注入方式 : 是jdk的注解不是spring * @Resource(name="bean的id值") */ @Resource(name="userDaoImpl") private UserDao dao;依赖
<!--日志包--> <!--日志包--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--Spring 依赖--> <!-- spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.9.RELEASE</version> </dependency>配置文件 项目结构
<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <!--配置扫描路径--> <context:component-scan base-package="com.xy"/> </beans>service层 要spring管理那个方法就在类加上注解
@Service public class PersonServiceImp implements PersonService { @Autowired//自动注入dao类 private PersonDaoImp personDao; @Override public int findPerson(Person person) { int i = 0; i = personDao.findPersonByName(person.getName()); return i; } }dao层
@Repository public class PersonDaoImp implements PersonDao { @Override public int findPersonByName(String name) { int i = 0; List list = new ArrayList(); list.add("ywf"); list.add("lyf"); list.add("wf"); if (list.indexOf(name) != -1) { i = 1; } return i; } }person类
public class Person { private String name; private String password; public Person(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }一定要在需要spring扫描管理的类上加上注解,不然会爆以下错误! 错误1
