当前位置: 首页> 教育> 就业 > 冰燃建站_网页制作的公司叫什么_今天重大新闻_搜索引擎的工作原理分为

冰燃建站_网页制作的公司叫什么_今天重大新闻_搜索引擎的工作原理分为

时间:2025/7/13 3:54:19来源:https://blog.csdn.net/WHabc2002/article/details/142659944 浏览次数:0次
冰燃建站_网页制作的公司叫什么_今天重大新闻_搜索引擎的工作原理分为

目录

1.什么是 IoC?

2.IoC类注解(五大注解)

2.1那为什么要这么多类注解? 

2.2五大注解是不是可以混用?

2.3程序被spring管理的条件是?

3.bean对象

3.1Bean 命名约定

3.2获取bean对象

4.⽅法注解 @Bean


1.什么是 IoC?

IoC: Inversion of Control (控制反转),是Spring的核⼼思想
什么是控制反转呢?
获得依赖对象的过程被反转了
也就是说, 当需要某个对象时, 传统开发模式中需要⾃⼰通过 new 创建对象, 现在不需要再进⾏创 建, 把创建对象的任务交给容器, 程序中只需要依赖注⼊ (Dependency Injection,DI)就可以了. 这个容器称为:IoC容器. Spring是⼀个IoC容器, 所以有时Spring 也称为Spring 容器。
通过上述我们还可以知道,Spring 是什么?
Spring 是包含了众多⼯具⽅法的 IoC 容器。

2.IoC类注解(五大注解)

前提: 

1.五大类注解:@Controller、@Service、@Repository、@Component、@Configuration 
2.bean对象:在spring容器中存放的对象。
3.ApplicationContext: 翻译过来就是: Spring 上下⽂。这个上下⽂, 就是指当前的运⾏环境, 也可以看作是⼀个容器。故ApplicationContext的对象中存放了所有与当前的运⾏环境有关的内容,比如 spring容器中存放的bean对象。

 它们的功能都是把对象交给spring管理。

2.1那为什么要这么多类注解? 

与应⽤分层呼应,让程序员看到类注解之后,就能直接了解当前类的⽤途。

(1)@Controller:控制层, 接收请求, 对请求进⾏处理, 并进⾏响应.  

(2)@Servie:业务逻辑层, 处理具体的业务逻辑 

(3)@Repository:数据访问层,也称为持久层. 负责数据访问操作  

(4)@Configuration:配置层. 处理项⽬中的⼀些配置信息. 

(5)@Component 是⼀个元注解,也就是说可以注解其他类注解

@Controller , @Service , @Repository ,@Configuration.这些注解被称为 @Component 的衍⽣注解。因为这些注解⾥⾯都有⼀个注解 @Component。

2.2五大注解是不是可以混用?

答:可以,但不是完全可以。

功能上:@Service , @Repository ,@Configuration,@Component 可以完全混用,@Controller有自己的特殊性。

规范上:不可以混用。因为我们想要与应⽤分层呼应。

2.3程序被spring管理的条件是?

 1.要被spring扫描到(默认路径是启动类所在的目录,包括子目录)

手动设置:

@ComponentScan(basePackages = "~~~~~")

2.需要配置五大注解和@Bean


3.bean对象

 1.   Object getBean(String name) throws BeansException;2.   <T> T getBean(String name, Class<T> requiredType) throws BeansException;3.   Object getBean(String name, Object... args) throws BeansException;4.   <T> T getBean(Class<T> requiredType) throws BeansException;5.   <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

 常⽤的是上述1,2,4种, 这三种⽅式,获取到的bean是⼀样的

其中1,2种都涉及到根据名称来获取对象. bean的名称是什么呢?

3.1Bean 命名约定

程序开发⼈员不需要为bean指定名称(BeanId), 如果没有显式的提供名称(BeanId),Spring容器将为该 bean⽣成唯⼀的名称.

1.一般命名约定使⽤Java标准约定作为实例字段名. 也就是说,bean名称以⼩写字⺟开头,然后使⽤驼峰式 ⼤⼩写。

2.特殊情况,当有多个字符并且第⼀个和第⼆个字符都是⼤写时, 将保留原始的⼤⼩写. 这些规则 与java.beans.Introspector.decapitalize (Spring在这⾥使⽤的)定义的规则相同.

3.使用@Bean注解的话,对象名就是添加@Bean注解方法的名称。

3.2获取bean对象

 使⽤ @Controller 存储 bean 的代码如下:

@Controller
public class UserController {public void sayHi(){System.out.println("UserController Hi");}
}

启动类: 

import com.wh.ioc.Controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class SpringIocApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);UserController bean1 = context.getBean(UserController.class);bean1.sayHi();UserController bean2 = (UserController) context.getBean("userController");bean2.sayHi();UserController bean3 = context.getBean("userController", UserController.class);bean3.sayHi();}}

4.⽅法注解 @Bean

类注解是添加到某个类上的, 但是存在两个问题:
1.使⽤外部包⾥的类, 没办法添加类注解
2.⼀个类, 需要多个对象, ⽐如多个数据源

⽅法注解 @Bean很好的解决了这两点。 

⽅法注解要配合类注解使⽤
在 Spring 框架的设计中,⽅法注解 @Bean 要配合类注解才能将对象正常的存储到 Spring 容器中。
外部类:Student
import lombok.Data;@Data
public class Student {private String name;private Integer id;public Student() {}public Student(String name) {this.name = name;}public Student(String name, Integer id) {this.name = name;this.id = id;}
}
BeanConfig类:
import com.wh.ioc.Model.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class BeanConfig {@Beanpublic Student StudentInfo() {return new Student("wh",01);}@Beanpublic Student StudentInfo2() {return new Student("Bob",02);}
}

启动类:

import com.wh.ioc.Controller.UserController;
import com.wh.ioc.Model.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class SpringIocApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);Student student = (Student) context.getBean("StudentInfo");System.out.println(student);Student student2 = (Student) context.getBean("StudentInfo2");System.out.println(student2);}
}

以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 

关键字:冰燃建站_网页制作的公司叫什么_今天重大新闻_搜索引擎的工作原理分为

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: