java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊
Spring
1.1.1.1 创建一个bean
- package com.zt.spring;
- public class MyBean {
- private String userName;
- private Integer userAge;
- }
1.1.1.2 配置Config 配置bean
- package com.zt.spring;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyBeanConfig {
- // 和xml中配置文件的bean的标签是一样的
- @Bean(name = "beanName")
- public MyBean createBean(){
- return new MyBean();
- }
- }
1.1.1.3 配置bean 单例还是多例的
- package com.zt.spring;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Scope;
- @Configuration
- public class MyBeanConfig {
- @Bean(name = "beanName")
- // 默认值是多例的 xml的方式可以在bean的标签上面 设置这个参数
- @Scope("prototype")
- public MyBean createBean(){
- return new MyBean();
- }
- }
1.1.1.4 获取上文,获取bean
- package com.zt.spring;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class App {
- public static void main(String[] args) {
- // 获取上下文
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
- // 通过bean的那么获取bean
- System.out.println(context.getBean("beanName"));
- // 获取bean的class文件获取
- System.out.println(context.getBean(MyBean.class));
- context.close();
- }
- }
1.1.2 通过FactoryBean实现bena的装配
1.1.2.1 创建一个bean
- package com.zt.spring;
- public class Car {
- }
1.1.2.2 配置Config 配置bean
- package com.zt.spring;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyBeanConfig {
- @Bean
- public CarFactoryBean createRunnableFactoryBean() {
- return new CarFactoryBean();
- }
- }
1.1.2.3 配置FactoryBean 生产bean
- package com.zt.spring;
- import org.springframework.beans.factory.FactoryBean;
- public class CarFactoryBean implements FactoryBean<Car> {
- //获取到对应的实体
- @Override
- public Car getObject() throws Exception {
- return new Car();
- }
- // 返回的额对应的是class文件
- @Override
- public Class<?> getObjectType() {
- return Car.class;
- }
- //配置是不是单例
- @Override
- public boolean isSingleton() {
- return true;
- }
- }
1.1.2.4 获取上文,获取bean
- package com.zt.spring;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class App {
- public static void main(String[] args) {
- // 获取上下文
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
- // 通过FactoryBean 创建的bean
- System.out.println(context.getBean(Car.class));
- // 通过name获取car的实体
- System.out.println(context.getBean("createRunnableFactoryBean"));
- context.close();
- }
- }
1.1.3 通过另外一种工工厂模式实现bena的装配
1.1.3.1 创建一个bean
- package com.zt.spring;
- public class Jeep {
- }
1.1.3.2 配置Config 配置bean
- package com.zt.spring;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyBeanConfig {
- @Bean
- public JeepFactory createJeepFactory() {
- return new JeepFactory();
- }
- @Bean
- public Jeep createJeep(JeepFactory factory) {
- return factory.creat();
- }
- }
1.1.3.3 创建 Factory 实现构建方法
- package com.zt.spring;
- public class JeepFactory {
- public Jeep creat() {
- return new Jeep();
- }
- }
1.1.3.4 获取上文,获取bean
- package com.zt.spring;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class App {
- public static void main(String[] args) {
- // 获取上下文
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
- System.out.println(context.getBean("createJeep"));
- System.out.println(context.getBean(Jeep.class));
- context.close();
- }
- }
1.2 spring中bean的销毁方法
1.2.1 调用spring自己的初始化和销毁的方法
1.2.1.1 创建一个bean
- 并且继承InitializingBean, DisposableBean ,实现afterPropertiesSet,destroy方法
- package com.zt.spring;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.InitializingBean;
- public class User implements InitializingBean, DisposableBean {
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("=======afterPropertiesSet========");
- }
- @Override
- public void destroy() throws Exception {
- System.out.println("=======destroy========");
- }
- }
1.2.1.2在conffig文件中装配这个bean
- package com.zt.spring;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyBeanConfig {
- @Bean
- public User createUser() {
- return new User();
- }
- }
1.2.1.3 获取上下文
- package com.zt.spring;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class App {
- public static void main(String[] args) {
- // 获取上下文
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
- System.out.println(context.getBean(User.class));
- context.close();
- }
- }
1.2.2 调用自定义初始化和销毁的方法
1.2.2.1 创建一个bean
自定义init,destroy方法
- package com.zt.spring;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyBeanConfig {
- @Bean(initMethod = "init",destroyMethod = "destroy")
- public Dog createDog() {
- return new Dog();
- }
- }
1.2.2.2在conffig文件中装配这个bean
- package com.zt.spring;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyBeanConfig {
- @Bean(initMethod = "init",destroyMethod = "destroy")
- public Dog createDog() {
- return new Dog();
- }
- }
1.2.2.3 获取上下文
- package com.zt.spring;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class App {
- public static void main(String[] args) {
- // 获取上下文
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
- System.out.println(context.getBean(Dog.class));
- context.close();
- }
- }
1.2.3 调用自定义并且注解初始化和销毁的方法
1.2.3.1 创建一个bean
自定义init,destroy方法 在加上注解放 @PostConstruct, @PreDestroy的方式实现
- package com.zt.spring;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- public class UserInfo {
- @PostConstruct
- public void afterPropertiesSet() throws Exception {
- System.out.println("=======afterPropertiesSet========");
- }
- @PreDestroy
- public void destroy() throws Exception {
- System.out.println("=======destroy========");
- }
- }
1.2.3.2在conffig文件中装配这个bean
- package com.zt.spring;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyBeanConfig {
- @Bean
- public UserInfo createUserInfo() {
- return new UserInfo();
- }
- }
1.2.2.3 获取上下文
- package com.zt.spring;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class App {
- public static void main(String[] args) {
- // 获取上下文
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
- System.out.println(context.getBean(UserInfo.class));
- context.close();
- }
- }
1.4 pom.文件
- <?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..zt</groupId>
- <artifactId>spring</artifactId>
- <version>1.0-SNAPSHOT</version>
- <properties>
- <project.build.sourceEncodding>UTF-8</project.build.sourceEncodding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.3.20.RELEASE</version>
- </dependency>
- </dependencies>
- </project>
最后
感谢你看到这里,说的都是自己的一些看法和见解,如有不对,请指正!觉得文章对你有帮助的话不妨给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!
java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊的更多相关文章
- Spring 中Bean的装配方式
最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...
- Spring笔记2——Spring中Bean的装配
1.引言 Spring中,对象无需自己负责查找或创建与其关联的其他对象,而是由容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间的协作关系的行为通常称为装配(Wiring),这也是依赖注入 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring中Bean的作用域
作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...
- Spring中Bean的装配方式
一.基于xml的装配 Student.java package com.yh; public class Student implements People { public void breath( ...
- spring中 Bean的装配 Bean后处理器
- 浅析Spring中bean的作用域
一.前言 刚刚花了点时间,阅读了一下Spring官方文档中,关于bean的作用域这一块的内容.Spring-4.3.21官方文档中,共介绍了七种bean作用域,这篇博客就来简单介绍一下这七种作用域 ...
- JAVA面试题:Spring中bean的生命周期
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- Spring中bean的配置
先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对象的时候,一般都是直接使用关键字类new一个对象,那这样有什么坏处呢?其实很显然的,使用new那么就表示当前模块已经 ...
- (转)Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...
随机推荐
- Linux标准重定向-输入-输出-错误-多重
一切皆文件,都是文件的操作 三种I/O设备 标准的输入输出 程序:指令+数据 读入数据:Input 输出数据:Output 系统中打开一个文件系统自动分配文件描述符,除了0,1,2是固定的,其他的都是 ...
- Java安全之Commons Collections3分析
Java安全之Commons Collections3分析 文章首发:Java安全之Commons Collections3分析 0x00 前言 在学习完成前面的CC1链和CC2链后,其实再来看CC3 ...
- ubuntu18.04下的off-by-null:hitcon_2018_children_tcache
又没做出来,先说说自己的思路 因为是off-by-null,所以准备构造重叠的chunk,但是发现程序里有memset,给构造prev size造成重大问题 所以来详细记录一下做题过程 先逆向,IDA ...
- Linux文件操作常用命令
一.一些文件操作命令. 1.cd /home 进入"home目录" 2.cd ../ 返回上一级目录 3.cd - 返回上次所在的目录 4.pwd 显示工程路径 5.ll 显示 ...
- C 语言编程习惯总结
笔者能力有限,如果文中出现错误的地方,还请各位朋友能够给我指出来,我将不胜感激,谢谢~ 引言 编程习惯的培养需要的是一个长期的过程,需要不断地总结,积累,并且我们需要从意识上认识其重要性,一个良好的编 ...
- 微信小程序--基于ColorUI构建皮皮虾短视频去水印组件(仅供学习使用)
微信小程序--基于ColorUI构建皮皮虾短视频去水印组件(仅供学习使用) 没错,我是皮友,我想学习舞蹈(/doge)和瑜伽 ,要无水印的那种有助于我加深学习. 1.组件效果展示 2.组件引入准备 h ...
- 我的第二次C语言作业
这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/SE2020-2/homework/11422 这个作业要求在哪里 https://www.cnblog ...
- 获取List集合对象中某一列属性值
例:获取disposeList集合中CorpusMarkPage对象中的responseId属性,生成新的List集合 List<String> responseIdList = disp ...
- C++ 数据结构 3:树和二叉树
1 树 1.1 定义 由一个或多个(n ≥ 0)结点组成的有限集合 T,有且仅有一个结点称为根(root),当 n > 1 时,其余的结点分为 m (m ≥ 0)个互不相交的有限集合T1,T2, ...
- Table is marked as crashed and should be repaired 解决办法
遇到这个问题几个敲命令轻松搞定 1.首先进入mysql命令台: mysql -u root -p 回车 输入密码 2.查询所有的库 mysql> show databases; 3.进入数据库 ...