Spring

1.1.1.1 创建一个bean

  1. package com.zt.spring;
  2.  
  3. public class MyBean {
  4.  
  5. private String userName;
  6.  
  7. private Integer userAge;
  8.  
  9. }

  

1.1.1.2 配置Config 配置bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class MyBeanConfig {
  8. // 和xml中配置文件的bean的标签是一样的
  9. @Bean(name = "beanName")
  10. public MyBean createBean(){
  11. return new MyBean();
  12. }
  13. }

  

1.1.1.3 配置bean 单例还是多例的

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Scope;
  6.  
  7. @Configuration
  8. public class MyBeanConfig {
  9.  
  10. @Bean(name = "beanName")
  11. // 默认值是多例的 xml的方式可以在bean的标签上面 设置这个参数
  12. @Scope("prototype")
  13. public MyBean createBean(){
  14. return new MyBean();
  15. }
  16.  
  17. }

  

1.1.1.4 获取上文,获取bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class App {
  6. public static void main(String[] args) {
  7. // 获取上下文
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
  9. // 通过bean的那么获取bean
  10. System.out.println(context.getBean("beanName"));
  11. // 获取bean的class文件获取
  12. System.out.println(context.getBean(MyBean.class));
  13. context.close();
  14. }
  15. }

  

1.1.2 通过FactoryBean实现bena的装配

1.1.2.1 创建一个bean

  1. package com.zt.spring;
  2.  
  3. public class Car {
  4. }

  

1.1.2.2 配置Config 配置bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class MyBeanConfig {
  8.  
  9. @Bean
  10. public CarFactoryBean createRunnableFactoryBean() {
  11. return new CarFactoryBean();
  12. }
  13.  
  14. }

  

1.1.2.3 配置FactoryBean 生产bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.beans.factory.FactoryBean;
  4.  
  5. public class CarFactoryBean implements FactoryBean<Car> {
  6.  
  7. //获取到对应的实体
  8. @Override
  9. public Car getObject() throws Exception {
  10. return new Car();
  11. }
  12.  
  13. // 返回的额对应的是class文件
  14. @Override
  15. public Class<?> getObjectType() {
  16. return Car.class;
  17. }
  18.  
  19. //配置是不是单例
  20. @Override
  21. public boolean isSingleton() {
  22. return true;
  23. }
  24.  
  25. }

  

1.1.2.4 获取上文,获取bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class App {
  6. public static void main(String[] args) {
  7. // 获取上下文
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
  9. // 通过FactoryBean 创建的bean
  10. System.out.println(context.getBean(Car.class));
  11. // 通过name获取car的实体
  12. System.out.println(context.getBean("createRunnableFactoryBean"));
  13. context.close();
  14. }
  15. }

  

1.1.3 通过另外一种工工厂模式实现bena的装配

1.1.3.1 创建一个bean

  1. package com.zt.spring;
  2.  
  3. public class Jeep {
  4. }

  

1.1.3.2 配置Config 配置bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class MyBeanConfig {
  8.  
  9. @Bean
  10. public JeepFactory createJeepFactory() {
  11. return new JeepFactory();
  12. }
  13.  
  14. @Bean
  15. public Jeep createJeep(JeepFactory factory) {
  16. return factory.creat();
  17. }
  18. }

  

1.1.3.3 创建 Factory 实现构建方法

  1. package com.zt.spring;
  2.  
  3. public class JeepFactory {
  4.  
  5. public Jeep creat() {
  6. return new Jeep();
  7. }
  8. }

  

1.1.3.4 获取上文,获取bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class App {
  6. public static void main(String[] args) {
  7. // 获取上下文
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
  9. System.out.println(context.getBean("createJeep"));
  10. System.out.println(context.getBean(Jeep.class));
  11. context.close();
  12. }
  13. }

  

1.2 spring中bean的销毁方法

1.2.1 调用spring自己的初始化和销毁的方法

1.2.1.1 创建一个bean

  1. 并且继承InitializingBean, DisposableBean ,实现afterPropertiesSetdestroy方法

  

  1. package com.zt.spring;
  2.  
  3. import org.springframework.beans.factory.DisposableBean;
  4. import org.springframework.beans.factory.InitializingBean;
  5.  
  6. public class User implements InitializingBean, DisposableBean {
  7.  
  8. @Override
  9. public void afterPropertiesSet() throws Exception {
  10. System.out.println("=======afterPropertiesSet========");
  11. }
  12.  
  13. @Override
  14. public void destroy() throws Exception {
  15. System.out.println("=======destroy========");
  16. }
  17. }

  

1.2.1.2在conffig文件中装配这个bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class MyBeanConfig {
  8.  
  9. @Bean
  10. public User createUser() {
  11. return new User();
  12. }
  13. }

  

1.2.1.3 获取上下文

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class App {
  6. public static void main(String[] args) {
  7. // 获取上下文
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
  9. System.out.println(context.getBean(User.class));
  10. context.close();
  11. }
  12. }

  

1.2.2 调用自定义初始化和销毁的方法

1.2.2.1 创建一个bean

  1. 自定义initdestroy方法
  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class MyBeanConfig {
  8.  
  9. @Bean(initMethod = "init",destroyMethod = "destroy")
  10. public Dog createDog() {
  11. return new Dog();
  12. }
  13. }

  

1.2.2.2在conffig文件中装配这个bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class MyBeanConfig {
  8.  
  9. @Bean(initMethod = "init",destroyMethod = "destroy")
  10. public Dog createDog() {
  11. return new Dog();
  12. }
  13. }

  

1.2.2.3 获取上下文

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class App {
  6. public static void main(String[] args) {
  7. // 获取上下文
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
  9. System.out.println(context.getBean(Dog.class));
  10. context.close();
  11. }
  12. }

  

1.2.3 调用自定义并且注解初始化和销毁的方法

1.2.3.1 创建一个bean

  1. 自定义initdestroy方法 在加上注解放 @PostConstruct @PreDestroy的方式实现
  1. package com.zt.spring;
  2.  
  3. import javax.annotation.PostConstruct;
  4. import javax.annotation.PreDestroy;
  5.  
  6. public class UserInfo {
  7.  
  8. @PostConstruct
  9. public void afterPropertiesSet() throws Exception {
  10. System.out.println("=======afterPropertiesSet========");
  11. }
  12.  
  13. @PreDestroy
  14. public void destroy() throws Exception {
  15. System.out.println("=======destroy========");
  16. }
  17. }

  

1.2.3.2在conffig文件中装配这个bean

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. @Configuration
  7. public class MyBeanConfig {
  8. @Bean
  9. public UserInfo createUserInfo() {
  10. return new UserInfo();
  11. }
  12. }

  

1.2.2.3 获取上下文

  1. package com.zt.spring;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class App {
  6. public static void main(String[] args) {
  7. // 获取上下文
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
  9. System.out.println(context.getBean(UserInfo.class));
  10. context.close();
  11. }
  12. }

  

1.4 pom.文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>com..zt</groupId>
  8. <artifactId>spring</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10.  
  11. <properties>
  12. <project.build.sourceEncodding>UTF-8</project.build.sourceEncodding>
  13. <maven.compiler.source>1.8</maven.compiler.source>
  14. <maven.compiler.target>1.8</maven.compiler.target>
  15. </properties>
  16.  
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-context</artifactId>
  21. <version>4.3.20.RELEASE</version>
  22. </dependency>
  23. </dependencies>
  24.  
  25. </project>

  

最后

感谢你看到这里,说的都是自己的一些看法和见解,如有不对,请指正!觉得文章对你有帮助的话不妨给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!

java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊的更多相关文章

  1. Spring 中Bean的装配方式

    最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...

  2. Spring笔记2——Spring中Bean的装配

    1.引言 Spring中,对象无需自己负责查找或创建与其关联的其他对象,而是由容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间的协作关系的行为通常称为装配(Wiring),这也是依赖注入 ...

  3. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring中Bean的作用域

    作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...

  4. Spring中Bean的装配方式

    一.基于xml的装配 Student.java package com.yh; public class Student implements People { public void breath( ...

  5. spring中 Bean的装配 Bean后处理器

  6. 浅析Spring中bean的作用域

    一.前言   刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...

  7. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  8. Spring中bean的配置

    先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对象的时候,一般都是直接使用关键字类new一个对象,那这样有什么坏处呢?其实很显然的,使用new那么就表示当前模块已经 ...

  9. (转)Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别

    Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...

随机推荐

  1. Linux标准重定向-输入-输出-错误-多重

    一切皆文件,都是文件的操作 三种I/O设备 标准的输入输出 程序:指令+数据 读入数据:Input 输出数据:Output 系统中打开一个文件系统自动分配文件描述符,除了0,1,2是固定的,其他的都是 ...

  2. Java安全之Commons Collections3分析

    Java安全之Commons Collections3分析 文章首发:Java安全之Commons Collections3分析 0x00 前言 在学习完成前面的CC1链和CC2链后,其实再来看CC3 ...

  3. ubuntu18.04下的off-by-null:hitcon_2018_children_tcache

    又没做出来,先说说自己的思路 因为是off-by-null,所以准备构造重叠的chunk,但是发现程序里有memset,给构造prev size造成重大问题 所以来详细记录一下做题过程 先逆向,IDA ...

  4. Linux文件操作常用命令

    一.一些文件操作命令. 1.cd /home  进入"home目录" 2.cd ../ 返回上一级目录 3.cd -  返回上次所在的目录 4.pwd 显示工程路径 5.ll 显示 ...

  5. C 语言编程习惯总结

    笔者能力有限,如果文中出现错误的地方,还请各位朋友能够给我指出来,我将不胜感激,谢谢~ 引言 编程习惯的培养需要的是一个长期的过程,需要不断地总结,积累,并且我们需要从意识上认识其重要性,一个良好的编 ...

  6. 微信小程序--基于ColorUI构建皮皮虾短视频去水印组件(仅供学习使用)

    微信小程序--基于ColorUI构建皮皮虾短视频去水印组件(仅供学习使用) 没错,我是皮友,我想学习舞蹈(/doge)和瑜伽 ,要无水印的那种有助于我加深学习. 1.组件效果展示 2.组件引入准备 h ...

  7. 我的第二次C语言作业

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/SE2020-2/homework/11422 这个作业要求在哪里 https://www.cnblog ...

  8. 获取List集合对象中某一列属性值

    例:获取disposeList集合中CorpusMarkPage对象中的responseId属性,生成新的List集合 List<String> responseIdList = disp ...

  9. C++ 数据结构 3:树和二叉树

    1 树 1.1 定义 由一个或多个(n ≥ 0)结点组成的有限集合 T,有且仅有一个结点称为根(root),当 n > 1 时,其余的结点分为 m (m ≥ 0)个互不相交的有限集合T1,T2, ...

  10. Table is marked as crashed and should be repaired 解决办法

    遇到这个问题几个敲命令轻松搞定 1.首先进入mysql命令台: mysql -u root -p 回车  输入密码 2.查询所有的库 mysql> show databases; 3.进入数据库 ...