129.【Spring 注解_IOC】
Spring 注解_IOC容器
(一)、组件注册
1. @Configuration 与 @Bean 容器注册组件
(1).无注解注入方式
- 在pom文件中加入spring-context依赖: xml文件和注解的包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency>
- 定义一个实体类
package com.jsxs.bean; /** * @Author Jsxs * @Date 2023/8/11 19:57 * @PackageName:com.jsxs.bean * @ClassName: Person * @Description: TODO * @Version 1.0 */ public class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public Person() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
- 在resource目录下的beans.xml配置文件中通过< bean>< /bean>标签注入类实例
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 通过Bean的方式进行我们的组件注入的操作 --> <bean id="person" class="com.jsxs.bean.Person"> <property name="name" value="李明"></property> <property name="age" value="19"></property> </bean> </beans>
- 获取容器中通过配置文件注入的实例对象
package com.jsxs; import com.jsxs.bean.Person; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @Author Jsxs * @Date 2023/8/11 20:14 * @PackageName:com.jsxs * @ClassName: MainTest * @Description: TODO * @Version 1.0 */ public class MainTest { public static void main(String[] args) { // 通过配置文件,创建ioc容器,并向容器中注入实列对象 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); // 根据bean的id获取容器中注入的实列对象 Person person = (Person)applicationContext.getBean("person"); System.out.println(person); } }
(2).注解注入方式
1.MyConfig.java
package com.jsxs.config; import com.jsxs.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author Jsxs * @Date 2023/8/12 9:05 * @PackageName:com.jsxs.config * @ClassName: MyConfig * @Description: TODO * @Version 1.0 */ @Configuration public class MyConfig { @Bean public Person person(){ return new Person("李三",21); } }
2.Main.java
package com.jsxs; import com.jsxs.bean.Person; import com.jsxs.config.MyConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @Author Jsxs * @Date 2023/8/12 9:07 * @PackageName:com.jsxs * @ClassName: Main * @Description: TODO * @Version 1.0 */ public class Main { public static void main(String[] args) { // 读取通过配置类,创建ioc容器,并向容器中注入实例对象 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Person person = (Person)applicationContext.getBean("person"); System.out.println(person); } }
2. @ComponentScan 自动扫描组件和自动扫描规则
(1).无注解扫描方式
只要我们加上 @Configuration 这个注解,这个类就会默认加入IOC容器。
- 设置三个组件,并通过包扫描的方式进行注入我们的容器。
package com.jsxs.Mapper; import org.springframework.stereotype.Repository; /** * @Author Jsxs * @Date 2023/8/12 9:39 * @PackageName:com.jsxs.Mapper * @ClassName: BookMapper * @Description: TODO * @Version 1.0 */ @Repository public class BookMapper { }
package com.jsxs.service; import org.springframework.stereotype.Service; /** * @Author Jsxs * @Date 2023/8/12 9:33 * @PackageName:com.jsxs.service * @ClassName: BookService * @Description: TODO * @Version 1.0 */ @Service public class BookService { }
package com.jsxs.controller; import org.springframework.stereotype.Controller; /** * @Author Jsxs * @Date 2023/8/12 9:33 * @PackageName:com.jsxs.controller * @ClassName: BookController * @Description: TODO * @Version 1.0 */ @Controller public class BookController { }
1. beans.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 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 包自动扫描: 凡是带有 @Controller @Service @Repository @Component --> <context:component-scan base-package="com.jsxs"/> <!-- 通过Bean的方式进行我们的组件注入的操作 --> <bean id="person" class="com.jsxs.bean.Person"> <property name="name" value="李明"/> <property name="age" value="19"/> </bean> </beans>
2.IOCTest.java
package com.jsxs.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @Author Jsxs * @Date 2023/8/12 9:39 * @PackageName:com.jsxs.Test * @ClassName: IOCTest * @Description: TODO * @Version 1.0 */ public class IOCTest { public static void main(String[] args) { // 读取通过配置文件注入容器中实列 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); // 进行遍历的操作 for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) { System.out.println(beanDefinitionName); } } }